-
Notifications
You must be signed in to change notification settings - Fork 584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build: sort.py improvements #5429
Conversation
kmk3
commented
Oct 18, 2022
Instead of manually adding 1 to lineno.
To the sort function, instead of wrapping it in a lambda function.
Which also makes it fit in under 80 characters. Always print "profile(s)" instead of changing the message based on the argument count.
To make it clearer. Both the input and output of the sort_alphabetical function are strings of comma-separated items, so there is no format conversion of any kind being done (from "raw" to "not raw"), only sorting.
To make it clearer. There are 3 different instances of protocol-related objects being used in the fix_protocol function: * The input * The array of common sorted lines * The (sorted) output
afb2f52
to
ad74763
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ad74763
to
8b0502c
Compare
@rusty-snake reviewed on Oct 18:
Indeed; removed the reference to PEP-8. I just wanted to make it fit in 79 or 80 columns. Also, what do you think about making it print And using Misc: Interesting, I didn't know about this footnotes functionality. |
Sounds good. It should also do this if only
Are there some rules about using f-strings for docstrings?
|
8b0502c
to
261be8d
Compare
@rusty-snake reviewed on Oct 18:
I saw docstrings using both forms in the docs (and in SO answers); I'm still Should it be not blank in the main docstring and blank in functions? I think that it indeed makes sense for at least the main one to be non-blank, Though it doesn't look as good in the source and potentially makes the first @rusty-snake commented on Oct 18:
Since it currently does not have any options, IMO only printing it without
I don't know, but it works for me at least. The main difference is that the
It always prints the filename used for running the script, even if it was $ ./contrib/sort.py
./contrib/sort.py
$ (cd contrib && ./sort.py)
./sort.py
$ python contrib/sort.py
contrib/sort.py The same applies to shell scripts (and likely also to other scripting $ ./contrib/sort.sh
./contrib/sort.sh
$ (cd contrib && ./sort.sh)
./sort.sh
$ bash contrib/sort.sh
contrib/sort.sh For example, the shell script sort.sh
#!/bin/sh
usage() {
cat <<EOF
Sort the arguments of the commands that support multiple arguments in profiles;
the following commands are supported:
private-bin, private-etc, private-lib, caps.drop, caps.keep, seccomp.drop,
seccomp.drop, protocol
Usage: $(basename "$0") [/path/to/profile ...]
Keep in mind that this will overwrite your profile(s).
Examples:
$ $0 MyAwesomeProfile.profile
$ $0 new_profile.profile second_new_profile.profile
$ $0 ~/.config/firejail/*.{profile,inc,local}
$ sudo $0 /etc/firejail/*.{profile,inc,local}
Exit Codes:
0: Success: No profiles needed fixing.
1: Error: One or more profiles could not be processed correctly.
101: Info: One or more profile were fixed.
EOF
}
usage Output: output of sort.sh
$ ./contrib/sort.sh
Sort the arguments of the commands that support multiple arguments in profiles;
the following commands are supported:
private-bin, private-etc, private-lib, caps.drop, caps.keep, seccomp.drop,
seccomp.drop, protocol
Usage: sort.sh [/path/to/profile ...]
Keep in mind that this will overwrite your profile(s).
Examples:
$ ./contrib/sort.sh MyAwesomeProfile.profile
$ ./contrib/sort.sh new_profile.profile second_new_profile.profile
$ ./contrib/sort.sh ~/.config/firejail/*.{profile,inc,local}
$ sudo ./contrib/sort.sh /etc/firejail/*.{profile,inc,local}
Exit Codes:
0: Success: No profiles needed fixing.
1: Error: One or more profiles could not be processed correctly.
101: Info: One or more profile were fixed. Anyway, the main advantage of doing this is that the help section remains Since it's in a standalone script (and not a module in a larger system, for Anyway, I force-pushed with both changes; let me know what you think. Not sure if it's a very "Pythonic" solution, but I can't think of any other way |
027fcbf
to
7e172a5
Compare
Changes: * Line-wrap comments at 79 characters * Make comments clearer * Make main docstring more similar to a command "usage" output See the result with the following command, which generates a man-page-like output and opens it in the man pager (such as in `less`): $ pydoc ./contrib/sort.py See also PEP-257, "Docstring Conventions"[1]. [1] https://peps.python.org/pep-0257/
Misc: The trailing comma is due to using the opinionated `black` Python formatter (which seems to be a relatively common one). This was the only change made, so the code seems to already be following the format used by this tool.
Where applicable, instead of creating custom ones. Example error messages: rm -f 123 && ./contrib/sort.py 123 [ Error ] [Errno 2] No such file or directory: '123' touch 123 && chmod -rwx 123 && ./contrib/sort.py 123 [ Error ] [Errno 13] Permission denied: '123'
7e172a5
to
58e403b
Compare
(Force-push) Tried to make the main docstring a bit clearer; I think that it looks better pydoc contrib/sort.py Also, added more details to some commit messages. |
And return a specific exit code, as suggested by @rusty-snake[1]. Escape the first line in the docstring to avoid printing a blank line as the first line of the output. [1] netblue30#5429 (comment)
58e403b
to
5b16fb3
Compare
With this, the help section remains consistent regardless of how the script is called and even if the filename is changed. For example, if someone renames "sort.py" to "firejail-sort" and puts it somewhere in `$PATH`. Example outputs of the script name (using `print(argv[0]); return`): $ ./contrib/sort.py ./contrib/sort.py $ python contrib/sort.sh contrib/sort.py $ (cd contrib && ./sort.py) ./sort.py Note: This depends on `os.path` and `sys.argv`, so the imports have to appear before the docstring. In which case, the docstring has to be explicitly assigned to `__doc__` (as it ceases to be the first statement in the file). Note2: When running `pydoc ./contrib/sort.py`, `argv[0]` becomes "/usr/bin/pydoc" (using python 3.10.8-1 on Artix Linux).
5b16fb3
to
c648adc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me - I say merge it in @kmk3 !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the reminder in #5479.
Rename `line` to `original_line` to make it less likely to accidentally read from/write to it instead of the fixed line. Rename `fixed_line` to `line` to make the code shorter since it is now referenced much more often (up to 3 times in the same line of code) than the original line. See also commit aa17ca5 ("sort.py: rename protocols to original_protocols", 2022-10-17) / PR netblue30#5429.
Rename `line` to `original_line` to make it less likely to accidentally read from/write to it instead of the fixed line. Rename `fixed_line` to `line` to make the code shorter since it is now referenced much more often (up to 3 times in the same line of code) than the original line. See also commit aa17ca5 ("sort.py: rename protocols to original_protocols", 2022-10-17) / PR netblue30#5429.
Rename `line` to `original_line` to make it less likely to accidentally read from/write to it instead of the fixed line. Rename `fixed_line` to `line` to make the code shorter since it is now referenced much more often (up to 3 times in the same line of code) than the original line. See also commit aa17ca5 ("sort.py: rename protocols to original_protocols", 2022-10-17) / PR netblue30#5429.