Skip to content

Commit 9ab61c4

Browse files
authored
Merge pull request #5429 from kmk3/sort-py-improvements
build: sort.py improvements
2 parents bf747f4 + c648adc commit 9ab61c4

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

contrib/sort.py

+56-35
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,61 @@
22
# This file is part of Firejail project
33
# Copyright (C) 2014-2022 Firejail Authors
44
# License GPL v2
5-
"""
6-
Sort the items of multi-item options in profiles, the following options are supported:
7-
private-bin, private-etc, private-lib, caps.drop, caps.keep, seccomp.drop, seccomp.drop, protocol
85

9-
Usage:
10-
$ ./sort.py /path/to/profile [ /path/to/profile2 /path/to/profile3 ... ]
6+
# Requirements:
7+
# python >= 3.6
8+
from os import path
9+
from sys import argv, exit as sys_exit, stderr
10+
11+
__doc__ = f"""\
12+
Sort the arguments of commands in profiles.
13+
14+
Usage: {path.basename(argv[0])} [/path/to/profile ...]
15+
16+
The following commands are supported:
17+
18+
private-bin, private-etc, private-lib, caps.drop, caps.keep, seccomp.drop,
19+
seccomp.drop, protocol
20+
21+
Note that this is only applicable to commands that support multiple arguments.
22+
1123
Keep in mind that this will overwrite your profile(s).
1224
1325
Examples:
14-
$ ./sort.py MyAwesomeProfile.profile
15-
$ ./sort.py new_profile.profile second_new_profile.profile
16-
$ ./sort.py ~/.config/firejail/*.{profile,inc,local}
17-
$ sudo ./sort.py /etc/firejail/*.{profile,inc,local}
18-
19-
Exit-Codes:
20-
0: No Error; No Profile Fixed.
21-
1: Error, one or more profiles were not processed correctly.
22-
101: No Error; One or more profile were fixed.
26+
$ {argv[0]} MyAwesomeProfile.profile
27+
$ {argv[0]} new_profile.profile second_new_profile.profile
28+
$ {argv[0]} ~/.config/firejail/*.{{profile,inc,local}}
29+
$ sudo {argv[0]} /etc/firejail/*.{{profile,inc,local}}
30+
31+
Exit Codes:
32+
0: Success: No profiles needed fixing.
33+
1: Error: One or more profiles could not be processed correctly.
34+
2: Error: Missing arguments.
35+
101: Info: One or more profiles were fixed.
2336
"""
2437

25-
# Requirements:
26-
# python >= 3.6
27-
from sys import argv, exit as sys_exit
28-
2938

30-
def sort_alphabetical(raw_items):
31-
items = raw_items.split(",")
32-
items.sort(key=lambda s: s.casefold())
39+
def sort_alphabetical(original_items):
40+
items = original_items.split(",")
41+
items.sort(key=str.casefold)
3342
return ",".join(items)
3443

3544

36-
def sort_protocol(protocols):
37-
"""sort the given protocols into this scheme: unix,inet,inet6,netlink,packet,bluetooth"""
45+
def sort_protocol(original_protocols):
46+
"""
47+
Sort the given protocols into the following order:
48+
49+
unix,inet,inet6,netlink,packet,bluetooth
50+
"""
3851

3952
# shortcut for common protocol lines
40-
if protocols in ("unix", "unix,inet,inet6"):
41-
return protocols
53+
if original_protocols in ("unix", "unix,inet,inet6"):
54+
return original_protocols
4255

4356
fixed_protocols = ""
4457
for protocol in ("unix", "inet", "inet6", "netlink", "packet", "bluetooth"):
4558
for prefix in ("", "-", "+", "="):
46-
if f",{prefix}{protocol}," in f",{protocols},":
59+
if f",{prefix}{protocol}," in f",{original_protocols},":
4760
fixed_protocols += f"{prefix}{protocol},"
4861
return fixed_protocols[:-1]
4962

@@ -53,7 +66,7 @@ def fix_profile(filename):
5366
lines = profile.read().split("\n")
5467
was_fixed = False
5568
fixed_profile = []
56-
for lineno, line in enumerate(lines):
69+
for lineno, line in enumerate(lines, 1):
5770
if line[:12] in ("private-bin ", "private-etc ", "private-lib "):
5871
fixed_line = f"{line[:12]}{sort_alphabetical(line[12:])}"
5972
elif line[:13] in ("seccomp.drop ", "seccomp.keep "):
@@ -69,8 +82,8 @@ def fix_profile(filename):
6982
if fixed_line != line:
7083
was_fixed = True
7184
print(
72-
f"{filename}:{lineno + 1}:-{line}\n"
73-
f"{filename}:{lineno + 1}:+{fixed_line}"
85+
f"{filename}:{lineno}:-{line}\n"
86+
f"{filename}:{lineno}:+{fixed_line}"
7487
)
7588
fixed_profile.append(fixed_line)
7689
if was_fixed:
@@ -84,22 +97,30 @@ def fix_profile(filename):
8497

8598

8699
def main(args):
100+
if len(args) < 1:
101+
print(__doc__, file=stderr)
102+
return 2
103+
104+
print(f"sort.py: checking {len(args)} profile(s)...")
105+
87106
exit_code = 0
88-
print(f"sort.py: checking {len(args)} {'profiles' if len(args) != 1 else 'profile'}...")
89107
for filename in args:
90108
try:
91109
if exit_code not in (1, 101):
92110
exit_code = fix_profile(filename)
93111
else:
94112
fix_profile(filename)
95-
except FileNotFoundError:
96-
print(f"[ Error ] Can't find `{filename}'")
113+
except FileNotFoundError as err:
114+
print(f"[ Error ] {err}", file=stderr)
97115
exit_code = 1
98-
except PermissionError:
99-
print(f"[ Error ] Can't read/write `{filename}'")
116+
except PermissionError as err:
117+
print(f"[ Error ] {err}", file=stderr)
100118
exit_code = 1
101119
except Exception as err:
102-
print(f"[ Error ] An error occurred while processing `{filename}': {err}")
120+
print(
121+
f"[ Error ] An error occurred while processing '{filename}': {err}",
122+
file=stderr,
123+
)
103124
exit_code = 1
104125
return exit_code
105126

0 commit comments

Comments
 (0)