From 211987de0de73e2747e14fcfa217b20c1ec7d817 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Fri, 6 Sep 2019 15:09:43 +0200 Subject: [PATCH 01/11] bpo-37064: add -a to Tools/Scripts/pathfix.py - this option enables to add single literal flag to kept flags --- Lib/test/test_tools/test_pathfix.py | 31 +++++++++++++++++-- .../2019-05-27-15-26-12.bpo-37064.k_SPW2.rst | 3 +- Tools/scripts/pathfix.py | 25 +++++++++++++-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index 0c1aea7211703fc..7b94af03731bd7e 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -17,7 +17,7 @@ def setUp(self): self.temp_file = support.TESTFN self.addCleanup(support.unlink, support.TESTFN) - def pathfix(self, shebang, pathfix_flags): + def pathfix(self, shebang, pathfix_flags, expected_returncode=0): with open(self.temp_file, 'w', encoding='utf8') as f: f.write(f'{shebang}\n' + 'print("Hello world")\n') @@ -25,7 +25,7 @@ def pathfix(self, shebang, pathfix_flags): [sys.executable, self.script, *pathfix_flags, '-n', self.temp_file], capture_output=True) - self.assertEqual(proc.returncode, 0, proc) + self.assertEqual(proc.returncode, expected_returncode, proc) with open(self.temp_file, 'r', encoding='utf8') as f: output = f.read() @@ -63,6 +63,33 @@ def test_pathfix_keeping_flags(self): '#! /usr/bin/python3', ) + def test_pathfix_adding_flag(self): + self.assertEqual( + self.pathfix( + '#! /usr/bin/env python', + ['-i', '/usr/bin/python3', '-a', 's',]), + '#! /usr/bin/python3 -s', + ) + self.assertEqual( + self.pathfix( + '#! /usr/bin/env python -s', + ['-i', '/usr/bin/python3', '-a', 's', ]), + '#! /usr/bin/python3 -ss', + ) + self.assertEqual( + self.pathfix( + '#! /usr/bin/env python', + ['-i', '/usr/bin/python3', '-a', 'Rs',], + expected_returncode=2), + '#! /usr/bin/env python' + ) + self.assertEqual( + self.pathfix( + '#! /usr/bin/env python -W something', + ['-i', '/usr/bin/python3', '-a', 's', ]), + '#! /usr/bin/python3 -sW something', + ) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst b/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst index 02599077c9d5f97..69b89c22f29b044 100644 --- a/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst +++ b/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst @@ -1 +1,2 @@ -Add flag -k to pathscript.py script: preserve shebang flags. \ No newline at end of file +Add flag -k to pathscript.py script: preserve shebang flags. +Add option -a to pathscript.py script: add single literal flag to preserved flags. \ No newline at end of file diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index eee684e3502d5bc..b354f143e3c19c2 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -14,6 +14,7 @@ # Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`. # Normally, pathfix overwrites the entire line, including the flags. # To change interpreter and keep flags from the original shebang line, use -k. +# If you want to keep flags and add to them one single literal flag, use option -a. # Undoubtedly you can do this using find and sed or perl, but this is @@ -39,18 +40,19 @@ preserve_timestamps = False create_backup = True keep_flags = False - +add_flag = False def main(): global new_interpreter global preserve_timestamps global create_backup global keep_flags + global add_flag - usage = ('usage: %s -i /interpreter -p -n -k file-or-directory ...\n' % + usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' % sys.argv[0]) try: - opts, args = getopt.getopt(sys.argv[1:], 'i:kpn') + opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn') except getopt.error as msg: err(str(msg) + '\n') err(usage) @@ -64,6 +66,13 @@ def main(): create_backup = False if o == '-k': keep_flags = True + if o == '-a': + add_flag = a.encode() + if len(add_flag) > 1: + err('-a option supports one literal flag') + err(usage) + sys.exit(2) + if not new_interpreter or not new_interpreter.startswith(b'/') or \ not args: err('-i option or file-or-directory missing\n') @@ -188,6 +197,14 @@ def parse_shebang(shebangline): return shebangline[start:] +def insert_flag_to_shebang(shebangline): + flags = parse_shebang(shebangline) + if flags: + flags = flags[2:] + + return b' -' + add_flag + flags + + def fixline(line): if not line.startswith(b'#!'): return line @@ -197,6 +214,8 @@ def fixline(line): flags = b'' if keep_flags: flags = parse_shebang(line) + if add_flag: + flags = insert_flag_to_shebang(line) return b'#! ' + new_interpreter + flags + b'\n' From 301850ba0f3d0310350ee1845710a609caa75a50 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Fri, 6 Sep 2019 18:14:45 +0200 Subject: [PATCH 02/11] make -k not implicit --- Lib/test/test_tools/test_pathfix.py | 23 ++++++++++++++--------- Tools/scripts/pathfix.py | 28 +++++++++++++--------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index 7b94af03731bd7e..908be1163e545de 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -39,7 +39,7 @@ def test_pathfix(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3',]), + ['-i', '/usr/bin/python3', ]), '#! /usr/bin/python3', ) self.assertEqual( @@ -53,13 +53,13 @@ def test_pathfix_keeping_flags(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', - ['-i', '/usr/bin/python3', '-k',]), + ['-i', '/usr/bin/python3', '-k', ]), '#! /usr/bin/python3 -R', ) self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', '-k',]), + ['-i', '/usr/bin/python3', '-k', ]), '#! /usr/bin/python3', ) @@ -67,26 +67,31 @@ def test_pathfix_adding_flag(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', '-a', 's',]), + ['-i', '/usr/bin/python3', '-a', 's', ]), '#! /usr/bin/python3 -s', ) self.assertEqual( self.pathfix( '#! /usr/bin/env python -s', ['-i', '/usr/bin/python3', '-a', 's', ]), - '#! /usr/bin/python3 -ss', + '#! /usr/bin/python3 -s', + ) + self.assertEqual( + self.pathfix( + '#! /usr/bin/env python -v', + ['-i', '/usr/bin/python3', '-a', 'v', '-k', ]), + '#! /usr/bin/python3 -vv', ) self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', '-a', 'Rs',], - expected_returncode=2), - '#! /usr/bin/env python' + ['-i', '/usr/bin/python3', '-a', 'Rs', ]), + '#! /usr/bin/python3 -Rs' ) self.assertEqual( self.pathfix( '#! /usr/bin/env python -W something', - ['-i', '/usr/bin/python3', '-a', 's', ]), + ['-i', '/usr/bin/python3', '-a', 's', '-k', ]), '#! /usr/bin/python3 -sW something', ) diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index b354f143e3c19c2..989c8f25b9f85ea 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -40,7 +40,8 @@ preserve_timestamps = False create_backup = True keep_flags = False -add_flag = False +add_flag = b'' + def main(): global new_interpreter @@ -68,10 +69,6 @@ def main(): keep_flags = True if o == '-a': add_flag = a.encode() - if len(add_flag) > 1: - err('-a option supports one literal flag') - err(usage) - sys.exit(2) if not new_interpreter or not new_interpreter.startswith(b'/') or \ not args: @@ -197,12 +194,15 @@ def parse_shebang(shebangline): return shebangline[start:] -def insert_flag_to_shebang(shebangline): - flags = parse_shebang(shebangline) - if flags: - flags = flags[2:] - - return b' -' + add_flag + flags +def populate_flags(shebangline): + old_flags = b'' + if keep_flags: + old_flags = parse_shebang(shebangline) + if old_flags: + old_flags = old_flags[2:] + if not (old_flags or add_flag): + return b'' + return b' -' + add_flag + old_flags def fixline(line): @@ -212,10 +212,8 @@ def fixline(line): if b"python" not in line: return line flags = b'' - if keep_flags: - flags = parse_shebang(line) - if add_flag: - flags = insert_flag_to_shebang(line) + if keep_flags or add_flag: + flags = populate_flags(line) return b'#! ' + new_interpreter + flags + b'\n' From 8af95b4fdcb76fbb1059dac195ebeeb377b06487 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Fri, 6 Sep 2019 18:16:06 +0200 Subject: [PATCH 03/11] rename variable add_flag to add_flags --- Tools/scripts/pathfix.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index 989c8f25b9f85ea..02fbc99f026bcac 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -40,7 +40,7 @@ preserve_timestamps = False create_backup = True keep_flags = False -add_flag = b'' +add_flags = b'' def main(): @@ -48,7 +48,7 @@ def main(): global preserve_timestamps global create_backup global keep_flags - global add_flag + global add_flags usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' % sys.argv[0]) @@ -68,7 +68,7 @@ def main(): if o == '-k': keep_flags = True if o == '-a': - add_flag = a.encode() + add_flags = a.encode() if not new_interpreter or not new_interpreter.startswith(b'/') or \ not args: @@ -200,9 +200,9 @@ def populate_flags(shebangline): old_flags = parse_shebang(shebangline) if old_flags: old_flags = old_flags[2:] - if not (old_flags or add_flag): + if not (old_flags or add_flags): return b'' - return b' -' + add_flag + old_flags + return b' -' + add_flags + old_flags def fixline(line): @@ -212,7 +212,7 @@ def fixline(line): if b"python" not in line: return line flags = b'' - if keep_flags or add_flag: + if keep_flags or add_flags: flags = populate_flags(line) return b'#! ' + new_interpreter + flags + b'\n' From a05ce9c19eb620279c620dcca5a96901b093a7b5 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Mon, 9 Sep 2019 16:58:48 +0200 Subject: [PATCH 04/11] adress Victor's comments --- Lib/test/test_tools/test_pathfix.py | 18 +++++++++--------- Tools/scripts/pathfix.py | 7 ++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index 908be1163e545de..96310bd464f8cfd 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -39,13 +39,13 @@ def test_pathfix(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', ]), + ['-i', '/usr/bin/python3']), '#! /usr/bin/python3', ) self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', - ['-i', '/usr/bin/python3', ]), + ['-i', '/usr/bin/python3']), '#! /usr/bin/python3', ) @@ -53,13 +53,13 @@ def test_pathfix_keeping_flags(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', - ['-i', '/usr/bin/python3', '-k', ]), + ['-i', '/usr/bin/python3', '-k']), '#! /usr/bin/python3 -R', ) self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', '-k', ]), + ['-i', '/usr/bin/python3', '-k']), '#! /usr/bin/python3', ) @@ -67,31 +67,31 @@ def test_pathfix_adding_flag(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', '-a', 's', ]), + ['-i', '/usr/bin/python3', '-a', 's']), '#! /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']), '#! /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']), '#! /usr/bin/python3 -vv', ) self.assertEqual( self.pathfix( '#! /usr/bin/env python', - ['-i', '/usr/bin/python3', '-a', 'Rs', ]), + ['-i', '/usr/bin/python3', '-a', 'Rs']), '#! /usr/bin/python3 -Rs' ) self.assertEqual( self.pathfix( '#! /usr/bin/env python -W something', - ['-i', '/usr/bin/python3', '-a', 's', '-k', ]), + ['-i', '/usr/bin/python3', '-a', 's', '-k']), '#! /usr/bin/python3 -sW something', ) diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index 02fbc99f026bcac..29a26738077de0f 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -202,6 +202,8 @@ def populate_flags(shebangline): old_flags = old_flags[2:] if not (old_flags or add_flags): return b'' + # On Linux, the entire string following the interpreter name + # is passed as a single argument to the interpreter. return b' -' + add_flags + old_flags @@ -211,9 +213,8 @@ def fixline(line): if b"python" not in line: return line - flags = b'' - if keep_flags or add_flags: - flags = populate_flags(line) + + flags = populate_flags(line) return b'#! ' + new_interpreter + flags + b'\n' From 0785830942f5ab45b9cde57737cb750387d21dd9 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Tue, 10 Sep 2019 16:00:12 +0200 Subject: [PATCH 05/11] add error when passing space to -a --- Lib/test/test_tools/test_pathfix.py | 37 +++++++++++++---------------- Tools/scripts/pathfix.py | 4 +++- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index 96310bd464f8cfd..e516356379989e1 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -40,60 +40,57 @@ def test_pathfix(self): self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3']), - '#! /usr/bin/python3', - ) + '#! /usr/bin/python3',) self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', ['-i', '/usr/bin/python3']), - '#! /usr/bin/python3', - ) + '#! /usr/bin/python3',) def test_pathfix_keeping_flags(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', ['-i', '/usr/bin/python3', '-k']), - '#! /usr/bin/python3 -R', - ) + '#! /usr/bin/python3 -R',) self.assertEqual( self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3', '-k']), - '#! /usr/bin/python3', - ) + '#! /usr/bin/python3',) def test_pathfix_adding_flag(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3', '-a', 's']), - '#! /usr/bin/python3 -s', - ) + '#! /usr/bin/python3 -s',) self.assertEqual( self.pathfix( - '#! /usr/bin/env python -s', + '#! /usr/bin/env python -S', ['-i', '/usr/bin/python3', '-a', 's']), - '#! /usr/bin/python3 -s', - ) + '#! /usr/bin/python3 -s',) self.assertEqual( self.pathfix( - '#! /usr/bin/env python -v', + '#! /usr/bin/env python -V', ['-i', '/usr/bin/python3', '-a', 'v', '-k']), - '#! /usr/bin/python3 -vv', - ) + '#! /usr/bin/python3 -vV',) self.assertEqual( self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3', '-a', 'Rs']), - '#! /usr/bin/python3 -Rs' - ) + '#! /usr/bin/python3 -Rs') self.assertEqual( self.pathfix( '#! /usr/bin/env python -W something', ['-i', '/usr/bin/python3', '-a', 's', '-k']), - '#! /usr/bin/python3 -sW something', - ) + '#! /usr/bin/python3 -sW something',) + self.assertEqual( + self.pathfix( + '#! /usr/bin/env python -W something', + ['-i', '/usr/bin/python3', '-a', ' af', '-k'], + expected_returncode=2), + '#! /usr/bin/env python -W something',) if __name__ == '__main__': diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index 29a26738077de0f..17ab7f2b881c56b 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -69,7 +69,9 @@ def main(): keep_flags = True if o == '-a': add_flags = a.encode() - + if ' ' in a: + err('Does not support arguments') + sys.exit(2) if not new_interpreter or not new_interpreter.startswith(b'/') or \ not args: err('-i option or file-or-directory missing\n') From 01dd5b9872649770f56a0ed08a6ba4d32aa4f633 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Thu, 12 Sep 2019 16:16:42 +0200 Subject: [PATCH 06/11] add examples to comment --- Lib/test/test_tools/test_pathfix.py | 18 +++++++++--------- Tools/scripts/pathfix.py | 7 ++++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index e516356379989e1..b7a40a6dbf83f59 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -40,41 +40,41 @@ def test_pathfix(self): self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3']), - '#! /usr/bin/python3',) + '#! /usr/bin/python3') self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', ['-i', '/usr/bin/python3']), - '#! /usr/bin/python3',) + '#! /usr/bin/python3') def test_pathfix_keeping_flags(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python -R', ['-i', '/usr/bin/python3', '-k']), - '#! /usr/bin/python3 -R',) + '#! /usr/bin/python3 -R') self.assertEqual( self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3', '-k']), - '#! /usr/bin/python3',) + '#! /usr/bin/python3') def test_pathfix_adding_flag(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python', ['-i', '/usr/bin/python3', '-a', 's']), - '#! /usr/bin/python3 -s',) + '#! /usr/bin/python3 -s') self.assertEqual( self.pathfix( '#! /usr/bin/env python -S', ['-i', '/usr/bin/python3', '-a', 's']), - '#! /usr/bin/python3 -s',) + '#! /usr/bin/python3 -s') self.assertEqual( self.pathfix( '#! /usr/bin/env python -V', ['-i', '/usr/bin/python3', '-a', 'v', '-k']), - '#! /usr/bin/python3 -vV',) + '#! /usr/bin/python3 -vV') self.assertEqual( self.pathfix( '#! /usr/bin/env python', @@ -84,13 +84,13 @@ def test_pathfix_adding_flag(self): self.pathfix( '#! /usr/bin/env python -W something', ['-i', '/usr/bin/python3', '-a', 's', '-k']), - '#! /usr/bin/python3 -sW something',) + '#! /usr/bin/python3 -sW something') self.assertEqual( self.pathfix( '#! /usr/bin/env python -W something', ['-i', '/usr/bin/python3', '-a', ' af', '-k'], expected_returncode=2), - '#! /usr/bin/env python -W something',) + '#! /usr/bin/env python -W something') if __name__ == '__main__': diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index 17ab7f2b881c56b..a75277efab50802 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -69,7 +69,7 @@ def main(): keep_flags = True if o == '-a': add_flags = a.encode() - if ' ' in a: + if b' ' in add_flags: err('Does not support arguments') sys.exit(2) if not new_interpreter or not new_interpreter.startswith(b'/') or \ @@ -206,6 +206,11 @@ def populate_flags(shebangline): return b'' # On Linux, the entire string following the interpreter name # is passed as a single argument to the interpreter. + # e.g. #! /usr/bin/python3 -W Error -s == /usr/bin/python3 -W "Error -s" + # so shebang should have single '-' where flags are given and + # flag might need argument for that reasons adding new flags is + # between '-' and original flags + # e.g. #! /usr/bin/python3 -sW Error return b' -' + add_flags + old_flags From 9131ddf5512785a8ce51019cd651ced57a6f8287 Mon Sep 17 00:00:00 2001 From: PatrikKopkan Date: Tue, 10 Sep 2019 16:01:04 +0200 Subject: [PATCH 07/11] Apply suggestions from code review Co-Authored-By: Victor Stinner Co-Authored-By: Victor Stinner --- .../next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst | 4 ++-- Tools/scripts/pathfix.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst b/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst index 69b89c22f29b044..d1210e29532bc66 100644 --- a/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst +++ b/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst @@ -1,2 +1,2 @@ -Add flag -k to pathscript.py script: preserve shebang flags. -Add option -a to pathscript.py script: add single literal flag to preserved flags. \ No newline at end of file +Add option -k to pathscript.py script: preserve shebang flags. +Add option -a to pathscript.py script: add flags. diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index a75277efab50802..b6910ee047b9bc8 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -70,7 +70,7 @@ def main(): if o == '-a': add_flags = a.encode() if b' ' in add_flags: - err('Does not support arguments') + err("-a option doesn't support whitespaces") sys.exit(2) if not new_interpreter or not new_interpreter.startswith(b'/') or \ not args: From c3424691174e344f0ff18ea424ad850309f5d28c Mon Sep 17 00:00:00 2001 From: PatrikKopkan Date: Mon, 16 Sep 2019 14:35:42 +0200 Subject: [PATCH 08/11] Update Tools/scripts/pathfix.py Co-Authored-By: Victor Stinner --- Tools/scripts/pathfix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index b6910ee047b9bc8..237a3d96e9fab50 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -206,7 +206,7 @@ def populate_flags(shebangline): return b'' # On Linux, the entire string following the interpreter name # is passed as a single argument to the interpreter. - # e.g. #! /usr/bin/python3 -W Error -s == /usr/bin/python3 -W "Error -s" + # e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s" # so shebang should have single '-' where flags are given and # flag might need argument for that reasons adding new flags is # between '-' and original flags From 110112c9ceb0655fe29a5ab918a7e79d011b917f Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Thu, 19 Sep 2019 12:13:27 +0200 Subject: [PATCH 09/11] test checks stderr output --- Lib/test/test_tools/test_pathfix.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index b7a40a6dbf83f59..e233b360967286d 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -17,7 +17,7 @@ def setUp(self): self.temp_file = support.TESTFN self.addCleanup(support.unlink, support.TESTFN) - def pathfix(self, shebang, pathfix_flags, expected_returncode=0): + def pathfix(self, shebang, pathfix_flags, expected_returncode=0, stderr=''): with open(self.temp_file, 'w', encoding='utf8') as f: f.write(f'{shebang}\n' + 'print("Hello world")\n') @@ -25,7 +25,9 @@ def pathfix(self, shebang, pathfix_flags, expected_returncode=0): [sys.executable, self.script, *pathfix_flags, '-n', self.temp_file], capture_output=True) + self.assertEqual(proc.returncode, expected_returncode, proc) + self.assertEqual(proc.stderr.decode(), stderr, proc) with open(self.temp_file, 'r', encoding='utf8') as f: output = f.read() @@ -85,11 +87,14 @@ def test_pathfix_adding_flag(self): '#! /usr/bin/env python -W something', ['-i', '/usr/bin/python3', '-a', 's', '-k']), '#! /usr/bin/python3 -sW something') + + def test_pathfix_adding_whitespaces(self): self.assertEqual( self.pathfix( '#! /usr/bin/env python -W something', ['-i', '/usr/bin/python3', '-a', ' af', '-k'], - expected_returncode=2), + expected_returncode=2, + stderr="-a option doesn't support whitespaces"), '#! /usr/bin/env python -W something') From 902b4016ec71fa5842095247e30586c5d9e3f607 Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Thu, 19 Sep 2019 21:49:13 +0200 Subject: [PATCH 10/11] adress Victor's comments --- Lib/test/test_tools/test_pathfix.py | 33 +++++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index e233b360967286d..9fdf1949c9d9739 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -17,25 +17,32 @@ def setUp(self): self.temp_file = support.TESTFN self.addCleanup(support.unlink, support.TESTFN) - def pathfix(self, shebang, pathfix_flags, expected_returncode=0, stderr=''): + 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], - capture_output=True) + capture_output=True, text=1) - self.assertEqual(proc.returncode, expected_returncode, proc) - self.assertEqual(proc.stderr.decode(), stderr, proc) + if stdout == '' and proc.returncode == 0: + stdout = f'{self.temp_file}: 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: output = f.read() lines = output.split('\n') self.assertEqual(lines[1:], ['print("Hello world")', '']) - shebang = lines[0] - return shebang + new_shebang = lines[0] + + if proc.returncode != 0: + self.assertEqual(shebang, new_shebang) + + return new_shebang def test_pathfix(self): self.assertEqual( @@ -88,14 +95,12 @@ def test_pathfix_adding_flag(self): ['-i', '/usr/bin/python3', '-a', 's', '-k']), '#! /usr/bin/python3 -sW something') - def test_pathfix_adding_whitespaces(self): - self.assertEqual( - self.pathfix( - '#! /usr/bin/env python -W something', - ['-i', '/usr/bin/python3', '-a', ' af', '-k'], - expected_returncode=2, - stderr="-a option doesn't support whitespaces"), - '#! /usr/bin/env python -W something') + def test_pathfix_adding_errors(self): + self.pathfix( + '#! /usr/bin/env python -W something', + ['-i', '/usr/bin/python3', '-a', ' af', '-k'], + exitcode=2, + stderr="-a option doesn't support whitespaces") if __name__ == '__main__': From 0ac33c1edc7f1940e1ec0cfcb3837fa1456642ec Mon Sep 17 00:00:00 2001 From: Patrik Kopkan Date: Wed, 25 Sep 2019 13:51:21 +0200 Subject: [PATCH 11/11] test authentic flags --- Lib/test/test_tools/test_pathfix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py index 9fdf1949c9d9739..a879924913445c1 100644 --- a/Lib/test/test_tools/test_pathfix.py +++ b/Lib/test/test_tools/test_pathfix.py @@ -91,14 +91,14 @@ def test_pathfix_adding_flag(self): '#! /usr/bin/python3 -Rs') self.assertEqual( self.pathfix( - '#! /usr/bin/env python -W something', + '#! /usr/bin/env python -W default', ['-i', '/usr/bin/python3', '-a', 's', '-k']), - '#! /usr/bin/python3 -sW something') + '#! /usr/bin/python3 -sW default') def test_pathfix_adding_errors(self): self.pathfix( - '#! /usr/bin/env python -W something', - ['-i', '/usr/bin/python3', '-a', ' af', '-k'], + '#! /usr/bin/env python -E', + ['-i', '/usr/bin/python3', '-a', 'W default', '-k'], exitcode=2, stderr="-a option doesn't support whitespaces")