diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-09-25 13:22:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-25 13:22:40 (GMT) |
commit | c71c54c62600fd721baed3c96709e3d6e9c33817 (patch) | |
tree | 034a81ed72ffb1bafc09d24b98d176b53c2d57b2 /Tools/scripts | |
parent | 16cec136b75daf438080a5b6685d2679dfa406af (diff) | |
download | cpython-c71c54c62600fd721baed3c96709e3d6e9c33817.zip cpython-c71c54c62600fd721baed3c96709e3d6e9c33817.tar.gz cpython-c71c54c62600fd721baed3c96709e3d6e9c33817.tar.bz2 |
bpo-37064: Add -k and -a options to pathfix.py tool (GH-16387)
* bpo-37064: Add option -k to Tools/scripts/pathfix.py (GH-15548)
Add flag -k to pathscript.py script: preserve shebang flags.
(cherry picked from commit 50254ac4c179cb412e90682098c97db786143929)
* bpo-37064: Add option -a to pathfix.py tool (GH-15717)
Add option -a to Tools/Scripts/pathfix.py script: add flags.
(cherry picked from commit 1dc1acbd73f05f14c974b7ce1041787d7abef31e)
Diffstat (limited to 'Tools/scripts')
-rwxr-xr-x | Tools/scripts/pathfix.py | 64 |
1 files changed, 59 insertions, 5 deletions
diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index 1a0cf1c..237a3d9 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Change the #! line occurring in Python scripts. The new interpreter +# Change the #! line (shebang) occurring in Python scripts. The new interpreter # pathname must be given with a -i option. # # Command line arguments are files or directories to be processed. @@ -10,7 +10,13 @@ # arguments). # The original file is kept as a back-up (with a "~" attached to its name), # -n flag can be used to disable this. -# + +# 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 # a nice example of Python code that recurses down a directory tree # and uses regular expressions. Also note several subtleties like @@ -33,16 +39,21 @@ rep = sys.stdout.write new_interpreter = None preserve_timestamps = False create_backup = True +keep_flags = False +add_flags = b'' def main(): global new_interpreter global preserve_timestamps global create_backup - usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' % + global keep_flags + global add_flags + + 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:pn') + opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn') except getopt.error as msg: err(str(msg) + '\n') err(usage) @@ -54,6 +65,13 @@ def main(): preserve_timestamps = True if o == '-n': create_backup = False + if o == '-k': + keep_flags = True + if o == '-a': + add_flags = a.encode() + if b' ' in add_flags: + err("-a option doesn't support whitespaces") + 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') @@ -70,10 +88,14 @@ def main(): if fix(arg): bad = 1 sys.exit(bad) + ispythonprog = re.compile(r'^[a-zA-Z0-9_]+\.py$') + + def ispython(name): return bool(ispythonprog.match(name)) + def recursedown(dirname): dbg('recursedown(%r)\n' % (dirname,)) bad = 0 @@ -96,6 +118,7 @@ def recursedown(dirname): if recursedown(fullname): bad = 1 return bad + def fix(filename): ## dbg('fix(%r)\n' % (filename,)) try: @@ -164,12 +187,43 @@ def fix(filename): # Return success return 0 + +def parse_shebang(shebangline): + shebangline = shebangline.rstrip(b'\n') + start = shebangline.find(b' -') + if start == -1: + return b'' + return shebangline[start:] + + +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_flags): + 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" 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 + # e.g. #! /usr/bin/python3 -sW Error + return b' -' + add_flags + old_flags + + def fixline(line): if not line.startswith(b'#!'): return line + if b"python" not in line: return line - return b'#! ' + new_interpreter + b'\n' + + flags = populate_flags(line) + return b'#! ' + new_interpreter + flags + b'\n' + if __name__ == '__main__': main() |