9
9
from sys import argv , exit as sys_exit , stderr
10
10
11
11
__doc__ = f"""\
12
- Sort the arguments of commands in profiles.
12
+ Strip whitespace and sort the arguments of commands in profiles.
13
13
14
14
Usage: { path .basename (argv [0 ])} [-h] [-i] [-n] [--] [/path/to/profile ...]
15
15
20
20
21
21
Note that this is only applicable to commands that support multiple arguments.
22
22
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
+
23
27
Options:
24
28
-h Print this message.
25
29
-i Edit the profile file(s) in-place (this is the default).
42
46
43
47
def sort_alphabetical (original_items ):
44
48
items = original_items .split ("," )
45
- items = filter (None , set (items ))
49
+ items = set (map (str .strip , items ))
50
+ items = filter (None , items )
46
51
items = sorted (items )
47
52
return "," .join (items )
48
53
@@ -54,6 +59,9 @@ def sort_protocol(original_protocols):
54
59
unix,inet,inet6,netlink,packet,bluetooth
55
60
"""
56
61
62
+ # remove all whitespace
63
+ original_protocols = "" .join (original_protocols .split ())
64
+
57
65
# shortcut for common protocol lines
58
66
if original_protocols in ("unix" , "unix,inet,inet6" ):
59
67
return original_protocols
@@ -71,26 +79,25 @@ def check_profile(filename, overwrite):
71
79
lines = profile .read ().split ("\n " )
72
80
was_fixed = False
73
81
fixed_profile = []
74
- for lineno , line in enumerate (lines , 1 ):
82
+ for lineno , original_line in enumerate (lines , 1 ):
83
+ line = original_line .rstrip ()
75
84
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 :])} "
77
86
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 :])} "
79
88
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 :])} "
81
90
elif line [:8 ] == "protocol" :
82
- fixed_line = f"protocol { sort_protocol (line [9 :])} "
91
+ line = f"protocol { sort_protocol (line [9 :])} "
83
92
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 :
88
95
was_fixed = True
89
96
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 } "
92
99
)
93
- fixed_profile .append (fixed_line )
100
+ fixed_profile .append (line )
94
101
95
102
if was_fixed :
96
103
if overwrite :
0 commit comments