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 if os.path.exists(full_path): | |
Paweł Hajdan Jr.
2016/12/28 08:43:04
Do we make it a silent error if full_path doesn't
RobertoCN
2016/12/28 21:47:10
We shouldn't. If this were to fail, then something
| |
33 owners_file_lines = open(full_path).readlines() | |
Paweł Hajdan Jr.
2016/12/28 08:43:04
nit: Consider using "with" so that we close the fi
RobertoCN
2016/12/28 21:47:10
Done.
| |
34 component_entries = [l for l in owners_file_lines if l.split()[:2] == | |
35 ['#', 'COMPONENT:']] | |
36 team_entries = [l for l in owners_file_lines if l.split()[:2] == | |
37 ['#', 'TEAM:']] | |
38 if len(component_entries) > 1: | |
39 return result_dict('Contains more than one component per directory') | |
40 if len(team_entries) > 1: | |
41 return result_dict('Contains more than one team per directory') | |
42 | |
43 if not component_entries and not team_entries: | |
44 return | |
45 | |
46 if component_entries: | |
47 try: | |
48 _ = component_entries[0].split()[2] | |
49 # TODO(robertocn): Check against a static list of valid components, | |
50 # perhaps obtained from monorail at the beginning of presubmit. | |
51 except IndexError: | |
52 return result_dict('Has COMPONENT line but no component name') | |
53 | |
54 if team_entries: | |
55 team_entry_parts = team_entries[0].split('@') | |
56 if len(team_entry_parts) != 2: | |
57 return result_dict('Has TEAM line, but not exactly 1 team email') | |
58 # TODO(robertocn): Raise a warning if only one of (COMPONENT, TEAM) is | |
59 # present. | |
60 | |
61 | |
62 def main(): | |
63 usage = """Usage: python %prog [--root <dir>] <owners_file1> <owners_file2>... | |
64 owners_fileX specifies the path to the file to check, these are expected | |
65 to be relative to the root directory if --root is used. | |
66 | |
67 Examples: | |
68 python %prog --root /home/<user>/chromium/src/ tools/OWNERS v8/OWNERS | |
69 python %prog /home/<user>/chromium/src/tools/OWNERS | |
70 python %prog ./OWNERS | |
71 """ | |
72 | |
73 parser = optparse.OptionParser(usage=usage) | |
74 parser.add_option( | |
75 '--root', | |
76 help='Specifies the repository root.') | |
77 parser.add_option( | |
78 '-v', '--verbose', action='count', default=0, help='Print debug logging') | |
79 parser.add_option( | |
80 '--bare', | |
81 action='store_true', | |
82 default=False, | |
83 help='Prints the bare filename triggering the checks') | |
84 parser.add_option('--json', help='Path to JSON output file') | |
85 options, args = parser.parse_args() | |
86 | |
87 levels = [logging.ERROR, logging.INFO, logging.DEBUG] | |
88 logging.basicConfig(level=levels[min(len(levels) - 1, options.verbose)]) | |
89 | |
90 errors = filter(None, [check_owners(options.root, f) for f in args]) | |
91 | |
92 if options.json: | |
93 with open(options.json, 'w') as f: | |
94 json.dump(errors, f) | |
95 | |
96 if errors: | |
97 if options.bare: | |
98 print '\n'.join(e['full_path'] for e in errors) | |
99 else: | |
100 print '\nFAILED\n' | |
101 print '\n'.join('%s: %s' % (e['full_path'], e['error']) for e in errors) | |
102 return 1 | |
103 if not options.bare: | |
104 print '\nSUCCESS\n' | |
105 return 0 | |
106 | |
107 | |
108 if '__main__' == __name__: | |
109 sys.exit(main()) | |
OLD | NEW |