bae5e50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
bae5e50
From: Patrik Kopkan <pkopkan@redhat.com>
bae5e50
Date: Tue, 28 Jul 2020 18:38:07 +0200
bae5e50
Subject: [PATCH] 00335: Backport pathfix change
9cf4138
bae5e50
Tools/scripts/pathfix.py backports
bae5e50
Add -k and -a command line options to preserve and add shebang flags
bae5e50
In upstream since 3.8: https://bugs.python.org/issue37064
bae5e50
Assume all .py files are Python scripts when working recursively:
bae5e50
In upstream since 3.8: https://bugs.python.org/issue38347
bae5e50
bae5e50
Co-authored-by: Victor Stinner <vstinner@redhat.com>
bae5e50
---
bae5e50
 Lib/test/test_tools/test_pathfix.py           | 129 ++++++++++++++++++
bae5e50
 .../2019-05-27-15-26-12.bpo-37064.k_SPW2.rst  |   2 +
bae5e50
 Tools/scripts/pathfix.py                      |  60 +++++++-
bae5e50
 3 files changed, 186 insertions(+), 5 deletions(-)
bae5e50
 create mode 100644 Lib/test/test_tools/test_pathfix.py
bae5e50
 create mode 100644 Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst
9cf4138
9cf4138
diff --git a/Lib/test/test_tools/test_pathfix.py b/Lib/test/test_tools/test_pathfix.py
9cf4138
new file mode 100644
bae5e50
index 0000000000..ec361178e6
9cf4138
--- /dev/null
9cf4138
+++ b/Lib/test/test_tools/test_pathfix.py
9cf4138
@@ -0,0 +1,129 @@
9cf4138
+import os
9cf4138
+import subprocess
9cf4138
+import sys
9cf4138
+import unittest
9cf4138
+from test import support
9cf4138
+from test.test_tools import import_tool, scriptsdir, skip_if_missing
9cf4138
+
9cf4138
+
9cf4138
+# need Tools/script/ directory: skip if run on Python installed on the system
9cf4138
+skip_if_missing()
9cf4138
+
9cf4138
+
9cf4138
+class TestPathfixFunctional(unittest.TestCase):
9cf4138
+    script = os.path.join(scriptsdir, 'pathfix.py')
9cf4138
+
9cf4138
+    def setUp(self):
9cf4138
+        self.addCleanup(support.unlink, support.TESTFN)
9cf4138
+
9cf4138
+    def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='',
9cf4138
+                directory=''):
9cf4138
+        if directory:
9cf4138
+            # bpo-38347: Test filename should contain lowercase, uppercase,
9cf4138
+            # "-", "_" and digits.
9cf4138
+            filename = os.path.join(directory, 'script-A_1.py')
9cf4138
+            pathfix_arg = directory
9cf4138
+        else:
9cf4138
+            filename = support.TESTFN
9cf4138
+            pathfix_arg = filename
9cf4138
+
9cf4138
+        with open(filename, 'w', encoding='utf8') as f:
9cf4138
+            f.write(f'{shebang}\n' + 'print("Hello world")\n')
9cf4138
+
9cf4138
+        proc = subprocess.run(
9cf4138
+            [sys.executable, self.script,
9cf4138
+             *pathfix_flags, '-n', pathfix_arg],
9cf4138
+            capture_output=True, text=1)
9cf4138
+
9cf4138
+        if stdout == '' and proc.returncode == 0:
9cf4138
+            stdout = f'{filename}: updating\n'
9cf4138
+        self.assertEqual(proc.returncode, exitcode, proc)
9cf4138
+        self.assertEqual(proc.stdout, stdout, proc)
9cf4138
+        self.assertEqual(proc.stderr, stderr, proc)
9cf4138
+
9cf4138
+        with open(filename, 'r', encoding='utf8') as f:
9cf4138
+            output = f.read()
9cf4138
+
9cf4138
+        lines = output.split('\n')
9cf4138
+        self.assertEqual(lines[1:], ['print("Hello world")', ''])
9cf4138
+        new_shebang = lines[0]
9cf4138
+
9cf4138
+        if proc.returncode != 0:
9cf4138
+            self.assertEqual(shebang, new_shebang)
9cf4138
+
9cf4138
+        return new_shebang
9cf4138
+
9cf4138
+    def test_recursive(self):
9cf4138
+        tmpdir = support.TESTFN + '.d'
9cf4138
+        self.addCleanup(support.rmtree, tmpdir)
9cf4138
+        os.mkdir(tmpdir)
9cf4138
+        expected_stderr = f"recursedown('{os.path.basename(tmpdir)}')\n"
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python',
9cf4138
+                ['-i', '/usr/bin/python3'],
9cf4138
+                directory=tmpdir,
9cf4138
+                stderr=expected_stderr),
9cf4138
+            '#! /usr/bin/python3')
9cf4138
+
9cf4138
+    def test_pathfix(self):
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python',
9cf4138
+                ['-i', '/usr/bin/python3']),
9cf4138
+            '#! /usr/bin/python3')
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python -R',
9cf4138
+                ['-i', '/usr/bin/python3']),
9cf4138
+            '#! /usr/bin/python3')
9cf4138
+
9cf4138
+    def test_pathfix_keeping_flags(self):
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python -R',
9cf4138
+                ['-i', '/usr/bin/python3', '-k']),
9cf4138
+            '#! /usr/bin/python3 -R')
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python',
9cf4138
+                ['-i', '/usr/bin/python3', '-k']),
9cf4138
+            '#! /usr/bin/python3')
9cf4138
+
9cf4138
+    def test_pathfix_adding_flag(self):
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python',
9cf4138
+                ['-i', '/usr/bin/python3', '-a', 's']),
9cf4138
+            '#! /usr/bin/python3 -s')
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python -S',
9cf4138
+                ['-i', '/usr/bin/python3', '-a', 's']),
9cf4138
+            '#! /usr/bin/python3 -s')
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python -V',
9cf4138
+                ['-i', '/usr/bin/python3', '-a', 'v', '-k']),
9cf4138
+            '#! /usr/bin/python3 -vV')
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python',
9cf4138
+                ['-i', '/usr/bin/python3', '-a', 'Rs']),
9cf4138
+            '#! /usr/bin/python3 -Rs')
9cf4138
+        self.assertEqual(
9cf4138
+            self.pathfix(
9cf4138
+                '#! /usr/bin/env python -W default',
9cf4138
+                ['-i', '/usr/bin/python3', '-a', 's', '-k']),
9cf4138
+            '#! /usr/bin/python3 -sW default')
9cf4138
+
9cf4138
+    def test_pathfix_adding_errors(self):
9cf4138
+        self.pathfix(
9cf4138
+            '#! /usr/bin/env python -E',
9cf4138
+            ['-i', '/usr/bin/python3', '-a', 'W default', '-k'],
9cf4138
+            exitcode=2,
9cf4138
+            stderr="-a option doesn't support whitespaces")
9cf4138
+
9cf4138
+
9cf4138
+if __name__ == '__main__':
9cf4138
+    unittest.main()
9cf4138
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
9cf4138
new file mode 100644
bae5e50
index 0000000000..d1210e2953
9cf4138
--- /dev/null
9cf4138
+++ b/Misc/NEWS.d/next/Tools-Demos/2019-05-27-15-26-12.bpo-37064.k_SPW2.rst
9cf4138
@@ -0,0 +1,2 @@
9cf4138
+Add option -k to pathscript.py script: preserve shebang flags.
9cf4138
+Add option -a to pathscript.py script: add flags.
9cf4138
diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py
bae5e50
index 28ee428a3a..127c2fe41e 100755
9cf4138
--- a/Tools/scripts/pathfix.py
9cf4138
+++ b/Tools/scripts/pathfix.py
9cf4138
@@ -1,6 +1,6 @@
9cf4138
 #!/usr/bin/env python3
