Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Makes sure OWNERS files have consistent TEAM and COMPONENT tags.""" | |
| 7 | |
| 8 | |
| 9 import json | |
| 10 import logging | |
| 11 import optparse | |
| 12 import os | |
| 13 import sys | |
| 14 | |
| 15 | |
| 16 def check_owners(root, owners_path): | |
| 17 """Component and Team check in OWNERS files. crbug.com/667954""" | |
| 18 if root: | |
| 19 full_path = os.path.join(root, owners_path) | |
| 20 rel_path = owners_path | |
| 21 else: | |
| 22 full_path = os.path.abspath(owners_path) | |
| 23 rel_path = os.path.relpath(owners_path) | |
| 24 | |
| 25 def result_dict(error): | |
| 26 return { | |
| 27 'error': error, | |
| 28 'full_path': full_path, | |
| 29 'rel_path': rel_path, | |
| 30 } | |
| 31 | |
| 32 with open(full_path) as f: | |
| 33 owners_file_lines = f.readlines() | |
| 34 | |
| 35 component_entries = [l for l in owners_file_lines if l.split()[:2] == | |
| 36 ['#', 'COMPONENT:']] | |
| 37 team_entries = [l for l in owners_file_lines if l.split()[:2] == | |
| 38 ['#', 'TEAM:']] | |
| 39 if len(component_entries) > 1: | |
| 40 return result_dict('Contains more than one component per directory') | |
| 41 if len(team_entries) > 1: | |
| 42 return result_dict('Contains more than one team per directory') | |
| 43 | |
| 44 if not component_entries and not team_entries: | |
| 45 return | |
| 46 | |
| 47 if component_entries: | |
| 48 try: | |
| 49 _ = component_entries[0].split()[2] | |
| 50 # TODO(robertocn): Check against a static list of valid components, | |
| 51 # perhaps obtained from monorail at the beginning of presubmit. | |
| 52 except IndexError: | |
| 53 return result_dict('Has COMPONENT line but no component name') | |
| 54 | |
| 55 if team_entries: | |
| 56 team_entry_parts = team_entries[0].split('@') | |
| 57 if len(team_entry_parts) != 2: | |
| 58 return result_dict('Has TEAM line, but not exactly 1 team email') | |
| 59 # TODO(robertocn): Raise a warning if only one of (COMPONENT, TEAM) is | |
| 60 # present. | |
| 61 | |
| 62 | |
| 63 def main(): | |
| 64 usage = """Usage: python %prog [--root <dir>] <owners_file1> <owners_file2>... | |
| 65 owners_fileX specifies the path to the file to check, these are expected | |
| 66 to be relative to the root directory if --root is used. | |
| 67 | |
| 68 Examples: | |
| 69 python %prog --root /home/<user>/chromium/src/ tools/OWNERS v8/OWNERS | |
| 70 python %prog /home/<user>/chromium/src/tools/OWNERS | |
| 71 python %prog ./OWNERS | |
| 72 """ | |
| 73 | |
| 74 parser = optparse.OptionParser(usage=usage) | |
| 75 parser.add_option( | |
| 76 '--root', | |
| 77 help='Specifies the repository root.') | |
|
stgao
2017/01/03 22:46:59
nit: fit in last line.
RobertoCN
2017/01/04 18:48:59
Done.
| |
| 78 parser.add_option( | |
| 79 '-v', '--verbose', action='count', default=0, help='Print debug logging') | |
| 80 parser.add_option( | |
| 81 '--bare', | |
| 82 action='store_true', | |
| 83 default=False, | |
| 84 help='Prints the bare filename triggering the checks') | |
| 85 parser.add_option('--json', help='Path to JSON output file') | |
| 86 options, args = parser.parse_args() | |
| 87 | |
| 88 levels = [logging.ERROR, logging.INFO, logging.DEBUG] | |
| 89 logging.basicConfig(level=levels[min(len(levels) - 1, options.verbose)]) | |
| 90 | |
| 91 errors = filter(None, [check_owners(options.root, f) for f in args]) | |
| 92 | |
| 93 if options.json: | |
| 94 with open(options.json, 'w') as f: | |
| 95 json.dump(errors, f) | |
| 96 | |
| 97 if errors: | |
| 98 if options.bare: | |
| 99 print '\n'.join(e['full_path'] for e in errors) | |
| 100 else: | |
| 101 print '\nFAILED\n' | |
| 102 print '\n'.join('%s: %s' % (e['full_path'], e['error']) for e in errors) | |
| 103 return 1 | |
| 104 if not options.bare: | |
| 105 print '\nSUCCESS\n' | |
| 106 return 0 | |
| 107 | |
| 108 | |
| 109 if '__main__' == __name__: | |
| 110 sys.exit(main()) | |
| OLD | NEW |