Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1dc1acb

Browse files
PatrikKopkanvstinner
authored andcommitted
bpo-37064: Add option -a to pathfix.py tool (GH-15717)
Add option -a to Tools/Scripts/pathfix.py script: add flags.
1 parent 468f8a6 commit 1dc1acb

3 files changed

Lines changed: 88 additions & 23 deletions

File tree

Lib/test/test_tools/test_pathfix.py

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,90 @@ def setUp(self):
1717
self.temp_file = support.TESTFN
1818
self.addCleanup(support.unlink, support.TESTFN)
1919

20-
def pathfix(self, shebang, pathfix_flags):
20+
def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):
2121
with open(self.temp_file, 'w', encoding='utf8') as f:
2222
f.write(f'{shebang}\n' + 'print("Hello world")\n')
2323

2424
proc = subprocess.run(
2525
[sys.executable, self.script,
2626
*pathfix_flags, '-n', self.temp_file],
27-
capture_output=True)
28-
self.assertEqual(proc.returncode, 0, proc)
27+
capture_output=True, text=1)
28+
29+
if stdout == '' and proc.returncode == 0:
30+
stdout = f'{self.temp_file}: updating\n'
31+
self.assertEqual(proc.returncode, exitcode, proc)
32+
self.assertEqual(proc.stdout, stdout, proc)
33+
self.assertEqual(proc.stderr, stderr, proc)
2934

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

3338
lines = output.split('\n')
3439
self.assertEqual(lines[1:], ['print("Hello world")', ''])
35-
shebang = lines[0]
36-
return shebang
40+
new_shebang = lines[0]
41+
42+
if proc.returncode != 0:
43+
self.assertEqual(shebang, new_shebang)
44+
45+
return new_shebang
3746

3847
def test_pathfix(self):
3948
self.assertEqual(
4049
self.pathfix(
4150
'#! /usr/bin/env python',
42-
['-i', '/usr/bin/python3',]),
43-
'#! /usr/bin/python3',
44-
)
51+
['-i', '/usr/bin/python3']),
52+
'#! /usr/bin/python3')
4553
self.assertEqual(
4654
self.pathfix(
4755
'#! /usr/bin/env python -R',
48-
['-i', '/usr/bin/python3', ]),
49-
'#! /usr/bin/python3',
50-
)
56+
['-i', '/usr/bin/python3']),
57+
'#! /usr/bin/python3')
5158

5259
def test_pathfix_keeping_flags(self):
5360
self.assertEqual(
5461
self.pathfix(
5562
'#! /usr/bin/env python -R',
56-
['-i', '/usr/bin/python3', '-k',]),
57-
'#! /usr/bin/python3 -R',
58-
)
63+
['-i', '/usr/bin/python3', '-k']),
64+
'#! /usr/bin/python3 -R')
5965
self.assertEqual(
6066
self.pathfix(
6167
'#! /usr/bin/env python',
62-
['-i', '/usr/bin/python3', '-k',]),
63-
'#! /usr/bin/python3',
64-
)
68+
['-i', '/usr/bin/python3', '-k']),
69+
'#! /usr/bin/python3')
70+
71+
def test_pathfix_adding_flag(self):
72+
self.assertEqual(
73+
self.pathfix(
74+
'#! /usr/bin/env python',
75+
['-i', '/usr/bin/python3', '-a', 's']),
76+
'#! /usr/bin/python3 -s')
77+
self.assertEqual(
78+
self.pathfix(
79+
'#! /usr/bin/env python -S',
80+
['-i', '/usr/bin/python3', '-a', 's']),
81+
'#! /usr/bin/python3 -s')
82+
self.assertEqual(
83+
self.pathfix(
84+
'#! /usr/bin/env python -V',
85+
['-i', '/usr/bin/python3', '-a', 'v', '-k']),
86+
'#! /usr/bin/python3 -vV')
87+
self.assertEqual(
88+
self.pathfix(
89+
'#! /usr/bin/env python',
90+
['-i', '/usr/bin/python3', '-a', 'Rs']),
91+
'#! /usr/bin/python3 -Rs')
92+
self.assertEqual(
93+
self.pathfix(
94+
'#! /usr/bin/env python -W default',
95+
['-i', '/usr/bin/python3', '-a', 's', '-k']),
96+
'#! /usr/bin/python3 -sW default')
97+
98+
def test_pathfix_adding_errors(self):
99+
self.pathfix(
100+
'#! /usr/bin/env python -E',
101+
['-i', '/usr/bin/python3', '-a', 'W default', '-k'],
102+
exitcode=2,
103+
stderr="-a option doesn't support whitespaces")
65104

66105

67106
if __name__ == '__main__':
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Add flag -k to pathscript.py script: preserve shebang flags.
1+
Add option -k to pathscript.py script: preserve shebang flags.
2+
Add option -a to pathscript.py script: add flags.

Tools/scripts/pathfix.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`.
1515
# Normally, pathfix overwrites the entire line, including the flags.
1616
# To change interpreter and keep flags from the original shebang line, use -k.
17+
# If you want to keep flags and add to them one single literal flag, use option -a.
1718

1819

1920
# Undoubtedly you can do this using find and sed or perl, but this is
@@ -39,18 +40,20 @@
3940
preserve_timestamps = False
4041
create_backup = True
4142
keep_flags = False
43+
add_flags = b''
4244

4345

4446
def main():
4547
global new_interpreter
4648
global preserve_timestamps
4749
global create_backup
4850
global keep_flags
51+
global add_flags
4952

50-
usage = ('usage: %s -i /interpreter -p -n -k file-or-directory ...\n' %
53+
usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' %
5154
sys.argv[0])
5255
try:
53-
opts, args = getopt.getopt(sys.argv[1:], 'i:kpn')
56+
opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn')
5457
except getopt.error as msg:
5558
err(str(msg) + '\n')
5659
err(usage)
@@ -64,6 +67,11 @@ def main():
6467
create_backup = False
6568
if o == '-k':
6669
keep_flags = True
70+
if o == '-a':
71+
add_flags = a.encode()
72+
if b' ' in add_flags:
73+
err("-a option doesn't support whitespaces")
74+
sys.exit(2)
6775
if not new_interpreter or not new_interpreter.startswith(b'/') or \
6876
not args:
6977
err('-i option or file-or-directory missing\n')
@@ -188,15 +196,32 @@ def parse_shebang(shebangline):
188196
return shebangline[start:]
189197

190198

199+
def populate_flags(shebangline):
200+
old_flags = b''
201+
if keep_flags:
202+
old_flags = parse_shebang(shebangline)
203+
if old_flags:
204+
old_flags = old_flags[2:]
205+
if not (old_flags or add_flags):
206+
return b''
207+
# On Linux, the entire string following the interpreter name
208+
# is passed as a single argument to the interpreter.
209+
# e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
210+
# so shebang should have single '-' where flags are given and
211+
# flag might need argument for that reasons adding new flags is
212+
# between '-' and original flags
213+
# e.g. #! /usr/bin/python3 -sW Error
214+
return b' -' + add_flags + old_flags
215+
216+
191217
def fixline(line):
192218
if not line.startswith(b'#!'):
193219
return line
194220

195221
if b"python" not in line:
196222
return line
197-
flags = b''
198-
if keep_flags:
199-
flags = parse_shebang(line)
223+
224+
flags = populate_flags(line)
200225
return b'#! ' + new_interpreter + flags + b'\n'
201226

202227

0 commit comments

Comments
 (0)