OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2017 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 component = component_entries[0].split(':')[1] |
| 49 if not component: |
| 50 return result_dict('Has COMPONENT line but no component name') |
| 51 # Check for either of the following formats: |
| 52 # component1, component2, ... |
| 53 # component1,component2,... |
| 54 # component1 component2 ... |
| 55 component_count = max( |
| 56 len(component.strip().split()), |
| 57 len(component.strip().split(','))) |
| 58 if component_count > 1: |
| 59 return result_dict('Has more than one component name') |
| 60 # TODO(robertocn): Check against a static list of valid components, |
| 61 # perhaps obtained from monorail at the beginning of presubmit. |
| 62 |
| 63 if team_entries: |
| 64 team_entry_parts = team_entries[0].split('@') |
| 65 if len(team_entry_parts) != 2: |
| 66 return result_dict('Has TEAM line, but not exactly 1 team email') |
| 67 # TODO(robertocn): Raise a warning if only one of (COMPONENT, TEAM) is |
| 68 # present. |
| 69 |
| 70 |
| 71 def main(): |
| 72 usage = """Usage: python %prog [--root <dir>] <owners_file1> <owners_file2>... |
| 73 owners_fileX specifies the path to the file to check, these are expected |
| 74 to be relative to the root directory if --root is used. |
| 75 |
| 76 Examples: |
| 77 python %prog --root /home/<user>/chromium/src/ tools/OWNERS v8/OWNERS |
| 78 python %prog /home/<user>/chromium/src/tools/OWNERS |
| 79 python %prog ./OWNERS |
| 80 """ |
| 81 |
| 82 parser = optparse.OptionParser(usage=usage) |
| 83 parser.add_option( |
| 84 '--root', help='Specifies the repository root.') |
| 85 parser.add_option( |
| 86 '-v', '--verbose', action='count', default=0, help='Print debug logging') |
| 87 parser.add_option( |
| 88 '--bare', |
| 89 action='store_true', |
| 90 default=False, |
| 91 help='Prints the bare filename triggering the checks') |
| 92 parser.add_option('--json', help='Path to JSON output file') |
| 93 options, args = parser.parse_args() |
| 94 |
| 95 levels = [logging.ERROR, logging.INFO, logging.DEBUG] |
| 96 logging.basicConfig(level=levels[min(len(levels) - 1, options.verbose)]) |
| 97 |
| 98 errors = filter(None, [check_owners(options.root, f) for f in args]) |
| 99 |
| 100 if options.json: |
| 101 with open(options.json, 'w') as f: |
| 102 json.dump(errors, f) |
| 103 |
| 104 if errors: |
| 105 if options.bare: |
| 106 print '\n'.join(e['full_path'] for e in errors) |
| 107 else: |
| 108 print '\nFAILED\n' |
| 109 print '\n'.join('%s: %s' % (e['full_path'], e['error']) for e in errors) |
| 110 return 1 |
| 111 if not options.bare: |
| 112 print '\nSUCCESS\n' |
| 113 return 0 |
| 114 |
| 115 |
| 116 if '__main__' == __name__: |
| 117 sys.exit(main()) |
OLD | NEW |