9cf4138
 
9cf4138
-# Change the #! line occurring in Python scripts.  The new interpreter
9cf4138
+# Change the #! line (shebang) occurring in Python scripts.  The new interpreter
9cf4138
 # pathname must be given with a -i option.
9cf4138
 #
9cf4138
 # Command line arguments are files or directories to be processed.
9cf4138
@@ -10,7 +10,13 @@
9cf4138
 # arguments).
9cf4138
 # The original file is kept as a back-up (with a "~" attached to its name),
9cf4138
 # -n flag can be used to disable this.
9cf4138
-#
9cf4138
+
9cf4138
+# Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`.
9cf4138
+# Normally, pathfix overwrites the entire line, including the flags.
9cf4138
+# To change interpreter and keep flags from the original shebang line, use -k.
9cf4138
+# If you want to keep flags and add to them one single literal flag, use option -a.
9cf4138
+
9cf4138
+
9cf4138
 # Undoubtedly you can do this using find and sed or perl, but this is
9cf4138
 # a nice example of Python code that recurses down a directory tree
9cf4138
 # and uses regular expressions.  Also note several subtleties like
9cf4138
@@ -33,16 +39,21 @@ rep = sys.stdout.write
9cf4138
 new_interpreter = None
9cf4138
 preserve_timestamps = False
