3 # thisscript.py --buildroot=~/tmp/somepackage ~/rpm/BUILD/somepackage/
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()
26 skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"]
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()
35 rpm_build_root_files = []
37 print("%s: Caching `%s' files..." % (sys.argv[0], args.buildroot))
38 for root, dirs, files in os.walk(args.buildroot):
40 fname, fext = os.path.splitext(name)
41 if fext in skip_files:
43 fpath = os.path.join(root, name)
45 rpm_build_root_files.append(hash(fpath))
46 except FileNotFoundError:
48 print("%s: Caching done." % (sys.argv[0]))
50 for root, dirs, files in os.walk(args.sourcedir):
52 fname, fext = os.path.splitext(name)
53 if fext in skip_files:
56 fpath = os.path.join(root, name)
58 with open(fpath, 'rt', encoding='utf-8', errors='replace') as f:
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)
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:
86 if not rpm_build_root_files or hash(f) in rpm_build_root_files:
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))
96 print("\n# Copy from here:")
97 print("# %s " % sys.argv[0], end='')
99 print("--buildroot=%s " % args.buildroot, end='')
100 print("%s\n" % args.sourcedir)
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,' \\",
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'])