]> git.proxmox.com Git - mirror_linux-firmware.git/blob - check_whence.py
amdgpu: update dimgrey cavefish firmware from 5.7 branch
[mirror_linux-firmware.git] / check_whence.py
1 #!/usr/bin/python3
2
3 import os, re, sys
4 from io import open
5
6
7 def list_whence():
8 with open("WHENCE", encoding="utf-8") as whence:
9 for line in whence:
10 match = re.match(r'(?:RawFile|File|Source):\s*"(.*)"', line)
11 if match:
12 yield match.group(1)
13 continue
14 match = re.match(r"(?:RawFile|File|Source):\s*(\S*)", line)
15 if match:
16 yield match.group(1)
17 continue
18 match = re.match(
19 r"Licen[cs]e: (?:.*\bSee (.*) for details\.?|(\S*))\n", line
20 )
21 if match:
22 if match.group(1):
23 for name in re.split(r", | and ", match.group(1)):
24 yield name
25 continue
26 if match.group(2):
27 # Just one word - may or may not be a filename
28 if not re.search(
29 r"unknown|distributable", match.group(2), re.IGNORECASE
30 ):
31 yield match.group(2)
32 continue
33
34
35 def list_whence_files():
36 with open("WHENCE", encoding="utf-8") as whence:
37 for line in whence:
38 match = re.match(r"(?:RawFile|File):\s*(.*)", line)
39 if match:
40 yield match.group(1).replace("\ ", " ").replace('"', "")
41 continue
42
43
44 def list_links_list():
45 with open("WHENCE", encoding="utf-8") as whence:
46 for line in whence:
47 match = re.match(r"Link:\s*(.*)", line)
48 if match:
49 linkname, target = match.group(1).split("->")
50
51 linkname = linkname.strip().replace("\ ", " ").replace('"', "")
52 target = target.strip().replace("\ ", " ").replace('"', "")
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
61
62 def list_git():
63 with os.popen("git ls-files") as git_files:
64 for line in git_files:
65 yield line.rstrip("\n")
66
67
68 def main():
69 ret = 0
70 whence_list = list(list_whence())
71 whence_files = list(list_whence_files())
72 links_list = list(list_links_list())
73 known_files = set(name for name in whence_list if not name.endswith("/")) | set(
74 [
75 ".gitignore",
76 ".codespell.cfg",
77 ".gitlab-ci.yml",
78 ".pre-commit-config.yaml",
79 "check_whence.py",
80 "configure",
81 "Makefile",
82 "README.md",
83 "copy-firmware.sh",
84 "WHENCE",
85 "Dockerfile",
86 ]
87 )
88 known_prefixes = set(name for name in whence_list if name.endswith("/"))
89 git_files = set(list_git())
90
91 for name in set(name for name in whence_files if name.endswith("/")):
92 sys.stderr.write("E: %s listed in WHENCE as File, but is directory\n" % name)
93 ret = 1
94
95 for name in set(fw for fw in whence_files if whence_files.count(fw) > 1):
96 sys.stderr.write("E: %s listed in WHENCE twice\n" % name)
97 ret = 1
98
99 for name in set(link for link in whence_files if os.path.islink(link)):
100 sys.stderr.write("E: %s listed in WHENCE as File, but is a symlink\n" % name)
101 ret = 1
102
103 for name in set(link[0] for link in links_list if os.path.islink(link[0])):
104 sys.stderr.write("E: %s listed in WHENCE as Link, is in tree\n" % name)
105 ret = 1
106
107 for name in sorted(list(known_files - git_files)):
108 sys.stderr.write("E: %s listed in WHENCE does not exist\n" % name)
109 ret = 1
110
111 # A link can point to another link, or to a file...
112 valid_targets = set(link[0] for link in links_list) | git_files
113
114 # ... or to a directory
115 for target in set(valid_targets):
116 dirname = target
117 while True:
118 dirname = os.path.dirname(dirname)
119 if dirname == "":
120 break
121 valid_targets.add(dirname)
122
123 for name, target in sorted(links_list):
124 if target not in valid_targets:
125 sys.stderr.write(
126 "E: target %s of link %s in WHENCE" " does not exist\n" % (target, name)
127 )
128 ret = 1
129
130 for name in sorted(list(git_files - known_files)):
131 # Ignore subdirectory changelogs and GPG detached signatures
132 if name.endswith("/ChangeLog") or (
133 name.endswith(".asc") and name[:-4] in known_files
134 ):
135 continue
136
137 # Ignore unknown files in known directories
138 for prefix in known_prefixes:
139 if name.startswith(prefix):
140 break
141 else:
142 sys.stderr.write("E: %s not listed in WHENCE\n" % name)
143 ret = 1
144 return ret
145
146
147 if __name__ == "__main__":
148 sys.exit(main())