]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - check-shebang.py
- shorter rules and don't replace twice
[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 }
22
23 skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"]
24
25 def 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
32 rpm_build_root_files = []
33 if args.buildroot:
34     print("%s: Caching `%s' files..." % (sys.argv[0], args.buildroot))
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
40             fpath = os.path.join(root, name)
41             rpm_build_root_files.append(hash(fpath))
42     print("%s: Caching done." % (sys.argv[0]))
43
44 for root, dirs, files in os.walk(args.sourcedir):
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
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):
62                 rep['python2'].append(fpath)
63             elif re.compile(r'^#!\s*/usr/bin/env\s+python3\s').match(shebang):
64                 rep['python3'].append(fpath)
65             elif re.compile(r'^#!\s*/usr/bin/env\s+perl\s').match(shebang):
66                 rep['perl'].append(fpath)
67
68 def gf(cmd, files):
69     newfiles = []
70     for f in files:
71         if not rpm_build_root_files or hash(f) in rpm_build_root_files:
72             newfiles.append(f)
73     newfiles.sort()
74     if not newfiles:
75         return
76     print(cmd)
77     for i in range(0, len(newfiles) - 1):
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))
80
81 print("\n# Copy from here:")
82 print("# %s " % sys.argv[0], end='')
83 if args.buildroot:
84     print("--buildroot=%s " % args.buildroot, end='')
85 print("%s\n" % args.sourcedir)
86
87 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,' \\",
88    rep['python2'])
89 gf("sed -E -i -e '1s,#!\s*/usr/bin/env\s+python3(\s|$),#!%{__python3}\\1,' \\", rep['python3'])
90 gf("sed -E -i -e '1s,#!\s*/usr/bin/env\s+perl(\s|$),#!%{__perl}\\1,' \\", rep['perl'])
This page took 0.047552 seconds and 4 git commands to generate.