Skip to content

bpo-37064: add option -k to Tools/scripts/pathfix.py #15548

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

Merged
merged 8 commits into from
Sep 5, 2019
Merged

bpo-37064: add option -k to Tools/scripts/pathfix.py #15548

merged 8 commits into from
Sep 5, 2019

Conversation

PatrikKopkan
Copy link
Contributor

@PatrikKopkan PatrikKopkan commented Aug 27, 2019

  • with this option it is possible to keep flags when changing shebang line

https://bugs.python.org/issue37064

 - with this option it is possible to keep flags
when changing shebang line
@@ -33,16 +38,20 @@
new_interpreter = None
preserve_timestamps = False
create_backup = True
add_flag = None
Copy link
Member

Choose a reason for hiding this comment

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

This variable looks unused.


def parse_shebang(shebangline):
end = len(shebangline)
start = shebangline.find(b' -', 0, end) # .find() returns index at space
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the comment, IMHO it's not really helpful.

", 0, end" are the default value and so can be removed.

@@ -164,12 +180,28 @@ def fix(filename):
# Return success
return 0


def parse_shebang(shebangline):
end = len(shebangline)
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment that on Linux, the first whitespace (U+0020) indicates the beginning of options.

start = shebangline.find(b' -', 0, end) # .find() returns index at space
if start == -1:
return b'', b''
print(shebangline[(start + 2):end])
Copy link
Member

Choose a reason for hiding this comment

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

Don't commit debug print please (remove it).

def fixline(line):
global keep_flags
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that this global statement is useful. Variables are global by default.

if b"python" not in line:
return line
if keep_flags:
flags = parse_shebang(line)
return b'#! ' + new_interpreter + b" -" + flags
return b'#! ' + new_interpreter + b'\n'
Copy link
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:

    if keep_flags:
        flags = parse_shebang(line)
    else:
        flags = b''
    return b'#! ' + new_interpreter + flags + b'\n'

So parse_shebang() must remove the trailing newline.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have zero idea how I deleted. In editor I still have:
return b'#! ' + new_interpreter + b' -' + flags + b'\n'

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' + 'print("Hello world")\n')
Copy link
Member

Choose a reason for hiding this comment

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

Please add a test using -k when the shebang does not contain any flag.

self.temp_file = support.TESTFN

with open(self.temp_file, 'w') as f:
f.write('#! /usr/bin/env python -R\n' + 'print("Hello world")\n')
Copy link
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 an helper method which takes two arguments: (shebang, pathfix_flags) and return the new shebang. To get tests which look like:

def test_keep_flags(self):
  shebang = '#! /usr/bin/env python -R'
  self.assertEqual(
    self.pathfix(shebang, ['-i', '/usr/bin/python']),
    '#! /usr/bin/python')
  self.assertEqual(
    self.pathfix(shebang, ['-i', '/usr/bin/python', '-k']),
    '#! /usr/bin/python -R')


@classmethod
def setupUpClass(cls):
self.addCleanup(support.unlink, support.TESTFN)
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't work: self doesn't exist. You may use setUp().

@@ -0,0 +1 @@
Add flag -k to pathscript.py. This flag preserve flags from shebang before change and adds them after change.
Copy link
Member

Choose a reason for hiding this comment

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

I suggest: "Add flag -k to pathscript.py script: preserve shebang flags."

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@@ -0,0 +1,48 @@
import unittest, os, subprocess
Copy link
Member

Choose a reason for hiding this comment

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

Please put one import per line: see PEP 8.

Try also to sort imports.

subprocess.call([sys.executable, self.script, *pathfix_flags, self.temp_file])
with open(self.temp_file) as f:
output = f.read()
print(output)
Copy link
Member

Choose a reason for hiding this comment

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

Don't forget to remove debug print when you submit a PR.

self.addCleanup(support.unlink, support.TESTFN)

def pathfix(self, shebang, pathfix_flags):
with open(self.temp_file, 'w') as f:
Copy link
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 specify the encoding: pass encoding="utf8" to this open, and the other one below.

self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python', '-k', '-n']),
Copy link
Member

Choose a reason for hiding this comment

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

Please remove -n from tests and always put it in pathfix() function.

self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python', '-k', '-n']),
'#! /usr/bin/python -R\n' + 'print("Hello world")\n'
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that it's useful to check for 'print("Hello world")\n' in each test. I would prefer to test it in pathfix() and just return the first line.

Something like this in pathfix():

lines = output.split('\n')
self.assertEqual(lines[1:], ['print("Hello world")\n']
shebang = lines[0][:-1] # strip the newline
return shebang

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

Thanks, the code now looks way better! A few more comments.

shebang = lines[0][:-1] # strip the newline
return shebang

def test_pathfix_keeping_flags(self):
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer:

  • put test_pathfix_without_keeping_flags() code into test_pathfix()
  • move test_pathfix_keeping_flags() after test_pathfix()
  • add a test in test_pathfix_keeping_flags(): use -k but the shebang doesn't contain any flag (the bug that you just fixed)

def parse_shebang(shebangline):
shebangline = shebangline.strip()
end = len(shebangline)
start = shebangline.find(b' -', 0, end)
Copy link
Member

Choose a reason for hiding this comment

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

"end" argument is useless: shebangline.find(b' -') behaves the same and is simpler. Remove 0 and end.

if b"python" not in line:
return line
if keep_flags:
flags = parse_shebang(line)
return b'#! ' + new_interpreter + b" -" + flags
return b'#! ' + new_interpreter + b'\n'
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

@vstinner vstinner merged commit 50254ac into python:master Sep 5, 2019
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
Add flag -k to pathscript.py script: preserve shebang flags.
vstinner added a commit that referenced this pull request Sep 25, 2019
* bpo-37064: Add option -k to Tools/scripts/pathfix.py (GH-15548)

Add flag -k to pathscript.py script: preserve shebang flags.

(cherry picked from commit 50254ac)

* bpo-37064: Add option -a to pathfix.py tool (GH-15717)

Add option -a to Tools/Scripts/pathfix.py script: add flags.

(cherry picked from commit 1dc1acb)
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
Add flag -k to pathscript.py script: preserve shebang flags.
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
Add flag -k to pathscript.py script: preserve shebang flags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants