aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Kelvin M. Klann <kmk3.code@protonmail.com>2024-03-25 08:51:59 +0000
committerLibravatar GitHub <noreply@github.com>2024-03-25 08:51:59 +0000
commitaa08aa132bcc48f550fa354c28877753ff2679a3 (patch)
treeec155f4a4e9ebdffba841e1d00a7e3b934511bd3
parentNew profile: qemu-common.profile (#6287) (diff)
downloadfirejail-aa08aa132bcc48f550fa354c28877753ff2679a3.tar.gz
firejail-aa08aa132bcc48f550fa354c28877753ff2679a3.tar.zst
firejail-aa08aa132bcc48f550fa354c28877753ff2679a3.zip
build: sort.py: add and require -i to edit in-place (#6290)
Similarly to `sed -i` and `perl -i`. This allows checking if sort.py correctly sorts the relevant lines in a profile without having to overwrite it, which makes debugging and testing easier (for example, in #6261). Note: If it finds items that are not sorted, it still sorts them, prints the diff and returns an error.
-rwxr-xr-xcontrib/sort.py36
1 files changed, 22 insertions, 14 deletions
diff --git a/contrib/sort.py b/contrib/sort.py
index 7a4f57da4..b65d87ab7 100755
--- a/contrib/sort.py
+++ b/contrib/sort.py
@@ -11,7 +11,7 @@ from sys import argv, exit as sys_exit, stderr
11__doc__ = f"""\ 11__doc__ = f"""\
12Sort the arguments of commands in profiles. 12Sort the arguments of commands in profiles.
13 13
14Usage: {path.basename(argv[0])} [/path/to/profile ...] 14Usage: {path.basename(argv[0])} [-i] [/path/to/profile ...]
15 15
16The following commands are supported: 16The following commands are supported:
17 17
@@ -20,13 +20,14 @@ The following commands are supported:
20 20
21Note that this is only applicable to commands that support multiple arguments. 21Note that this is only applicable to commands that support multiple arguments.
22 22
23Keep in mind that this will overwrite your profile(s). 23Options:
24 -i Edit the profile file(s) in-place.
24 25
25Examples: 26Examples:
26 $ {argv[0]} MyAwesomeProfile.profile 27 $ {argv[0]} -i MyAwesomeProfile.profile
27 $ {argv[0]} new_profile.profile second_new_profile.profile 28 $ {argv[0]} -i new_profile.profile second_new_profile.profile
28 $ {argv[0]} ~/.config/firejail/*.{{profile,inc,local}} 29 $ {argv[0]} -i ~/.config/firejail/*.{{profile,inc,local}}
29 $ sudo {argv[0]} /etc/firejail/*.{{profile,inc,local}} 30 $ sudo {argv[0]} -i /etc/firejail/*.{{profile,inc,local}}
30 31
31Exit Codes: 32Exit Codes:
32 0: Success: No profiles needed fixing. 33 0: Success: No profiles needed fixing.
@@ -62,7 +63,7 @@ def sort_protocol(original_protocols):
62 return fixed_protocols[:-1] 63 return fixed_protocols[:-1]
63 64
64 65
65def fix_profile(filename): 66def check_profile(filename, overwrite):
66 with open(filename, "r+") as profile: 67 with open(filename, "r+") as profile:
67 lines = profile.read().split("\n") 68 lines = profile.read().split("\n")
68 was_fixed = False 69 was_fixed = False
@@ -87,17 +88,24 @@ def fix_profile(filename):
87 f"{filename}:{lineno}:+{fixed_line}" 88 f"{filename}:{lineno}:+{fixed_line}"
88 ) 89 )
89 fixed_profile.append(fixed_line) 90 fixed_profile.append(fixed_line)
91
90 if was_fixed: 92 if was_fixed:
91 profile.seek(0) 93 if overwrite:
92 profile.truncate() 94 profile.seek(0)
93 profile.write("\n".join(fixed_profile)) 95 profile.truncate()
94 profile.flush() 96 profile.write("\n".join(fixed_profile))
95 print(f"[ Fixed ] {filename}") 97 profile.flush()
98 print(f"[ Fixed ] {filename}")
96 return 101 99 return 101
97 return 0 100 return 0
98 101
99 102
100def main(args): 103def main(args):
104 overwrite = False
105 if len(args) > 0 and args[0] == "-i":
106 overwrite = True
107 args.pop(0)
108
101 if len(args) < 1: 109 if len(args) < 1:
102 print(__doc__, file=stderr) 110 print(__doc__, file=stderr)
103 return 2 111 return 2
@@ -108,9 +116,9 @@ def main(args):
108 for filename in args: 116 for filename in args:
109 try: 117 try:
110 if exit_code not in (1, 101): 118 if exit_code not in (1, 101):
111 exit_code = fix_profile(filename) 119 exit_code = check_profile(filename, overwrite)
112 else: 120 else:
113 fix_profile(filename) 121 check_profile(filename, overwrite)
114 except FileNotFoundError as err: 122 except FileNotFoundError as err:
115 print(f"[ Error ] {err}", file=stderr) 123 print(f"[ Error ] {err}", file=stderr)
116 exit_code = 1 124 exit_code = 1