|
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.
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.
I don't know if necessary this example
|
Thanks @eamanu for review |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
This should fix the -f "" use case.
| new_interpreter = None | ||
| preserve_timestamps = False | ||
| create_backup = True | ||
| add_flag = '' |
There was a problem hiding this comment.
| add_flag = '' | |
| add_flag = None |
| return b'#! ' + new_interpreter + b'\n' | ||
|
|
||
| fixedline = b'#! ' + new_interpreter | ||
| if not add_flag: |
There was a problem hiding this comment.
| if not add_flag: | |
| if add_flag in None: |
| 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.
Shouldn't this test that the backup is not created?
| 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.
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 |
There was a problem hiding this comment.
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.
|
|
||
| # 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.
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 FLAGSThere was a problem hiding this comment.
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.
| 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.
I dislike global variables :-( Maybe all "options" and be stored in a "options" variable (namespace) when updating the code to argparse.
| """ | ||
| 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.
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.
| 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.
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