2 # Copyright 2017 gRPC authors.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
24 # Find the root of the git tree
27 git_root = (subprocess.check_output(['git', 'rev-parse', '--show-toplevel'
28 ]).decode('utf-8').strip())
31 # Parse command line arguments
34 default_out = os.path.join(git_root, '.github', 'CODEOWNERS')
36 argp = argparse.ArgumentParser('Generate .github/CODEOWNERS file')
37 argp.add_argument('--out',
41 help='Output file (default %s)' % default_out)
42 args = argp.parse_args()
45 # Walk git tree to locate all OWNERS files
49 os.path.join(root, 'OWNERS')
50 for root, dirs, files in os.walk(git_root)
58 Owners = collections.namedtuple('Owners', 'parent directives dir')
59 Directive = collections.namedtuple('Directive', 'who globs')
62 def parse_owners(filename):
63 with open(filename) as f:
64 src = f.read().splitlines()
69 # line := directive | comment
71 if line[0] == '#': continue
74 if line == 'set noparent':
77 directive = Directive(who='*', globs=[])
79 (who, globs) = line.split(' ', 1)
80 globs_list = [glob for glob in globs.split(' ') if glob]
81 directive = Directive(who=who, globs=globs_list)
83 directive = Directive(who=line, globs=[])
85 directives.append(directive)
86 return Owners(parent=parent,
87 directives=directives,
88 dir=os.path.relpath(os.path.dirname(filename), git_root))
91 owners_data = sorted([parse_owners(filename) for filename in owners_files],
92 key=operator.attrgetter('dir'))
95 # Modify owners so that parented OWNERS files point to the actual
96 # Owners tuple with their parent field
100 for owners in owners_data:
101 if owners.parent == True:
103 best_parent_score = None
104 for possible_parent in owners_data:
105 if possible_parent is owners: continue
106 rel = os.path.relpath(owners.dir, possible_parent.dir)
107 # '..' ==> we had to walk up from possible_parent to get to owners
109 if '..' in rel: continue
110 depth = len(rel.split(os.sep))
111 if not best_parent or depth < best_parent_score:
112 best_parent = possible_parent
113 best_parent_score = depth
115 owners = owners._replace(parent=best_parent.dir)
117 owners = owners._replace(parent=None)
118 new_owners_data.append(owners)
119 owners_data = new_owners_data
122 # In bottom to top order, process owners data structures to build up
123 # a CODEOWNERS file for GitHub
127 def full_dir(rules_dir, sub_path):
128 return os.path.join(rules_dir, sub_path) if rules_dir != '.' else sub_path
137 if glob in gg_cache: return gg_cache[glob]
139 subprocess.check_output([
140 'git', 'ls-files', os.path.join(git_root, glob)
141 ]).decode('utf-8').strip().splitlines())
146 def expand_directives(root, directives):
147 globs = collections.OrderedDict()
148 # build a table of glob --> owners
149 for directive in directives:
150 for glob in directive.globs or ['**']:
151 if glob not in globs:
153 if directive.who not in globs[glob]:
154 globs[glob].append(directive.who)
155 # expand owners for intersecting globs
156 sorted_globs = sorted(globs.keys(),
157 key=lambda g: len(git_glob(full_dir(root, g))),
159 out_globs = collections.OrderedDict()
160 for glob_add in sorted_globs:
161 who_add = globs[glob_add]
162 pre_items = [i for i in out_globs.items()]
163 out_globs[glob_add] = who_add.copy()
164 for glob_have, who_have in pre_items:
165 files_add = git_glob(full_dir(root, glob_add))
166 files_have = git_glob(full_dir(root, glob_have))
167 intersect = files_have.intersection(files_add)
169 for f in sorted(files_add): # sorted to ensure merge stability
170 if f not in intersect:
171 out_globs[os.path.relpath(f, start=root)] = who_add
173 if who not in out_globs[glob_add]:
174 out_globs[glob_add].append(who)
178 def add_parent_to_globs(parent, globs, globs_dir):
179 if not parent: return
180 for owners in owners_data:
181 if owners.dir == parent:
182 owners_globs = expand_directives(owners.dir, owners.directives)
183 for oglob, oglob_who in owners_globs.items():
184 for gglob, gglob_who in globs.items():
185 files_parent = git_glob(full_dir(owners.dir, oglob))
186 files_child = git_glob(full_dir(globs_dir, gglob))
187 intersect = files_parent.intersection(files_child)
188 gglob_who_orig = gglob_who.copy()
190 for f in sorted(files_child
191 ): # sorted to ensure merge stability
192 if f not in intersect:
193 who = gglob_who_orig.copy()
194 globs[os.path.relpath(f, start=globs_dir)] = who
195 for who in oglob_who:
196 if who not in gglob_who:
197 gglob_who.append(who)
198 add_parent_to_globs(owners.parent, globs, globs_dir)
203 todo = owners_data.copy()
205 with open(args.out, 'w') as out:
206 out.write('# Auto-generated by the tools/mkowners/mkowners.py tool\n')
207 out.write('# Uses OWNERS files in different modules throughout the\n')
208 out.write('# repository as the source of truth for module ownership.\n')
212 if head.parent and not head.parent in done:
215 globs = expand_directives(head.dir, head.directives)
216 add_parent_to_globs(head.parent, globs, head.dir)
217 for glob, owners in globs.items():
219 for glob1, owners1, dir1 in reversed(written_globs):
220 files = git_glob(full_dir(head.dir, glob))
221 files1 = git_glob(full_dir(dir1, glob1))
222 intersect = files.intersection(files1)
223 if files == intersect:
224 if sorted(owners) == sorted(owners1):
225 skip = True # nothing new in this rule
228 # continuing would cause a semantic change since some files are
229 # affected differently by this rule and CODEOWNERS is order dependent
232 out.write('/%s %s\n' %
233 (full_dir(head.dir, glob), ' '.join(owners)))
234 written_globs.append((glob, owners, head.dir))