]>
Commit | Line | Data |
---|---|---|
ba239351 AM |
1 | #!/usr/bin/python3 |
2 | ||
b7387090 AM |
3 | # thisscript.py --root=~/tmp/somepackage ~/rpm/BUILD/somepackage/ |
4 | ||
5 | import argparse | |
ba239351 AM |
6 | import io |
7 | import os | |
8 | import re | |
9 | import sys | |
10 | ||
b7387090 AM |
11 | parser = argparse.ArgumentParser() |
12 | parser.add_argument('sourcedir', help='RPM_SOURCE_DIR directory') | |
13 | parser.add_argument("--buildroot", help="RPM_BUILD_ROOT directory") | |
14 | args = parser.parse_args() | |
15 | ||
ba239351 AM |
16 | rep = { |
17 | 'python2': [], | |
18 | 'python3': [], | |
19 | 'perl': [], | |
20 | } | |
21 | ||
b7387090 AM |
22 | skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg", ".ko", ".gz", ".o"] |
23 | ||
24 | if args.buildroot: | |
25 | print("%s: Caching `%s' files..." % (sys.argv[0], args.buildroot)) | |
26 | rpm_build_root_files = [] | |
27 | for root, dirs, files in os.walk(args.buildroot): | |
28 | for name in files: | |
29 | fname, fext = os.path.splitext(name) | |
30 | if fext in skip_files: | |
31 | continue | |
32 | rpm_build_root_files.append(fname) | |
33 | print("%s: Caching done." % (sys.argv[0])) | |
ba239351 | 34 | |
b7387090 | 35 | for root, dirs, files in os.walk(args.sourcedir): |
ba239351 AM |
36 | for name in files: |
37 | fname, fext = os.path.splitext(name) | |
38 | if fext in skip_files: | |
39 | continue | |
40 | ||
41 | fpath = os.path.join(root, name) | |
42 | with open(fpath, 'rt', encoding='utf-8', errors='replace') as f: | |
43 | try: | |
44 | fline = f.read(128) | |
45 | f = io.StringIO(fline) | |
46 | shebang = f.readline() | |
47 | except UnicodeDecodeError as e: | |
48 | print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr) | |
49 | continue | |
50 | if re.compile(r'^#!/usr/bin/env python\s').match(shebang) \ | |
51 | or re.compile(r'^#!/usr/bin/env python2\s').match(shebang) \ | |
52 | or re.compile(r'^#!/usr/bin/python\s').match(shebang): | |
53 | rep['python2'].append(fpath) | |
54 | elif re.compile(r'^#!/usr/bin/env python3\s').match(shebang): | |
55 | rep['python3'].append(fpath) | |
56 | elif re.compile(r'^#!/usr/bin/env perl\s').match(shebang): | |
57 | rep['perl'].append(fpath) | |
58 | ||
59 | def gf(files): | |
b7387090 AM |
60 | newfiles = [] |
61 | for f in files: | |
62 | if not rpm_build_root_files or os.path.basename(f) in rpm_build_root_files: | |
63 | newfiles.append(f) | |
64 | newfiles.sort() | |
65 | for i in range(0, len(newfiles) - 1): | |
fb933793 AM |
66 | print("\t%s \\\n" % os.path.relpath(newfiles[i], start=args.sourcedir), end='') |
67 | print("\t%s\n" % os.path.relpath(newfiles[len(newfiles) - 1], start=args.sourcedir)) | |
ba239351 | 68 | |
b7387090 AM |
69 | print("\n# Copy from here:", file=sys.stderr) |
70 | print("# %s " % sys.argv[0], end='') | |
71 | if args.buildroot: | |
72 | print("--root=%s " % args.buildroot, end='') | |
73 | print("%s\n" % args.sourcedir) | |
ba239351 AM |
74 | |
75 | if rep['python2']: | |
76 | print("sed -i -e '1s,#!/usr/bin/env python2,%{__python},' -e '1s,#!/usr/bin/env python,%{__python},' -e '1s,#!/usr/bin/python,%{__python},' \\") | |
77 | gf(rep['python2']) | |
78 | ||
79 | if rep['python3']: | |
80 | print("sed -i -e '1s,#!/usr/bin/env python3,%{__python3},' \\") | |
81 | gf(rep['python3']) | |
82 | ||
83 | if rep['perl']: | |
84 | print("sed -i -e '1s,#!/usr/bin/env perl,%{__perl},' \\") | |
85 | gf(rep['perl']) |