Blame SOURCES/00356-k_and_a_options_for_pathfix.patch

71f264
From 0cfd9a7f26488567b9a3e5ec192099a8b80ad9df Mon Sep 17 00:00:00 2001
71f264
From: Lumir Balhar <lbalhar@redhat.com>
71f264
Date: Tue, 19 Jan 2021 07:55:37 +0100
71f264
Subject: [PATCH] [PATCH] bpo-37064: Add -k and -a options to pathfix.py tool
71f264
 (GH-16387)
71f264
71f264
* bpo-37064: Add option -k to Tools/scripts/pathfix.py (GH-15548)
71f264
71f264
Add flag -k to pathscript.py script: preserve shebang flags.
71f264
71f264
(cherry picked from commit 50254ac4c179cb412e90682098c97db786143929)
71f264
71f264
* bpo-37064: Add option -a to pathfix.py tool (GH-15717)
71f264
71f264
Add option -a to Tools/Scripts/pathfix.py script: add flags.
71f264
71f264
(cherry picked from commit 1dc1acbd73f05f14c974b7ce1041787d7abef31e)
71f264
---
71f264
 Lib/test/test_tools/test_pathfix.py | 104 ++++++++++++++++++++++++++++
71f264
 Tools/scripts/pathfix.py            |  64 +++++++++++++++--
71f264
 2 files changed, 163 insertions(+), 5 deletions(-)
71f264
 create mode 100644 Lib/test/test_tools/test_pathfix.py
71f264
71f264
diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py
71f264
new file mode 100644
71f264
index 0000000..1f0585e
71f264
--- /dev/null
71f264
+++ b/Lib/test/test_tools/test_pathfix.py
71f264
@@ -0,0 +1,104 @@
71f264
+import os
71f264
+import subprocess
71f264
+import sys
71f264
+import unittest
71f264
+from test import support
71f264
+from test.test_tools import import_tool, scriptsdir
71f264
+
71f264
+
71f264
+class TestPathfixFunctional(unittest.TestCase):
71f264
+    script = os.path.join(scriptsdir, 'pathfix.py')
71f264
+
71f264
+    def setUp(self):
71f264
+        self.temp_file = support.TESTFN
71f264
+        self.addCleanup(support.unlink, support.TESTFN)
71f264
+
71f264
+    def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr=''):
71f264
+        with open(self.temp_file, 'w', encoding='utf8') as f:
71f264
+            f.write(f'{shebang}\n' + 'print("Hello world")\n')
71f264
+
71f264
+        proc = subprocess.run(
71f264
+            [sys.executable, self.script,
71f264
+             *pathfix_flags, '-n', self.temp_file],
71f264
+            universal_newlines=True, stdout=subprocess.PIPE,
71f264
+            stderr=subprocess.PIPE)
71f264
+
71f264
+        if stdout == '' and proc.returncode == 0:
71f264
+            stdout = f'{self.temp_file}: updating\n'
71f264
+        self.assertEqual(proc.returncode, exitcode, proc)
71f264
+        self.assertEqual(proc.stdout, stdout, proc)
71f264
+        self.assertEqual(proc.stderr, stderr, proc)
71f264
+
71f264
+        with open(self.temp_file, 'r', encoding='utf8') as f:
71f264
+            output = f.read()
71f264
+
71f264
+        lines = output.split('\n')
71f264
+        self.assertEqual(lines[1:], ['print("Hello world")', ''])
71f264
+        new_shebang = lines[0]
71f264
+
71f264
+        if proc.returncode != 0:
71f264
+            self.assertEqual(shebang, new_shebang)
71f264
+
71f264
+        return new_shebang
71f264
+
71f264
+    def test_pathfix(self):
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python',
71f264
+                ['-i', '/usr/bin/python3']),
71f264
+            '#! /usr/bin/python3')
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python -R',
71f264
+                ['-i', '/usr/bin/python3']),
71f264
+            '#! /usr/bin/python3')
71f264
+
71f264
+    def test_pathfix_keeping_flags(self):
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python -R',
71f264
+                ['-i', '/usr/bin/python3', '-k']),
71f264
+            '#! /usr/bin/python3 -R')
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python',
71f264
+                ['-i', '/usr/bin/python3', '-k']),
71f264
+            '#! /usr/bin/python3')
71f264
+
71f264
+    def test_pathfix_adding_flag(self):
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python',
71f264
+                ['-i', '/usr/bin/python3', '-a', 's']),
71f264
+            '#! /usr/bin/python3 -s')
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python -S',
71f264
+                ['-i', '/usr/bin/python3', '-a', 's']),
71f264
+            '#! /usr/bin/python3 -s')
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python -V',
71f264
+                ['-i', '/usr/bin/python3', '-a', 'v', '-k']),
71f264
+            '#! /usr/bin/python3 -vV')
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python',
71f264
+                ['-i', '/usr/bin/python3', '-a', 'Rs']),
71f264
+            '#! /usr/bin/python3 -Rs')
71f264
+        self.assertEqual(
71f264
+            self.pathfix(
71f264
+                '#! /usr/bin/env python -W default',
71f264
+                ['-i', '/usr/bin/python3', '-a', 's', '-k']),
71f264
+            '#! /usr/bin/python3 -sW default')
71f264
+
71f264
+    def test_pathfix_adding_errors(self):
71f264
+        self.pathfix(
71f264
+            '#! /usr/bin/env python -E',
71f264
+            ['-i', '/usr/bin/python3', '-a', 'W default', '-k'],
71f264
+            exitcode=2,
71f264
+            stderr="-a option doesn't support whitespaces")
71f264
+
71f264
+
71f264
+if __name__ == '__main__':
71f264
+    unittest.main()
71f264
diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py
71f264
index c5bf984..2dfa6e8 100755
71f264
--- a/Tools/scripts/pathfix.py
71f264
+++ b/Tools/scripts/pathfix.py
71f264
@@ -1,6 +1,6 @@
71f264
 #!/usr/bin/env python3
71f264
 
71f264
-# Change the #! line occurring in Python scripts.  The new interpreter
71f264
+# Change the #! line (shebang) occurring in Python scripts.  The new interpreter
71f264
 # pathname must be given with a -i option.
71f264
 #
71f264
 # Command line arguments are files or directories to be processed.
71f264
@@ -10,7 +10,13 @@
71f264
 # arguments).
71f264
 # The original file is kept as a back-up (with a "~" attached to its name),
71f264
 # -n flag can be used to disable this.
71f264
-#
71f264
+
71f264
+# Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`.
71f264
+# Normally, pathfix overwrites the entire line, including the flags.
71f264
+# To change interpreter and keep flags from the original shebang line, use -k.
71f264
+# If you want to keep flags and add to them one single literal flag, use option -a.
71f264
+
71f264
+
71f264
 # Undoubtedly you can do this using find and sed or perl, but this is
71f264
 # a nice example of Python code that recurses down a directory tree
71f264
 # and uses regular expressions.  Also note several subtleties like
71f264
@@ -33,16 +39,21 @@ rep = sys.stdout.write
71f264
 new_interpreter = None
71f264
 preserve_timestamps = False
71f264
 create_backup = True
71f264
+keep_flags = False
71f264
+add_flags = b''
71f264
 
71f264
 
71f264
 def main():
71f264
     global new_interpreter
71f264
     global preserve_timestamps
71f264
     global create_backup
71f264
-    usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' %
71f264
+    global keep_flags
71f264
+    global add_flags
71f264
+
71f264
+    usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' %
71f264
              sys.argv[0])
71f264
     try:
71f264
-        opts, args = getopt.getopt(sys.argv[1:], 'i:pn')
71f264
+        opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn')
71f264
     except getopt.error as msg:
71f264
         err(str(msg) + '\n')
71f264
         err(usage)
71f264
@@ -54,6 +65,13 @@ def main():
71f264
             preserve_timestamps = True
71f264
         if o == '-n':
71f264
             create_backup = False
71f264
+        if o == '-k':
71f264
+            keep_flags = True
71f264
+        if o == '-a':
71f264
+            add_flags = a.encode()
71f264
+            if b' ' in add_flags:
71f264
+                err("-a option doesn't support whitespaces")
71f264
+                sys.exit(2)
71f264
     if not new_interpreter or not new_interpreter.startswith(b'/') or \
71f264
            not args:
71f264
         err('-i option or file-or-directory missing\n')
71f264
@@ -70,10 +88,14 @@ def main():
71f264
             if fix(arg): bad = 1
71f264
     sys.exit(bad)
71f264
 
71f264
+
71f264
 ispythonprog = re.compile(r'^[a-zA-Z0-9_]+\.py$')
71f264
+
71f264
+
71f264
 def ispython(name):
71f264
     return bool(ispythonprog.match(name))
71f264
 
71f264
+
71f264
 def recursedown(dirname):
71f264
     dbg('recursedown(%r)\n' % (dirname,))
71f264
     bad = 0
71f264
@@ -96,6 +118,7 @@ def recursedown(dirname):
71f264
         if recursedown(fullname): bad = 1
71f264
     return bad
71f264
 
71f264
+
71f264
 def fix(filename):
71f264
 ##  dbg('fix(%r)\n' % (filename,))
71f264
     try:
71f264
@@ -166,12 +189,43 @@ def fix(filename):
71f264
     # Return success
71f264
     return 0
71f264
 
71f264
+
71f264
+def parse_shebang(shebangline):
71f264
+    shebangline = shebangline.rstrip(b'\n')
71f264
+    start = shebangline.find(b' -')
71f264
+    if start == -1:
71f264
+        return b''
71f264
+    return shebangline[start:]
71f264
+
71f264
+
71f264
+def populate_flags(shebangline):
71f264
+    old_flags = b''
71f264
+    if keep_flags:
71f264
+        old_flags = parse_shebang(shebangline)
71f264
+        if old_flags:
71f264
+            old_flags = old_flags[2:]
71f264
+    if not (old_flags or add_flags):
71f264
+        return b''
71f264
+    # On Linux, the entire string following the interpreter name
71f264
+    # is passed as a single argument to the interpreter.
71f264
+    # e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
71f264
+    # so shebang should have single '-' where flags are given and
71f264
+    # flag might need argument for that reasons adding new flags is
71f264
+    # between '-' and original flags
71f264
+    # e.g. #! /usr/bin/python3 -sW Error
71f264
+    return b' -' + add_flags + old_flags
71f264
+
71f264
+
71f264
 def fixline(line):
71f264
     if not line.startswith(b'#!'):
71f264
         return line
71f264
+
71f264
     if b"python" not in line:
71f264
         return line
71f264
-    return b'#! ' + new_interpreter + b'\n'
71f264
+
71f264
+    flags = populate_flags(line)
71f264
+    return b'#! ' + new_interpreter + flags + b'\n'
71f264
+
71f264
 
71f264
 if __name__ == '__main__':
71f264
     main()
71f264
-- 
71f264
2.29.2
71f264