]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - check-shebang.py
- shorter rules and don't replace twice
[packages/rpm-build-tools.git] / check-shebang.py
CommitLineData
ba239351
AM
1#!/usr/bin/python3
2
178671a1 3# thisscript.py --buildroot=~/tmp/somepackage ~/rpm/BUILD/somepackage/
b7387090
AM
4
5import argparse
ba289b0b 6import hashlib
ba239351
AM
7import io
8import os
9import re
10import sys
11
b7387090
AM
12parser = argparse.ArgumentParser()
13parser.add_argument('sourcedir', help='RPM_SOURCE_DIR directory')
14parser.add_argument("--buildroot", help="RPM_BUILD_ROOT directory")
15args = parser.parse_args()
16
ba239351
AM
17rep = {
18 'python2': [],
19 'python3': [],
20 'perl': [],
21}
22
b7387090
AM
23skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"]
24
ba289b0b
AM
25def hash(fname):
26 hash_alg = hashlib.sha256()
27 with open(fname, "rb") as f:
28 for chunk in iter(lambda: f.read(4096), b""):
29 hash_alg.update(chunk)
30 return hash_alg.hexdigest()
31
32rpm_build_root_files = []
b7387090
AM
33if args.buildroot:
34 print("%s: Caching `%s' files..." % (sys.argv[0], args.buildroot))
b7387090
AM
35 for root, dirs, files in os.walk(args.buildroot):
36 for name in files:
37 fname, fext = os.path.splitext(name)
38 if fext in skip_files:
39 continue
ba289b0b
AM
40 fpath = os.path.join(root, name)
41 rpm_build_root_files.append(hash(fpath))
b7387090 42 print("%s: Caching done." % (sys.argv[0]))
ba239351 43
b7387090 44for root, dirs, files in os.walk(args.sourcedir):
ba239351
AM
45 for name in files:
46 fname, fext = os.path.splitext(name)
47 if fext in skip_files:
48 continue
49
50 fpath = os.path.join(root, name)
51 with open(fpath, 'rt', encoding='utf-8', errors='replace') as f:
52 try:
53 fline = f.read(128)
54 f = io.StringIO(fline)
55 shebang = f.readline()
56 except UnicodeDecodeError as e:
57 print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr)
58 continue
0b6e4a06
AM
59 if re.compile(r'^#!\s*/usr/bin/env python\s').match(shebang) \
60 or re.compile(r'^#!\s*/usr/bin/env\s+python2\s').match(shebang) \
61 or re.compile(r'^#!\s*/usr/bin/python\s').match(shebang):
ba239351 62 rep['python2'].append(fpath)
0b6e4a06 63 elif re.compile(r'^#!\s*/usr/bin/env\s+python3\s').match(shebang):
ba239351 64 rep['python3'].append(fpath)
0b6e4a06 65 elif re.compile(r'^#!\s*/usr/bin/env\s+perl\s').match(shebang):
ba239351
AM
66 rep['perl'].append(fpath)
67
ba289b0b 68def gf(cmd, files):
b7387090
AM
69 newfiles = []
70 for f in files:
ba289b0b 71 if not rpm_build_root_files or hash(f) in rpm_build_root_files:
b7387090
AM
72 newfiles.append(f)
73 newfiles.sort()
ba289b0b
AM
74 if not newfiles:
75 return
76 print(cmd)
b7387090 77 for i in range(0, len(newfiles) - 1):
2c47d05a
AM
78 print(" %s \\\n" % os.path.relpath(newfiles[i], start=args.sourcedir), end='')
79 print(" %s\n" % os.path.relpath(newfiles[len(newfiles) - 1], start=args.sourcedir))
ba239351 80
178671a1 81print("\n# Copy from here:")
b7387090
AM
82print("# %s " % sys.argv[0], end='')
83if args.buildroot:
2c47d05a 84 print("--buildroot=%s " % args.buildroot, end='')
b7387090 85print("%s\n" % args.sourcedir)
ba239351 86
2c47d05a 87gf("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,' \\",
ba289b0b 88 rep['python2'])
2c47d05a
AM
89gf("sed -E -i -e '1s,#!\s*/usr/bin/env\s+python3(\s|$),#!%{__python3}\\1,' \\", rep['python3'])
90gf("sed -E -i -e '1s,#!\s*/usr/bin/env\s+perl(\s|$),#!%{__perl}\\1,' \\", rep['perl'])
This page took 0.083937 seconds and 4 git commands to generate.