Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os, re, sys |
Brian Norris | df6c2a4 | 2020-02-28 18:15:05 -0800 | [diff] [blame^] | 4 | from io import open |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 5 | |
| 6 | def list_whence(): |
Brian Norris | df6c2a4 | 2020-02-28 18:15:05 -0800 | [diff] [blame^] | 7 | with open('WHENCE', encoding='utf-8') as whence: |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 8 | for line in whence: |
Thierry Reding | 016a2ec | 2019-09-30 13:17:04 +0200 | [diff] [blame] | 9 | match = re.match(r'(?:File|Source):\s*"(.*)"', line) |
Hans de Goede | 026bbcf | 2018-12-13 17:35:11 +0100 | [diff] [blame] | 10 | if match: |
| 11 | yield match.group(1) |
| 12 | continue |
Thierry Reding | 016a2ec | 2019-09-30 13:17:04 +0200 | [diff] [blame] | 13 | match = re.match(r'(?:File|Source):\s*(\S*)', line) |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 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_git(): |
| 32 | with os.popen('git ls-files') as git_files: |
| 33 | for line in git_files: |
| 34 | yield line.rstrip('\n') |
| 35 | |
| 36 | def main(): |
Brian Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame] | 37 | ret = 0 |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 38 | 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 Boyer | 95d22ac | 2019-08-21 08:03:43 -0400 | [diff] [blame] | 41 | 'README', 'copy-firmware.sh', 'WHENCE']) |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 42 | known_prefixes = set(name for name in whence_list if name.endswith('/')) |
| 43 | git_files = set(list_git()) |
| 44 | |
Brian Norris | 5a12147 | 2020-01-23 10:40:34 -0800 | [diff] [blame] | 45 | known_files |= set(['OWNERS', |
| 46 | 'PRESUBMIT.cfg', |
| 47 | 'README.chromium.md', |
| 48 | ]) |
| 49 | |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 50 | for name in sorted(list(known_files - git_files)): |
| 51 | 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] | 52 | ret = 1 |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 53 | |
| 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 Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame] | 66 | ret = 1 |
| 67 | return ret |
Ben Hutchings | 746efa1 | 2016-09-18 01:44:04 +0100 | [diff] [blame] | 68 | |
| 69 | if __name__ == '__main__': |
Brian Norris | 9528945 | 2020-02-28 17:32:48 -0800 | [diff] [blame] | 70 | sys.exit(main()) |