]> git.proxmox.com Git - mirror_linux-firmware.git/blob - check_whence.py
check_whence: Check link targets are valid
[mirror_linux-firmware.git] / check_whence.py
1 #!/usr/bin/python3
2
3 import os, re, sys
4 from io import open
5
6 def list_whence():
7 with open('WHENCE', encoding='utf-8') as whence:
8 for line in whence:
9 match = re.match(r'(?:File|Source):\s*"(.*)"', line)
10 if match:
11 yield match.group(1)
12 continue
13 match = re.match(r'(?:File|Source):\s*(\S*)', line)
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
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:
36 yield match.group(1).replace("\ ", " ").replace("\"", "")
37 continue
38
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
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():
62 ret = 0
63 whence_list = list(list_whence())
64 whence_files = list(list_whence_files())
65 links_list = list(list_links_list())
66 known_files = set(name for name in whence_list if not name.endswith('/')) | \
67 set(['check_whence.py', 'configure', 'Makefile',
68 'README', 'copy-firmware.sh', 'WHENCE'])
69 known_prefixes = set(name for name in whence_list if name.endswith('/'))
70 git_files = set(list_git())
71
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
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
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
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
90 for name in sorted(list(known_files - git_files)):
91 sys.stderr.write('E: %s listed in WHENCE does not exist\n' % name)
92 ret = 1
93
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
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)
124 ret = 1
125 return ret
126
127 if __name__ == '__main__':
128 sys.exit(main())