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