Skip to content

bpo-37064: adds option to keep/add flags to pathfix. - #13591

Closed
PatrikKopkan wants to merge 22 commits into
python:masterfrom
PatrikKopkan:pathfix
Closed

bpo-37064: adds option to keep/add flags to pathfix.#13591
PatrikKopkan wants to merge 22 commits into
python:masterfrom
PatrikKopkan:pathfix

Conversation

@PatrikKopkan

@PatrikKopkan PatrikKopkan commented May 27, 2019

Copy link
Copy Markdown
Contributor

@the-knights-who-say-ni

Copy link
Copy Markdown

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
before our records are updated.

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.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Comment thread Lib/test/test_tools/test_pathfix.py Outdated
@PatrikKopkan

Copy link
Copy Markdown
Contributor Author

Thanks @eamanu for review

@auvipy auvipy left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@brettcannon brettcannon added the type-feature A feature request or enhancement label Jun 3, 2019

@hroncok hroncok left a comment

Copy link
Copy Markdown
Contributor

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 hroncok left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread Tools/scripts/pathfix.py Outdated
new_interpreter = None
preserve_timestamps = False
create_backup = True
add_flag = ''

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_flag = ''
add_flag = None

Comment thread Tools/scripts/pathfix.py Outdated
return b'#! ' + new_interpreter + b'\n'

fixedline = b'#! ' + new_interpreter
if not add_flag:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not add_flag:
if add_flag in None:

Comment thread 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')

Copy link
Copy Markdown
Member

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?

Comment thread 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 = \

Copy link
Copy Markdown
Member

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.

@PatrikKopkan

Copy link
Copy Markdown
Contributor Author

Here is failed test_os. I believe it is not related to this PR change.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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.

Comment thread 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 "".

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor

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.

Comment thread 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

Copy link
Copy Markdown
Member

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.

Comment thread 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():

Copy link
Copy Markdown
Member

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.

Comment thread 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'

Copy link
Copy Markdown
Member

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.

@encukou

encukou commented Jul 8, 2019

Copy link
Copy Markdown
Member

I suggest to work on a first PR only to replace optparse with argparse and add a few basic tests, before adding new features.

Are you saying an enhancement can't be accepted without refactoring the whole tool?

@vstinner

vstinner commented Jul 9, 2019

Copy link
Copy Markdown
Member

Are you saying an enhancement can't be accepted without refactoring the whole tool?

It's not are requirement, just a suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants