Blame SOURCES/00356-k_and_a_options_for_pathfix.patch

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