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 |
Chih-Hung Hsieh | bee0dec | 2017-04-05 14:16:49 -0700 | [diff] [blame] | 8 | import urllib |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 9 | import urllib2 |
| 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: |
| 28 | print msg |
| 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:' |
Chih-Hung Hsieh | bee0dec | 2017-04-05 14:16:49 -0700 | [diff] [blame] | 34 | + urllib.quote(address)) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 35 | echo('Checking email address: ' + address) |
| 36 | result = urllib2.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 |
| 46 | print '%s:%d: ERROR: unknown email address: %s' % (fname, num, address) |
| 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) |
| 55 | directive = '(%s|%s)' % (emails, noparent) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 56 | glob = '[a-zA-Z0-9_\\.\\-\\*\\?]+' |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 57 | globs = '(%s( *, *%s)*)' % (glob, glob) |
| 58 | perfile = 'per-file +' + globs + ' *= *' + directive |
Chih-Hung Hsieh | 6512949 | 2018-12-07 14:27:17 -0800 | [diff] [blame] | 59 | include = 'include +([^ :]+ *: *)?[^ ]+' |
| 60 | pats = '(|%s|%s|%s|%s)$' % (noparent, email, perfile, include) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 61 | patterns = re.compile(pats) |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 62 | address_pattern = re.compile('([^@ ]+@[^ @]+)') |
| 63 | perfile_pattern = re.compile('per-file +.*=(.*)') |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 64 | |
| 65 | error = 0 |
| 66 | for fname in args.owners: |
| 67 | echo('Checking file: ' + fname) |
| 68 | num = 0 |
| 69 | for line in open(fname, 'r'): |
| 70 | num += 1 |
| 71 | stripped_line = re.sub('#.*$', '', line).strip() |
| 72 | if not patterns.match(stripped_line): |
Chih-Hung Hsieh | f77c7f7 | 2018-08-15 16:07:27 -0700 | [diff] [blame] | 73 | error += 1 |
| 74 | print '%s:%d: ERROR: unknown line [%s]' % (fname, num, line.strip()) |
| 75 | elif args.check_address: |
| 76 | if perfile_pattern.match(stripped_line): |
| 77 | for addr in perfile_pattern.match(stripped_line).group(1).split(','): |
| 78 | a = addr.strip() |
| 79 | if a and a != '*': |
| 80 | error += check_address(fname, num, addr.strip()) |
| 81 | elif address_pattern.match(stripped_line): |
| 82 | error += check_address(fname, num, stripped_line) |
Chih-Hung Hsieh | adaed14 | 2017-02-22 16:14:48 -0800 | [diff] [blame] | 83 | sys.exit(error) |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | main() |