]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - check-shebang.py
Release 2 (by relup.sh)
[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     'bash': [],
23     'awk': [],
24 }
25
26 skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"]
27
28 def hash(fname):
29     hash_alg = hashlib.sha256()
30     with open(fname, "rb") as f:
31         for chunk in iter(lambda: f.read(4096), b""):
32             hash_alg.update(chunk)
33     return hash_alg.hexdigest()
34
35 rpm_build_root_files = []
36 if args.buildroot:
37     print("%s: Caching `%s' files..." % (sys.argv[0], args.buildroot))
38     for root, dirs, files in os.walk(args.buildroot):
39         for name in files:
40             fname, fext = os.path.splitext(name)
41             if fext in skip_files:
42                 continue
43             fpath = os.path.join(root, name)
44             try:
45                 rpm_build_root_files.append(hash(fpath))
46             except FileNotFoundError:
47                 pass
48     print("%s: Caching done." % (sys.argv[0]))
49
50 for root, dirs, files in os.walk(args.sourcedir):
51     for name in files:
52         fname, fext = os.path.splitext(name)
53         if fext in skip_files:
54             continue
55
56         fpath = os.path.join(root, name)
57         try:
58             with open(fpath, 'rt', encoding='utf-8', errors='replace') as f:
59                 try:
60                     fline = f.read(128)
61                     f = io.StringIO(fline)
62                     shebang = f.readline()
63                 except UnicodeDecodeError as e:
64                     print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr)
65                     continue
66                 if re.compile(r'^#!\s*/usr/bin/env python\s').match(shebang) \
67                         or re.compile(r'^#!\s*/usr/bin/env\s+python2\s').match(shebang) \
68                         or re.compile(r'^#!\s*/usr/bin/python\s').match(shebang):
69                     rep['python2'].append(fpath)
70                 elif re.compile(r'^#!\s*/usr/bin/env\s+python3\s').match(shebang):
71                     rep['python3'].append(fpath)
72                 elif re.compile(r'^#!\s*/usr/bin/env\s+perl\s').match(shebang):
73                     rep['perl'].append(fpath)
74                 elif re.compile(r'^#!\s*/usr/bin/env\s+ruby\s').match(shebang):
75                     rep['ruby'].append(fpath)
76                 elif re.compile(r'^#!\s*/usr/bin/env\s+bash\s').match(shebang):
77                     rep['bash'].append(fpath)
78                 elif re.compile(r'^#!\s*/usr/bin/env\s+awk\s').match(shebang):
79                     rep['awk'].append(fpath)
80         except FileNotFoundError:
81             pass
82
83 def gf(cmd, files):
84     newfiles = []
85     for f in files:
86         if not rpm_build_root_files or hash(f) in rpm_build_root_files:
87             newfiles.append(f)
88     newfiles.sort()
89     if not newfiles:
90         return
91     print(cmd)
92     for i in range(0, len(newfiles) - 1):
93         print("      %s \\\n" % os.path.relpath(newfiles[i], start=args.sourcedir), end='')
94     print("      %s\n" % os.path.relpath(newfiles[len(newfiles) - 1], start=args.sourcedir))
95
96 print("\n# Copy from here:")
97 print("# %s " % sys.argv[0], end='')
98 if args.buildroot:
99     print("--buildroot=%s " % args.buildroot, end='')
100 print("%s\n" % args.sourcedir)
101
102 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,' \\",
103    rep['python2'])
104 gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+python3(\s|$),#!%{__python3}\\1,' \\", rep['python3'])
105 gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+perl(\s|$),#!%{__perl}\\1,' \\", rep['perl'])
106 gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+ruby(\s|$),#!%{__ruby}\\1,' \\", rep['ruby'])
107 gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+bash(\s|$),#!/bin/bash\\1,' \\", rep['bash'])
108 gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+awk(\s|$),#!/bin/awk\\1,' \\", rep['awk'])
This page took 0.037042 seconds and 3 git commands to generate.