Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: sort.py: add and require -i to edit in-place #6290

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions contrib/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__doc__ = f"""\
Sort the arguments of commands in profiles.

Usage: {path.basename(argv[0])} [/path/to/profile ...]
Usage: {path.basename(argv[0])} [-i] [/path/to/profile ...]

The following commands are supported:

Expand All @@ -20,13 +20,14 @@

Note that this is only applicable to commands that support multiple arguments.

Keep in mind that this will overwrite your profile(s).
Options:
-i Edit the profile file(s) in-place.

Examples:
$ {argv[0]} MyAwesomeProfile.profile
$ {argv[0]} new_profile.profile second_new_profile.profile
$ {argv[0]} ~/.config/firejail/*.{{profile,inc,local}}
$ sudo {argv[0]} /etc/firejail/*.{{profile,inc,local}}
$ {argv[0]} -i MyAwesomeProfile.profile
$ {argv[0]} -i new_profile.profile second_new_profile.profile
$ {argv[0]} -i ~/.config/firejail/*.{{profile,inc,local}}
$ sudo {argv[0]} -i /etc/firejail/*.{{profile,inc,local}}

Exit Codes:
0: Success: No profiles needed fixing.
Expand Down Expand Up @@ -62,7 +63,7 @@ def sort_protocol(original_protocols):
return fixed_protocols[:-1]


def fix_profile(filename):
def check_profile(filename, overwrite):
with open(filename, "r+") as profile:
lines = profile.read().split("\n")
was_fixed = False
Expand All @@ -87,17 +88,24 @@ def fix_profile(filename):
f"{filename}:{lineno}:+{fixed_line}"
)
fixed_profile.append(fixed_line)

if was_fixed:
profile.seek(0)
profile.truncate()
profile.write("\n".join(fixed_profile))
profile.flush()
print(f"[ Fixed ] {filename}")
if overwrite:
profile.seek(0)
profile.truncate()
profile.write("\n".join(fixed_profile))
profile.flush()
print(f"[ Fixed ] {filename}")
return 101
return 0


def main(args):
overwrite = False
if len(args) > 0 and args[0] == "-i":
overwrite = True
args.pop(0)

if len(args) < 1:
print(__doc__, file=stderr)
return 2
Expand All @@ -108,9 +116,9 @@ def main(args):
for filename in args:
try:
if exit_code not in (1, 101):
exit_code = fix_profile(filename)
exit_code = check_profile(filename, overwrite)
else:
fix_profile(filename)
check_profile(filename, overwrite)
except FileNotFoundError as err:
print(f"[ Error ] {err}", file=stderr)
exit_code = 1
Expand Down