diff options
Diffstat (limited to 'contrib/sort.py')
-rwxr-xr-x | contrib/sort.py | 36 |
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"""\ |
12 | Sort the arguments of commands in profiles. | 12 | Sort the arguments of commands in profiles. |
13 | 13 | ||
14 | Usage: {path.basename(argv[0])} [/path/to/profile ...] | 14 | Usage: {path.basename(argv[0])} [-i] [/path/to/profile ...] |
15 | 15 | ||
16 | The following commands are supported: | 16 | The following commands are supported: |
17 | 17 | ||
@@ -20,13 +20,14 @@ The following commands are supported: | |||
20 | 20 | ||
21 | Note that this is only applicable to commands that support multiple arguments. | 21 | Note that this is only applicable to commands that support multiple arguments. |
22 | 22 | ||
23 | Keep in mind that this will overwrite your profile(s). | 23 | Options: |
24 | -i Edit the profile file(s) in-place. | ||
24 | 25 | ||
25 | Examples: | 26 | Examples: |
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 | ||
31 | Exit Codes: | 32 | Exit 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 | ||
65 | def fix_profile(filename): | 66 | def 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 | ||
100 | def main(args): | 103 | def 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 |