X-Git-Url: http://git.pld-linux.org/?a=blobdiff_plain;f=check-shebang.py;h=0ea9ca0ff85736c90b3e783587b5e36fb32c9ba4;hb=3e5d0ee427278c4c7de0bb065ccddaa3f676ec5a;hp=7e02e72fabb7474ddb9bcb46babbca9de0ccc874;hpb=85308cf2f2a71ad86ffb62a07029c9a338fee056;p=packages%2Frpm-build-tools.git diff --git a/check-shebang.py b/check-shebang.py index 7e02e72..0ea9ca0 100755 --- a/check-shebang.py +++ b/check-shebang.py @@ -18,6 +18,9 @@ rep = { 'python2': [], 'python3': [], 'perl': [], + 'ruby': [], + 'bash': [], + 'awk': [], } skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"] @@ -38,7 +41,10 @@ if args.buildroot: if fext in skip_files: continue fpath = os.path.join(root, name) - rpm_build_root_files.append(hash(fpath)) + try: + rpm_build_root_files.append(hash(fpath)) + except FileNotFoundError: + pass print("%s: Caching done." % (sys.argv[0])) for root, dirs, files in os.walk(args.sourcedir): @@ -48,22 +54,31 @@ for root, dirs, files in os.walk(args.sourcedir): continue fpath = os.path.join(root, name) - with open(fpath, 'rt', encoding='utf-8', errors='replace') as f: - try: - fline = f.read(128) - f = io.StringIO(fline) - shebang = f.readline() - except UnicodeDecodeError as e: - print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr) - continue - if re.compile(r'^#!\s*/usr/bin/env python\s').match(shebang) \ - or re.compile(r'^#!\s*/usr/bin/env\s+python2\s').match(shebang) \ - or re.compile(r'^#!\s*/usr/bin/python\s').match(shebang): - rep['python2'].append(fpath) - elif re.compile(r'^#!\s*/usr/bin/env\s+python3\s').match(shebang): - rep['python3'].append(fpath) - elif re.compile(r'^#!\s*/usr/bin/env\s+perl\s').match(shebang): - rep['perl'].append(fpath) + try: + with open(fpath, 'rt', encoding='utf-8', errors='replace') as f: + try: + fline = f.read(128) + f = io.StringIO(fline) + shebang = f.readline() + except UnicodeDecodeError as e: + print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr) + continue + if re.compile(r'^#!\s*/usr/bin/env python\s').match(shebang) \ + or re.compile(r'^#!\s*/usr/bin/env\s+python2\s').match(shebang) \ + or re.compile(r'^#!\s*/usr/bin/python\s').match(shebang): + rep['python2'].append(fpath) + elif re.compile(r'^#!\s*/usr/bin/env\s+python3\s').match(shebang): + rep['python3'].append(fpath) + elif re.compile(r'^#!\s*/usr/bin/env\s+perl\s').match(shebang): + rep['perl'].append(fpath) + elif re.compile(r'^#!\s*/usr/bin/env\s+ruby\s').match(shebang): + rep['ruby'].append(fpath) + elif re.compile(r'^#!\s*/usr/bin/env\s+bash\s').match(shebang): + rep['bash'].append(fpath) + elif re.compile(r'^#!\s*/usr/bin/env\s+awk\s').match(shebang): + rep['awk'].append(fpath) + except FileNotFoundError: + pass def gf(cmd, files): newfiles = [] @@ -75,16 +90,19 @@ def gf(cmd, files): return print(cmd) for i in range(0, len(newfiles) - 1): - print("\t%s \\\n" % os.path.relpath(newfiles[i], start=args.sourcedir), end='') - print("\t%s\n" % os.path.relpath(newfiles[len(newfiles) - 1], start=args.sourcedir)) + print(" %s \\\n" % os.path.relpath(newfiles[i], start=args.sourcedir), end='') + print(" %s\n" % os.path.relpath(newfiles[len(newfiles) - 1], start=args.sourcedir)) print("\n# Copy from here:") print("# %s " % sys.argv[0], end='') if args.buildroot: - print("--root=%s " % args.buildroot, end='') + print("--buildroot=%s " % args.buildroot, end='') print("%s\n" % args.sourcedir) -gf("sed -E -i -e '1s,#![[:space:]]*/usr/bin/env[[:space:]]+python2,#!%{__python},' -e '1s,#![[:space:]]*/usr/bin/env[[:space:]]+python,#!%{__python},' -e '1s,#![[:space:]]*/usr/bin/python,#!%{__python},' \\", +gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+python2(\s|$),#!%{__python}\\1,' -e '1s,#!\s*/usr/bin/env\s+python(\s|$),#!%{__python}\\1,' -e '1s,#!\s*/usr/bin/python(\s|$),#!%{__python}\\1,' \\", rep['python2']) -gf("sed -E -i -e '1s,#![[:space:]]*/usr/bin/env[[:space:]]+python3,#!%{__python3},' \\", rep['python3']) -gf("sed -E -i -e '1s,#![[:space:]]*/usr/bin/env[[:space:]]+perl,#!%{__perl},' \\", rep['perl']) +gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+python3(\s|$),#!%{__python3}\\1,' \\", rep['python3']) +gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+perl(\s|$),#!%{__perl}\\1,' \\", rep['perl']) +gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+ruby(\s|$),#!%{__ruby}\\1,' \\", rep['ruby']) +gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+bash(\s|$),#!/bin/bash\\1,' \\", rep['bash']) +gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+awk(\s|$),#!/bin/awk\\1,' \\", rep['awk'])