Skip to content

bpo-38347: find Python scripts whose name contain a '-' #16536

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 19 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions Lib/test/test_tools/test_pathfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,34 @@ class TestPathfixFunctional(unittest.TestCase):
script = os.path.join(scriptsdir, 'pathfix.py')

def setUp(self):
self.temp_file = support.TESTFN
self.addCleanup(support.unlink, support.TESTFN)

def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):
with open(self.temp_file, 'w', encoding='utf8') as f:
def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='',
directory=''):
if directory:
# bpo-38347: Test filename should contain lowercase, uppercase,
# "-", "_" and digits.
filename = os.path.join(directory, 'script-A_1.py')
pathfix_arg = directory
else:
filename = support.TESTFN
pathfix_arg = filename

with open(filename, 'w', encoding='utf8') as f:
f.write(f'{shebang}\n' + 'print("Hello world")\n')

proc = subprocess.run(
[sys.executable, self.script,
*pathfix_flags, '-n', self.temp_file],
*pathfix_flags, '-n', pathfix_arg],
capture_output=True, text=1)

if stdout == '' and proc.returncode == 0:
stdout = f'{self.temp_file}: updating\n'
stdout = f'{filename}: updating\n'
self.assertEqual(proc.returncode, exitcode, proc)
self.assertEqual(proc.stdout, stdout, proc)
self.assertEqual(proc.stderr, stderr, proc)

with open(self.temp_file, 'r', encoding='utf8') as f:
with open(filename, 'r', encoding='utf8') as f:
output = f.read()

lines = output.split('\n')
Expand All @@ -44,6 +53,19 @@ def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):

return new_shebang

def test_recursive(self):
tmpdir = support.TESTFN + '.d'
self.addCleanup(support.rmtree, tmpdir)
os.mkdir(tmpdir)
expected_stderr = f"recursedown('{os.path.basename(tmpdir)}')\n"
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3'],
directory=tmpdir,
stderr=expected_stderr),
'#! /usr/bin/python3')

def test_pathfix(self):
self.assertEqual(
self.pathfix(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pathfix.py: Assume all files that end on '.py' are Python scripts when working recursively.
5 changes: 1 addition & 4 deletions Tools/scripts/pathfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ def main():
sys.exit(bad)


ispythonprog = re.compile(r'^[a-zA-Z0-9_]+\.py$')


def ispython(name):
return bool(ispythonprog.match(name))
return name.endswith('.py')


def recursedown(dirname):
Expand Down