blob: 6d58077cf6357370cb39ca238e819f8cb1e8cae8 [file] [log] [blame]
Ben Hutchings746efa12016-09-18 01:44:04 +01001#!/usr/bin/python
2
3import os, re, sys
Brian Norrisdf6c2a42020-02-28 18:15:05 -08004from io import open
Ben Hutchings746efa12016-09-18 01:44:04 +01005
6def list_whence():
Brian Norrisdf6c2a42020-02-28 18:15:05 -08007 with open('WHENCE', encoding='utf-8') as whence:
Ben Hutchings746efa12016-09-18 01:44:04 +01008 for line in whence:
Thierry Reding016a2ec2019-09-30 13:17:04 +02009 match = re.match(r'(?:File|Source):\s*"(.*)"', line)
Hans de Goede026bbcf2018-12-13 17:35:11 +010010 if match:
11 yield match.group(1)
12 continue
Thierry Reding016a2ec2019-09-30 13:17:04 +020013 match = re.match(r'(?:File|Source):\s*(\S*)', line)
Ben Hutchings746efa12016-09-18 01:44:04 +010014 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
31def list_git():
32 with os.popen('git ls-files') as git_files:
33 for line in git_files:
34 yield line.rstrip('\n')
35
36def main():
Brian Norris95289452020-02-28 17:32:48 -080037 ret = 0
Ben Hutchings746efa12016-09-18 01:44:04 +010038 whence_list = list(list_whence())
39 known_files = set(name for name in whence_list if not name.endswith('/')) | \
40 set(['check_whence.py', 'configure', 'Makefile',
Josh Boyer95d22ac2019-08-21 08:03:43 -040041 'README', 'copy-firmware.sh', 'WHENCE'])
Ben Hutchings746efa12016-09-18 01:44:04 +010042 known_prefixes = set(name for name in whence_list if name.endswith('/'))
43 git_files = set(list_git())
44
Brian Norris5a121472020-01-23 10:40:34 -080045 known_files |= set(['OWNERS',
46 'PRESUBMIT.cfg',
47 'README.chromium.md',
48 ])
49
Ben Hutchings746efa12016-09-18 01:44:04 +010050 for name in sorted(list(known_files - git_files)):
51 sys.stderr.write('E: %s listed in WHENCE does not exist\n' % name)
Brian Norris95289452020-02-28 17:32:48 -080052 ret = 1
Ben Hutchings746efa12016-09-18 01:44:04 +010053
54 for name in sorted(list(git_files - known_files)):
55 # Ignore subdirectory changelogs and GPG detached signatures
56 if (name.endswith('/ChangeLog') or
57 (name.endswith('.asc') and name[:-4] in known_files)):
58 continue
59
60 # Ignore unknown files in known directories
61 for prefix in known_prefixes:
62 if name.startswith(prefix):
63 break
64 else:
65 sys.stderr.write('E: %s not listed in WHENCE\n' % name)
Brian Norris95289452020-02-28 17:32:48 -080066 ret = 1
67 return ret
Ben Hutchings746efa12016-09-18 01:44:04 +010068
69if __name__ == '__main__':
Brian Norris95289452020-02-28 17:32:48 -080070 sys.exit(main())