]>
Commit | Line | Data |
---|---|---|
6de0c03f | 1 | #!/usr/bin/python3 |
7d887360 BH |
2 | |
3 | import os, re, sys | |
94cb0a68 | 4 | from io import open |
7d887360 BH |
5 | |
6 | def list_whence(): | |
94cb0a68 | 7 | with open('WHENCE', encoding='utf-8') as whence: |
7d887360 | 8 | for line in whence: |
9cfefbd7 | 9 | match = re.match(r'(?:File|Source):\s*"(.*)"', line) |
be15035d HG |
10 | if match: |
11 | yield match.group(1) | |
12 | continue | |
9cfefbd7 | 13 | match = re.match(r'(?:File|Source):\s*(\S*)', line) |
7d887360 BH |
14 | if match: |
15 | yield match.group(1) | |
16 | continue | |
17 | match = re.match(r'Licen[cs]e: (?:.*\bSee (.*) for details\.?|(\S*))\n', | |
18 | line) | |
19 | if match: | |
20 | if match.group(1): | |
21 | for name in re.split(r', | and ', match.group(1)): | |
22 | yield name | |
23 | continue | |
24 | if match.group(2): | |
25 | # Just one word - may or may not be a filename | |
26 | if not re.search(r'unknown|distributable', match.group(2), | |
27 | re.IGNORECASE): | |
28 | yield match.group(2) | |
29 | continue | |
30 | ||
05183b7b EV |
31 | def list_whence_files(): |
32 | with open('WHENCE', encoding='utf-8') as whence: | |
33 | for line in whence: | |
34 | match = re.match(r'File:\s*(.*)', line) | |
35 | if match: | |
4b539e7a | 36 | yield match.group(1).replace("\ ", " ").replace("\"", "") |
05183b7b EV |
37 | continue |
38 | ||
77f92e0b EV |
39 | def list_links_list(): |
40 | with open('WHENCE', encoding='utf-8') as whence: | |
41 | for line in whence: | |
42 | match = re.match(r'Link:\s*(.*)', line) | |
43 | if match: | |
44 | linkname, target = match.group(1).split("->") | |
45 | ||
46 | linkname = linkname.strip().replace("\ ", " ").replace("\"", "") | |
47 | target = target.strip().replace("\ ", " ").replace("\"", "") | |
48 | ||
49 | # Link target is relative to the link | |
50 | target = os.path.join(os.path.dirname(linkname), target) | |
51 | target = os.path.normpath(target) | |
52 | ||
53 | yield (linkname, target) | |
54 | continue | |
55 | ||
7d887360 BH |
56 | def list_git(): |
57 | with os.popen('git ls-files') as git_files: | |
58 | for line in git_files: | |
59 | yield line.rstrip('\n') | |
60 | ||
61 | def main(): | |
7fa32bcc | 62 | ret = 0 |
7d887360 | 63 | whence_list = list(list_whence()) |
05183b7b | 64 | whence_files = list(list_whence_files()) |
77f92e0b | 65 | links_list = list(list_links_list()) |
7d887360 BH |
66 | known_files = set(name for name in whence_list if not name.endswith('/')) | \ |
67 | set(['check_whence.py', 'configure', 'Makefile', | |
c0fb3d98 | 68 | 'README', 'copy-firmware.sh', 'WHENCE']) |
7d887360 BH |
69 | known_prefixes = set(name for name in whence_list if name.endswith('/')) |
70 | git_files = set(list_git()) | |
71 | ||
6c9e0ed5 EV |
72 | for name in set(name for name in whence_files if name.endswith('/')): |
73 | sys.stderr.write('E: %s listed in WHENCE as File, but is directory\n' % | |
74 | name) | |
75 | ret = 1 | |
76 | ||
05183b7b EV |
77 | for name in set(fw for fw in whence_files if whence_files.count(fw) > 1): |
78 | sys.stderr.write('E: %s listed in WHENCE twice\n' % name) | |
79 | ret = 1 | |
80 | ||
f2671b1f EV |
81 | for name in set(link for link in whence_files if os.path.islink(link)): |
82 | sys.stderr.write('E: %s listed in WHENCE as File, but is a symlink\n' % | |
83 | name) | |
84 | ret = 1 | |
85 | ||
77f92e0b EV |
86 | for name in set(link[0] for link in links_list if os.path.islink(link[0])): |
87 | sys.stderr.write('E: %s listed in WHENCE as Link, is in tree\n' % name) | |
88 | ret = 1 | |
89 | ||
7d887360 BH |
90 | for name in sorted(list(known_files - git_files)): |
91 | sys.stderr.write('E: %s listed in WHENCE does not exist\n' % name) | |
7fa32bcc | 92 | ret = 1 |
7d887360 | 93 | |
9e0343cf AS |
94 | # A link can point to another link, or to a file... |
95 | valid_targets = set(link[0] for link in links_list) | git_files | |
96 | ||
97 | # ... or to a directory | |
98 | for target in set(valid_targets): | |
99 | dirname = target | |
100 | while True: | |
101 | dirname = os.path.dirname(dirname) | |
102 | if dirname == '': | |
103 | break | |
104 | valid_targets.add(dirname) | |
105 | ||
106 | for name, target in sorted(links_list): | |
107 | if target not in valid_targets: | |
108 | sys.stderr.write('E: target %s of link %s in WHENCE' | |
109 | ' does not exist\n' % (target, name)) | |
110 | ret = 1 | |
111 | ||
7d887360 BH |
112 | for name in sorted(list(git_files - known_files)): |
113 | # Ignore subdirectory changelogs and GPG detached signatures | |
114 | if (name.endswith('/ChangeLog') or | |
115 | (name.endswith('.asc') and name[:-4] in known_files)): | |
116 | continue | |
117 | ||
118 | # Ignore unknown files in known directories | |
119 | for prefix in known_prefixes: | |
120 | if name.startswith(prefix): | |
121 | break | |
122 | else: | |
123 | sys.stderr.write('E: %s not listed in WHENCE\n' % name) | |
7fa32bcc BN |
124 | ret = 1 |
125 | return ret | |
7d887360 BH |
126 | |
127 | if __name__ == '__main__': | |
7fa32bcc | 128 | sys.exit(main()) |