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