build: sort.py: strip trailing whitespace in all lines

Currently the output is mangled if the last item on the line contains
trailing whitespace and is moved when sorting.

So remove trailing whitespace in all lines (that is, not just in lines
containing supported commands).

Leave leading whitespace as is for now since it could potentially be
used for indentation.

Before:

    $ printf '# hello world  \nprivate-bin a,b  \nprivate-bin b,a  \nprivate-bin  a,b\n' \
      >foo.profile
    $ ./contrib/sort.py -n foo.profile | tr ' ' .
    sort.py:.checking.1.profile(s)...
    foo.profile:3:-private-bin.b,a..
    foo.profile:3:+private-bin.a..,b

After:

    $ printf '# hello world  \nprivate-bin a,b  \nprivate-bin b,a  \n' \
      >foo.profile
    $ ./contrib/sort.py -n foo.profile | tr ' ' .
    sort.py:.checking.1.profile(s)...
    foo.profile:1:-#.hello.world..
    foo.profile:1:+#.hello.world
    foo.profile:2:-private-bin.a,b..
    foo.profile:2:+private-bin.a,b
    foo.profile:3:-private-bin.b,a..
    foo.profile:3:+private-bin.a,b
This commit is contained in:
Kelvin M. Klann 2024-11-26 23:04:32 -03:00
parent 406b1cb18e
commit 53ff8e0ad9

View file

@ -9,7 +9,7 @@ from os import path
from sys import argv, exit as sys_exit, stderr
__doc__ = f"""\
Sort the arguments of commands in profiles.
Strip whitespace and sort the arguments of commands in profiles.
Usage: {path.basename(argv[0])} [-h] [-i] [-n] [--] [/path/to/profile ...]
@ -20,6 +20,9 @@ The following commands are supported:
Note that this is only applicable to commands that support multiple arguments.
Trailing whitespace is removed in all lines (that is, not just in lines
containing supported commands).
Options:
-h Print this message.
-i Edit the profile file(s) in-place (this is the default).
@ -72,7 +75,7 @@ def check_profile(filename, overwrite):
was_fixed = False
fixed_profile = []
for lineno, original_line in enumerate(lines, 1):
line = original_line
line = original_line.rstrip()
if line[:12] in ("private-bin ", "private-etc ", "private-lib "):
line = f"{line[:12]}{sort_alphabetical(line[12:])}"
elif line[:13] in ("seccomp.drop ", "seccomp.keep "):