Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """Parse and check syntax errors of a given OWNERS file.""" |
| 4 | |
| 5 | import argparse |
| 6 | import re |
| 7 | import sys |
Orion Hodson | e421668 | 2022-04-04 13:22:38 +0100 | [diff] [blame] | 8 | import urllib.request, urllib.parse, urllib.error |
| 9 | import urllib.request, urllib.error, urllib.parse |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 10 | |
| 11 | parser = argparse.ArgumentParser(description='Check OWNERS file syntax') |
| 12 | parser.add_argument('-v', '--verbose', dest='verbose', |
| 13 | action='store_true', default=False, |
| 14 | help='Verbose output to debug') |
| 15 | parser.add_argument('-c', '--check_address', dest='check_address', |
| 16 | action='store_true', default=False, |
| 17 | help='Check email addresses') |
| 18 | parser.add_argument(dest='owners', metavar='OWNERS', nargs='+', |
| 19 | help='Path to OWNERS file') |
| 20 | args = parser.parse_args() |
| 21 | |
| 22 | gerrit_server = 'https://android-review.googlesource.com' |
| 23 | checked_addresses = {} |
| 24 | |
| 25 | |
| 26 | def echo(msg): |
| 27 | if args.verbose: |
Orion Hodson | e421668 | 2022-04-04 13:22:38 +0100 | [diff] [blame] | 28 | print(msg) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def find_address(address): |
| 32 | if address not in checked_addresses: |
Chih-Hung Hsieh | c5c443c | 2018-05-16 11:44:50 -0700 | [diff] [blame] | 33 | request = (gerrit_server + '/accounts/?n=1&q=email:' |
Orion Hodson | e421668 | 2022-04-04 13:22:38 +0100 | [diff] [blame] | 34 | + urllib.parse.quote(address)) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 35 | echo('Checking email address: ' + address) |
Orion Hodson | e421668 | 2022-04-04 13:22:38 +0100 | [diff] [blame] | 36 | result = urllib.request.urlopen(request).read() |
Chih-Hung Hsieh | c5c443c | 2018-05-16 11:44:50 -0700 | [diff] [blame] | 37 | checked_addresses[address] = result.find('"_account_id":') >= 0 |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 38 | if checked_addresses[address]: |
| 39 | echo('Found email address: ' + address) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 40 | return checked_addresses[address] |
| 41 | |
| 42 | |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 43 | def check_address(fname, num, address): |
| 44 | if find_address(address): |
| 45 | return 0 |
Orion Hodson | e421668 | 2022-04-04 13:22:38 +0100 | [diff] [blame] | 46 | print('%s:%d: ERROR: unknown email address: %s' % (fname, num, address)) |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 47 | return 1 |
| 48 | |
| 49 | |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 50 | def main(): |
| 51 | # One regular expression to check all valid lines. |
| 52 | noparent = 'set +noparent' |
| 53 | email = '([^@ ]+@[^ @]+|\\*)' |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 54 | emails = '(%s( *, *%s)*)' % (email, email) |
Chih-Hung Hsieh | cc47729 | 2019-03-04 15:13:20 -0800 | [diff] [blame] | 55 | file_directive = 'file: *([^ :]+ *: *)?[^ ]+' |
| 56 | directive = '(%s|%s|%s)' % (emails, noparent, file_directive) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 57 | glob = '[a-zA-Z0-9_\\.\\-\\*\\?]+' |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 58 | globs = '(%s( *, *%s)*)' % (glob, glob) |
| 59 | perfile = 'per-file +' + globs + ' *= *' + directive |
Chih-Hung Hsieh | 6512949 | 2018-12-07 14:27:17 -0800 | [diff] [blame] | 60 | include = 'include +([^ :]+ *: *)?[^ ]+' |
Chih-Hung Hsieh | cc47729 | 2019-03-04 15:13:20 -0800 | [diff] [blame] | 61 | pats = '(|%s|%s|%s|%s|%s)$' % (noparent, email, perfile, include, file_directive) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 62 | patterns = re.compile(pats) |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 63 | address_pattern = re.compile('([^@ ]+@[^ @]+)') |
| 64 | perfile_pattern = re.compile('per-file +.*=(.*)') |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 65 | |
| 66 | error = 0 |
| 67 | for fname in args.owners: |
| 68 | echo('Checking file: ' + fname) |
| 69 | num = 0 |
| 70 | for line in open(fname, 'r'): |
| 71 | num += 1 |
| 72 | stripped_line = re.sub('#.*$', '', line).strip() |
| 73 | if not patterns.match(stripped_line): |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 74 | error += 1 |
Orion Hodson | e421668 | 2022-04-04 13:22:38 +0100 | [diff] [blame] | 75 | print('%s:%d: ERROR: unknown line [%s]' % (fname, num, line.strip())) |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 76 | elif args.check_address: |
| 77 | if perfile_pattern.match(stripped_line): |
| 78 | for addr in perfile_pattern.match(stripped_line).group(1).split(','): |
| 79 | a = addr.strip() |
| 80 | if a and a != '*': |
| 81 | error += check_address(fname, num, addr.strip()) |
| 82 | elif address_pattern.match(stripped_line): |
| 83 | error += check_address(fname, num, stripped_line) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 84 | sys.exit(error) |
| 85 | |
| 86 | if __name__ == '__main__': |
| 87 | main() |