]>
Commit | Line | Data |
---|---|---|
ba239351 AM |
1 | #!/usr/bin/python3 |
2 | ||
3 | import io | |
4 | import os | |
5 | import re | |
6 | import sys | |
7 | ||
8 | rep = { | |
9 | 'python2': [], | |
10 | 'python3': [], | |
11 | 'perl': [], | |
12 | } | |
13 | ||
14 | skip_files = [".h", ".c", ".cc", ".gif", ".png", ".jpg"] | |
15 | ||
16 | for root, dirs, files in os.walk(sys.argv[1]): | |
17 | for name in files: | |
18 | fname, fext = os.path.splitext(name) | |
19 | if fext in skip_files: | |
20 | continue | |
21 | ||
22 | fpath = os.path.join(root, name) | |
23 | with open(fpath, 'rt', encoding='utf-8', errors='replace') as f: | |
24 | try: | |
25 | fline = f.read(128) | |
26 | f = io.StringIO(fline) | |
27 | shebang = f.readline() | |
28 | except UnicodeDecodeError as e: | |
29 | print("%s: skipping file `%s': %s" % (sys.argv[0], fpath, e), file=sys.stderr) | |
30 | continue | |
31 | if re.compile(r'^#!/usr/bin/env python\s').match(shebang) \ | |
32 | or re.compile(r'^#!/usr/bin/env python2\s').match(shebang) \ | |
33 | or re.compile(r'^#!/usr/bin/python\s').match(shebang): | |
34 | rep['python2'].append(fpath) | |
35 | elif re.compile(r'^#!/usr/bin/env python3\s').match(shebang): | |
36 | rep['python3'].append(fpath) | |
37 | elif re.compile(r'^#!/usr/bin/env perl\s').match(shebang): | |
38 | rep['perl'].append(fpath) | |
39 | ||
40 | def gf(files): | |
41 | files.sort() | |
42 | for i in range(0, len(files) - 1): | |
43 | print("\t%s \\\n" % files[i], end='') | |
44 | print("\t%s\n" % files[len(files) - 1]) | |
45 | ||
46 | ||
47 | if rep['python2']: | |
48 | print("sed -i -e '1s,#!/usr/bin/env python2,%{__python},' -e '1s,#!/usr/bin/env python,%{__python},' -e '1s,#!/usr/bin/python,%{__python},' \\") | |
49 | gf(rep['python2']) | |
50 | ||
51 | if rep['python3']: | |
52 | print("sed -i -e '1s,#!/usr/bin/env python3,%{__python3},' \\") | |
53 | gf(rep['python3']) | |
54 | ||
55 | if rep['perl']: | |
56 | print("sed -i -e '1s,#!/usr/bin/env perl,%{__perl},' \\") | |
57 | gf(rep['perl']) |