]>
Commit | Line | Data |
---|---|---|
ba239351 AM |
1 | #!/usr/bin/python3 |
2 | ||
178671a1 | 3 | # thisscript.py --buildroot=~/tmp/somepackage ~/rpm/BUILD/somepackage/ |
b7387090 AM |
4 | |
5 | import argparse | |
ba289b0b | 6 | import hashlib |
ba239351 AM |
7 | import io |
8 | import os | |
9 | import re | |
10 | import sys | |
11 | ||
b7387090 AM |
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 | ||
ba239351 AM |
17 | rep = { |
18 | 'python2': [], | |
19 | 'python3': [], | |
20 | 'perl': [], | |
2d40e6ee | 21 | 'ruby': [], |
ba239351 AM |
22 | } |
23 | ||
b7387090 AM |
24 | skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"] |
25 | ||
ba289b0b AM |
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 = [] | |
b7387090 AM |
34 | if args.buildroot: |
35 | print("%s: Caching `%s' files..." % (sys.argv[0], args.buildroot)) | |
b7387090 AM |
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 | |
ba289b0b | 41 | fpath = os.path.join(root, name) |
58c0fa8a JR |
42 | try: |
43 | rpm_build_root_files.append(hash(fpath)) | |
44 | except FileNotFoundError: | |
45 | pass | |
b7387090 | 46 | print("%s: Caching done." % (sys.argv[0])) |
ba239351 | 47 | |
b7387090 | 48 | for root, dirs, files in os.walk(args.sourcedir): |
ba239351 AM |
49 | for name in files: |
50 | fname, fext = os.path.splitext(name) | |
51 | if fext in skip_files: | |
52 | continue | |
53 | ||
54 | fpath = os.path.join(root, name) | |
58c0fa8a JR |
55 | try: |
56 | with open(fpath, 'rt', encoding='utf-8', errors='replace') as f: | |
57 | try: | |
58 | fline = f.read(128) | |
59 | f = io.StringIO(fline) | |
60 | shebang = f.readline() | |
61 | except UnicodeDecodeError as e: | |
62 | print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr) | |
63 | continue | |
64 | if re.compile(r'^#!\s*/usr/bin/env python\s').match(shebang) \ | |
65 | or re.compile(r'^#!\s*/usr/bin/env\s+python2\s').match(shebang) \ | |
66 | or re.compile(r'^#!\s*/usr/bin/python\s').match(shebang): | |
67 | rep['python2'].append(fpath) | |
68 | elif re.compile(r'^#!\s*/usr/bin/env\s+python3\s').match(shebang): | |
69 | rep['python3'].append(fpath) | |
70 | elif re.compile(r'^#!\s*/usr/bin/env\s+perl\s').match(shebang): | |
71 | rep['perl'].append(fpath) | |
72 | elif re.compile(r'^#!\s*/usr/bin/env\s+ruby\s').match(shebang): | |
73 | rep['ruby'].append(fpath) | |
74 | except FileNotFoundError: | |
75 | pass | |
ba239351 | 76 | |
ba289b0b | 77 | def gf(cmd, files): |
b7387090 AM |
78 | newfiles = [] |
79 | for f in files: | |
ba289b0b | 80 | if not rpm_build_root_files or hash(f) in rpm_build_root_files: |
b7387090 AM |
81 | newfiles.append(f) |
82 | newfiles.sort() | |
ba289b0b AM |
83 | if not newfiles: |
84 | return | |
85 | print(cmd) | |
b7387090 | 86 | for i in range(0, len(newfiles) - 1): |
2c47d05a AM |
87 | print(" %s \\\n" % os.path.relpath(newfiles[i], start=args.sourcedir), end='') |
88 | print(" %s\n" % os.path.relpath(newfiles[len(newfiles) - 1], start=args.sourcedir)) | |
ba239351 | 89 | |
178671a1 | 90 | print("\n# Copy from here:") |
b7387090 AM |
91 | print("# %s " % sys.argv[0], end='') |
92 | if args.buildroot: | |
2c47d05a | 93 | print("--buildroot=%s " % args.buildroot, end='') |
b7387090 | 94 | print("%s\n" % args.sourcedir) |
ba239351 | 95 | |
2d40e6ee | 96 | 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,' \\", |
ba289b0b | 97 | rep['python2']) |
2d40e6ee JR |
98 | gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+python3(\s|$),#!%{__python3}\\1,' \\", rep['python3']) |
99 | gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+perl(\s|$),#!%{__perl}\\1,' \\", rep['perl']) | |
100 | gf("%{__sed} -E -i -e '1s,#!\s*/usr/bin/env\s+ruby(\s|$),#!%{__ruby}\\1,' \\", rep['ruby']) |