8000 bpo-38347: find Python scripts whose name contain a '-' by rpluem · Pull Request #16536 · python/cpython · GitHub
[go: up one dir, main page]

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 10 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
51 changes: 35 additions & 16 deletions Lib/test/test_tools/test_pathfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ 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)
self.temp_directory = support.TESTFN + '.d'
self.addCleanup(support.rmtree, self.temp_directory)
os.mkdir(self.temp_directory)
self.temp_file = self.temp_directory + os.sep + \
os.path.basename(support.TESTFN) + '-t.py'
self.addCleanup(support.unlink, self.temp_file)

def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='',
filename=''):
if filename == '':
filename = self.temp_file

def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):
with open(self.temp_file, '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', filename],
capture_output=True, text=1)

if stdout == '' and proc.returncode == 0:
Expand All @@ -44,55 +52,66 @@ def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):

return new_shebang

def test_pathfix(self):
def test_recursive(self):
temp_directory_basename = os.path.basename(self.temp_directory)
expected_stderr = f'recursedown(\'{temp_directory_basename}\')\n'
for method_name in dir(self):
method = getattr(self, method_name)
if method_name.startswith('test_') and \
method_name != 'test_recursive' and \
method_name != 'test_pathfix_adding_errors' and \
callable(method):
method(filename=self.temp_directory, stderr=expected_stderr)

def test_pathfix(self, **kwargs):
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3']),
['-i', '/usr/bin/python3'], **kwargs),
'#! /usr/bin/python3')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python3']),
['-i', '/usr/bin/python3'], **kwargs),
'#! /usr/bin/python3')

def test_pathfix_keeping_flags(self):
def test_pathfix_keeping_flags(self, **kwargs):
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python3', '-k']),
['-i', '/usr/bin/python3', '-k'], **kwargs),
'#! /usr/bin/python3 -R')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3', '-k']),
['-i', '/usr/bin/python3', '-k'], **kwargs),
'#! /usr/bin/python3')

def test_pathfix_adding_flag(self):
def test_pathfix_adding_flag(self, **kwargs):
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3', '-a', 's']),
['-i', '/usr/bin/python3', '-a', 's'], **kwargs),
'#! /usr/bin/python3 -s')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -S',
['-i', '/usr/bin/python3', '-a', 's']),
['-i', '/usr/bin/python3', '-a', 's'], **kwargs),
'#! /usr/bin/python3 -s')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -V',
['-i', '/usr/bin/python3', '-a', 'v', '-k']),
['-i', '/usr/bin/python3', '-a', 'v', '-k'], **kwargs),
'#! /usr/bin/python3 -vV')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3', '-a', 'Rs']),
['-i', '/usr/bin/python3', '-a', 'Rs'], **kwargs),
'#! /usr/bin/python3 -Rs')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -W default',
['-i', '/usr/bin/python3', '-a', 's', '-k']),
['-i', '/usr/bin/python3', '-a', 's', '-k'], **kwargs),
'#! /usr/bin/python3 -sW default')

def test_pathfix_adding_errors(self):
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
0