From 53ff8e0ad9775885e3ae0740947507ddf9f2d7bc Mon Sep 17 00:00:00 2001 From: "Kelvin M. Klann" Date: Tue, 26 Nov 2024 23:04:32 -0300 Subject: [PATCH] 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 --- contrib/sort.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/contrib/sort.py b/contrib/sort.py index d1ca21d79..195f51ece 100755 --- a/contrib/sort.py +++ b/contrib/sort.py @@ -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 "):