Created
August 7, 2021 06:05
-
-
Save JonathonReinhart/a3888bedfb9de4ba03af7d243a18306f to your computer and use it in GitHub Desktop.
Find Python scripts which use an old Python shebang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
from pathlib import Path | |
import sys | |
bad_shebangs = { | |
'/usr/bin/python', | |
'/usr/bin/env python', | |
} | |
def color(s, c): | |
if not color.use: | |
return s | |
return f"\033[{c}m{s}\033[m" | |
color.use = sys.stdout.isatty() | |
def red(s): | |
return color(s, 31) | |
def blue(s): | |
return color(s, 34) | |
def process_file(path): | |
try: | |
f = path.open('rb') | |
except OSError: | |
return | |
with f: | |
head = f.readline(80) | |
if not (head.startswith(b'#!') and head.endswith(b'\n')): | |
return | |
shebang = head[2:-1].decode().strip() | |
if shebang in bad_shebangs: | |
print(f"{blue(path)}: {red(shebang)}") | |
def process_dir(toppath): | |
for dirpath, dirnames, filenames in os.walk(toppath): | |
dirpath = Path(dirpath) | |
for filename in filenames: | |
path = dirpath / filename | |
process_file(path) | |
def parse_args(): | |
import argparse | |
ap = argparse.ArgumentParser() | |
ap.add_argument('dir', default='.') | |
return ap.parse_args() | |
def main(): | |
args = parse_args() | |
process_dir(args.dir) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment