-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-37064: adds option to keep/add flags to pathfix. #13591
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
Conversation
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
| @@ -0,0 +1,3 @@ | |||
| adds option -f to pathscript.py. The option takes argument and is for adding flags. If argument is empty, it just keeps flags. | |||
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.
What about:
Add option -f to pathscript.py. This option takes argument to adding flags ...
| @@ -0,0 +1,3 @@ | |||
| adds option -f to pathscript.py. The option takes argument and is for adding flags. If argument is empty, it just keeps flags. | |||
| Let's say you have script with '#! /usr/bin/env -R' and you want to just change interpreter. You can run now | |||
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.
I don't know if necessary this example
|
Thanks @eamanu for review |
auvipy
left a comment
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
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.
The usage is not updated:
$ python3 Tools/scripts/pathfix.py
-i option or file-or-directory missing
usage: Tools/scripts/pathfix.py -i /interpreter -p -n file-or-directory ...
Also, not sure how to use -f without a value:
$ python3 Tools/scripts/pathfix.py -i /usr/bin/python3 -f script
-i option or file-or-directory missing
usage: Tools/scripts/pathfix.py -i /interpreter -p -n file-or-directory ...
$ python3 Tools/scripts/pathfix.py -i /usr/bin/python3 script -f
script: updating
-f: cannot open: FileNotFoundError(2, 'No such file or directory')
$ cat script
#!python -xyz
[cpython (master %)]$ python3 Tools/scripts/pathfix.py -i /usr/bin/python3.7 -f "" script
script: updating
[cpython (master %)]$ cat script
#! /usr/bin/python3.7
hroncok
left a comment
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.
This should fix the -f "" use case.
Tools/scripts/pathfix.py
Outdated
| new_interpreter = None | ||
| preserve_timestamps = False | ||
| create_backup = True | ||
| add_flag = '' |
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.
| add_flag = '' | |
| add_flag = None |
Tools/scripts/pathfix.py
Outdated
| return b'#! ' + new_interpreter + b'\n' | ||
|
|
||
| fixedline = b'#! ' + new_interpreter | ||
| if not add_flag: |
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.
| if not add_flag: | |
| if add_flag in None: |
Lib/test/test_tools/test_pathfix.py
Outdated
| subprocess.call([sys.executable, self.script, '-i', '/usr/bin/python', '-n', self.temp_file]) | ||
| with open(self.temp_file) as f: | ||
| output = f.read() | ||
| self.assertEqual(output, '#! /usr/bin/python\n') |
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.
Shouldn't this test that the backup is not created?
Lib/test/test_tools/test_pathfix.py
Outdated
| testing_output = self.pathfix.fixline(line) | ||
| self.assertEqual(testing_output, b'#! ' + self.pathfix.new_interpreter + b'\n') | ||
|
|
||
| input_file = \ |
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.
Please keep to PEP 8:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
|
Here is failed |
vstinner
left a comment
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.
pathfix.py seems to be old and can be modernized. I suggest to work on a first PR only to replace optparse with argparse and add a few basic tests, before adding new features.
Tools/scripts/pathfix.py
Outdated
|
|
||
| # Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`. | ||
| # Normally, pathfix overwrites the entire line, including the flags. | ||
| # To keep flags from the original shebang line, use -f "". |
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.
I would prefer to have one option to preserve existing flags and another to add more flags. Like:
pathfix.py --preserve-flags --add-flags "..." script
where --add-flags "..." would accept an arbitrary string like: --add-flags "-X dev --check-hash-based-pycs=never".
I suggest to replace legacy optparse with newer argparse, it's easier to use "long flags" (like --preserve-flags) in argparse.
If you really want to short flags (single letter), I suggest:
--preserve-flags/-p--add-flags/-a FLAGS
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.
The Linux kernel treats everything following the first “word” in the shebang line as a single argument. Such flags as -X dev --check-hash-based-pycs=never are impossible here.
Tools/scripts/pathfix.py
Outdated
| global preserve_timestamps | ||
| global create_backup | ||
| usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' % | ||
| global add_flag |
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.
I dislike global variables :-( Maybe all "options" and be stored in a "options" variable (namespace) when updating the code to argparse.
Tools/scripts/pathfix.py
Outdated
| """ | ||
| end = len(shebangline) | ||
| start = shebangline.find(b' -', 0, end) + 2 # .find() returns index at space | ||
| if chr(shebangline[start]).isalpha(): |
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.
This code looks fragile. It doesn't support --check-hash-based-pycs=MODE option.
IMHO it could be simpler: cut the string at " -" without checking what is next.
The Linux kernel simply uses a space to separate the end of the program name and the start of command line arguments. Maybe " -" is maybe safer than just " ", since I'm not sure how the Windows launcher py.exe handles spaces.
Tools/scripts/pathfix.py
Outdated
| if add_flag in flags: | ||
| return fixedline + b' -' + flags + args + b'\n' | ||
|
|
||
| return fixedline + b' -' + add_flag + flags + args + b'\n' |
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.
I would expect a line like: b'#! ' + new_interpreter + flags + b'\n'. Then arrange your code to build flags as you want, flags can an empty string.
I also suggest to create a sub-function fix_shebang() to clarify the purpose of the function.
Are you saying an enhancement can't be accepted without refactoring the whole tool? |
It's not are requirement, just a suggestion. |
https://bugs.python.org/issue37064