Skip to content

Commit

Permalink
build: sort.py: strip whitespace in commands
Browse files Browse the repository at this point in the history
Currently whitespace is left as is within an entry.

In a `protocol` entry, if there is whitespace between the command and
its argument or around an item, the item in question is dropped from the
output.

Changes:

* `protocol`: Strip all whitespace after `protocol `
* Other commands: Strip leading/trailing whitespace around each item,
  including any extra whitespace between a command and its argument

Note: Whitespace characters inside paths are left as is, as some paths
(such as `Foo Bar` may contain spaces.

Before:

    $ printf 'private-bin a,b\nprivate-bin  a,b\nprivate-bin  b,a\nprivate-bin  C,A  B\nprotocol  unix,net\nprotocol  inet,unix\n'
      >foo.profile
    $ ./contrib/sort.py -n foo.profile
    sort.py: checking 1 profile(s)...
    foo.profile:5:-protocol  unix,net
    foo.profile:5:+protocol
    foo.profile:6:-protocol  inet,unix
    foo.profile:6:+protocol unix

After:

    $ printf 'private-bin a,b\nprivate-bin  a,b\nprivate-bin  b,a\nprivate-bin  C,A  B\nprotocol  unix,net\nprotocol  inet,unix\n'
      >foo.profile
    $ ./contrib/sort.py -n foo.profile
    sort.py: checking 1 profile(s)...
    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
    foo.profile:4:-private-bin  C,A  B
    foo.profile:4:+private-bin A  B,C
    foo.profile:5:-protocol  unix,net
    foo.profile:5:+protocol unix
    foo.profile:6:-protocol  inet,unix
    foo.profile:6:+protocol unix,inet
  • Loading branch information
kmk3 committed Dec 5, 2024
1 parent 6b5f65b commit 0f23429
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions contrib/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
Note that this is only applicable to commands that support multiple arguments.
Leading/trailing whitespace is removed in all lines (that is, not just in lines
containing supported commands).
containing supported commands) and other whitespace is stripped depending on
the command.
Options:
-h Print this message.
Expand All @@ -45,7 +46,8 @@

def sort_alphabetical(original_items):
items = original_items.split(",")
items = filter(None, set(items))
items = set(map(str.strip, items))
items = filter(None, items)
items = sorted(items)
return ",".join(items)

Expand All @@ -57,6 +59,9 @@ def sort_protocol(original_protocols):
unix,inet,inet6,netlink,packet,bluetooth
"""

# remove all whitespace
original_protocols = "".join(original_protocols.split())

# shortcut for common protocol lines
if original_protocols in ("unix", "unix,inet,inet6"):
return original_protocols
Expand Down

0 comments on commit 0f23429

Please sign in to comment.