Skip to content

Commit 3826645

Browse files
authored
Merge pull request netblue30#6556 from kmk3/sort-py-strip-ws
build: sort.py: strip whitespace in profiles
2 parents 4e8253a + 08e5f81 commit 3826645

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

contrib/sort.py

+21-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sys import argv, exit as sys_exit, stderr
1010

1111
__doc__ = f"""\
12-
Sort the arguments of commands in profiles.
12+
Strip whitespace and sort the arguments of commands in profiles.
1313
1414
Usage: {path.basename(argv[0])} [-h] [-i] [-n] [--] [/path/to/profile ...]
1515
@@ -20,6 +20,10 @@
2020
2121
Note that this is only applicable to commands that support multiple arguments.
2222
23+
Trailing whitespace is removed in all lines (that is, not just in lines
24+
containing supported commands) and other whitespace is stripped depending on
25+
the command.
26+
2327
Options:
2428
-h Print this message.
2529
-i Edit the profile file(s) in-place (this is the default).
@@ -42,7 +46,8 @@
4246

4347
def sort_alphabetical(original_items):
4448
items = original_items.split(",")
45-
items = filter(None, set(items))
49+
items = set(map(str.strip, items))
50+
items = filter(None, items)
4651
items = sorted(items)
4752
return ",".join(items)
4853

@@ -54,6 +59,9 @@ def sort_protocol(original_protocols):
5459
unix,inet,inet6,netlink,packet,bluetooth
5560
"""
5661

62+
# remove all whitespace
63+
original_protocols = "".join(original_protocols.split())
64+
5765
# shortcut for common protocol lines
5866
if original_protocols in ("unix", "unix,inet,inet6"):
5967
return original_protocols
@@ -71,26 +79,25 @@ def check_profile(filename, overwrite):
7179
lines = profile.read().split("\n")
7280
was_fixed = False
7381
fixed_profile = []
74-
for lineno, line in enumerate(lines, 1):
82+
for lineno, original_line in enumerate(lines, 1):
83+
line = original_line.rstrip()
7584
if line[:12] in ("private-bin ", "private-etc ", "private-lib "):
76-
fixed_line = f"{line[:12]}{sort_alphabetical(line[12:])}"
85+
line = f"{line[:12]}{sort_alphabetical(line[12:])}"
7786
elif line[:13] in ("seccomp.drop ", "seccomp.keep "):
78-
fixed_line = f"{line[:13]}{sort_alphabetical(line[13:])}"
87+
line = f"{line[:13]}{sort_alphabetical(line[13:])}"
7988
elif line[:10] in ("caps.drop ", "caps.keep "):
80-
fixed_line = f"{line[:10]}{sort_alphabetical(line[10:])}"
89+
line = f"{line[:10]}{sort_alphabetical(line[10:])}"
8190
elif line[:8] == "protocol":
82-
fixed_line = f"protocol {sort_protocol(line[9:])}"
91+
line = f"protocol {sort_protocol(line[9:])}"
8392
elif line[:8] == "seccomp ":
84-
fixed_line = f"{line[:8]}{sort_alphabetical(line[8:])}"
85-
else:
86-
fixed_line = line
87-
if fixed_line != line:
93+
line = f"{line[:8]}{sort_alphabetical(line[8:])}"
94+
if line != original_line:
8895
was_fixed = True
8996
print(
90-
f"{filename}:{lineno}:-{line}\n"
91-
f"{filename}:{lineno}:+{fixed_line}"
97+
f"{filename}:{lineno}:-{original_line}\n"
98+
f"{filename}:{lineno}:+{line}"
9299
)
93-
fixed_profile.append(fixed_line)
100+
fixed_profile.append(line)
94101

95102
if was_fixed:
96103
if overwrite:

0 commit comments

Comments
 (0)