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