9cf4138
 create_backup = True
9cf4138
+keep_flags = False
9cf4138
+add_flags = b''
9cf4138
 
9cf4138
 
9cf4138
 def main():
9cf4138
     global new_interpreter
9cf4138
     global preserve_timestamps
9cf4138
     global create_backup
9cf4138
-    usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' %
9cf4138
+    global keep_flags
9cf4138
+    global add_flags
9cf4138
+
9cf4138
+    usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' %
9cf4138
              sys.argv[0])
9cf4138
     try:
9cf4138
-        opts, args = getopt.getopt(sys.argv[1:], 'i:pn')
9cf4138
+        opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn')
9cf4138
     except getopt.error as msg:
9cf4138
         err(str(msg) + '\n')
9cf4138
         err(usage)
9cf4138
@@ -54,6 +65,13 @@ def main():
9cf4138
             preserve_timestamps = True
9cf4138
         if o == '-n':
9cf4138
             create_backup = False
9cf4138
+        if o == '-k':
9cf4138
+            keep_flags = True
9cf4138
+        if o == '-a':
9cf4138
+            add_flags = a.encode()
9cf4138
+            if b' ' in add_flags:
9cf4138
+                err("-a option doesn't support whitespaces")
9cf4138
+                sys.exit(2)
9cf4138
     if not new_interpreter or not new_interpreter.startswith(b'/') or \
9cf4138
            not args:
9cf4138
         err('-i option or file-or-directory missing\n')
58c995a
@@ -96,6 +114,7 @@ def recursedown(dirname):
9cf4138
         if recursedown(fullname): bad = 1
9cf4138
     return bad
9cf4138
 
9cf4138
+
9cf4138
 def fix(filename):
9cf4138
 ##  dbg('fix(%r)\n' % (filename,))
9cf4138
     try:
58c995a
@@ -166,12 +185,43 @@ def fix(filename):
9cf4138
     # Return success
9cf4138
     return 0
9cf4138
 
9cf4138
+
9cf4138
+def parse_shebang(shebangline):
9cf4138
+    shebangline = shebangline.rstrip(b'\n')
9cf4138
+    start = shebangline.find(b' -')
9cf4138
+    if start == -1:
9cf4138
+        return b''
9cf4138
+    return shebangline[start:]
9cf4138
+
9cf4138
+
9cf4138
+def populate_flags(shebangline):
9cf4138
+    old_flags = b''
9cf4138
+    if keep_flags:
9cf4138
+        old_flags = parse_shebang(shebangline)
9cf4138
+        if old_flags:
9cf4138
+            old_flags = old_flags[2:]
9cf4138
+    if not (old_flags or add_flags):
9cf4138
+        return b''
9cf4138
+    # On Linux, the entire string following the interpreter name
9cf4138
+    # is passed as a single argument to the interpreter.
9cf4138
+    # e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
9cf4138
+    # so shebang should have single '-' where flags are given and
9cf4138
+    # flag might need argument for that reasons adding new flags is
9cf4138
+    # between '-' and original flags
9cf4138
+    # e.g. #! /usr/bin/python3 -sW Error
9cf4138
+    return b' -' + add_flags + old_flags
9cf4138
+
9cf4138
+
9cf4138
 def fixline(line):
9cf4138
     if not line.startswith(b'#!'):
9cf4138
         return line
9cf4138
+
9cf4138
     if b"python" not in line:
9cf4138
         return line
9cf4138
-    return b'#! ' + new_interpreter + b'\n'
9cf4138
+
9cf4138
+    flags = populate_flags(line)
9cf4138
+    return b'#! ' + new_interpreter + flags + b'\n'
9cf4138
+
9cf4138
 
9cf4138
 if __name__ == '__main__':
9cf4138
     main()