Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os, re, sys |
| 4 | |
| 5 | def list_whence(): |
| 6 | with open('WHENCE') as whence: |
| 7 | for line in whence: |
Thierry Reding | 016a2ec | 2019-09-30 13:17:04 +0200 | [diff] [blame] | 8 | match = re.match(r'(?:File|Source):\s*"(.*)"', line) |
Hans de Goede | 026bbcf | 2018-12-13 17:35:11 +0100 | [diff] [blame] | 9 | if match: |
| 10 | yield match.group(1) |
| 11 | continue |
Thierry Reding | 016a2ec | 2019-09-30 13:17:04 +0200 | [diff] [blame] | 12 | match = re.match(r'(?:File|Source):\s*(\S*)', line) |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 13 | if match: |
| 14 | yield match.group(1) |
| 15 | continue |
| 16 | match = re.match(r'Licen[cs]e: (?:.*\bSee (.*) for details\.?|(\S*))\n', |
| 17 | line) |
| 18 | if match: |
| 19 | if match.group(1): |
| 20 | for name in re.split(r', | and ', match.group(1)): |
| 21 | yield name |
| 22 | continue |
| 23 | if match.group(2): |
| 24 | # Just one word - may or may not be a filename |
| 25 | if not re.search(r'unknown|distributable', match.group(2), |
| 26 | re.IGNORECASE): |
| 27 | yield match.group(2) |
| 28 | continue |
| 29 | |
| 30 | def list_git(): |
| 31 | with os.popen('git ls-files') as git_files: |
| 32 | for line in git_files: |
| 33 | yield line.rstrip('\n') |
| 34 | |
| 35 | def main(): |
Brian Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame^] | 36 | ret = 0 |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 37 | whence_list = list(list_whence()) |
| 38 | known_files = set(name for name in whence_list if not name.endswith('/')) | \ |
| 39 | set(['check_whence.py', 'configure', 'Makefile', |
Josh Boyer | 95d22ac | 2019-08-21 08:03:43 -0400 | [diff] [blame] | 40 | 'README', 'copy-firmware.sh', 'WHENCE']) |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 41 | known_prefixes = set(name for name in whence_list if name.endswith('/')) |
| 42 | git_files = set(list_git()) |
| 43 | |
Brian Norris | 5a12147 | 2020-01-23 10:40:34 -0800 | [diff] [blame] | 44 | known_files |= set(['OWNERS', |
| 45 | 'PRESUBMIT.cfg', |
| 46 | 'README.chromium.md', |
| 47 | ]) |
| 48 | |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 49 | for name in sorted(list(known_files - git_files)): |
| 50 | sys.stderr.write('E: %s listed in WHENCE does not exist\n' % name) |
Brian Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame^] | 51 | ret = 1 |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 52 | |
| 53 | for name in sorted(list(git_files - known_files)): |
| 54 | # Ignore subdirectory changelogs and GPG detached signatures |
| 55 | if (name.endswith('/ChangeLog') or |
| 56 | (name.endswith('.asc') and name[:-4] in known_files)): |
| 57 | continue |
| 58 | |
| 59 | # Ignore unknown files in known directories |
| 60 | for prefix in known_prefixes: |
| 61 | if name.startswith(prefix): |
| 62 | break |
| 63 | else: |
| 64 | sys.stderr.write('E: %s not listed in WHENCE\n' % name) |
Brian Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame^] | 65 | ret = 1 |
| 66 | return ret |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 67 | |
| 68 | if __name__ == '__main__': |
Brian Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame^] | 69 | sys.exit(main()) |