Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: owners_finder.py

Issue 12712002: An interactive tool to help find owners covering current change list. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Fix nits Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « owners.py ('k') | tests/owners_finder_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Interactive tool for finding reviewers/owners for a change."""
6
7 import os
8 import copy
9 import owners as owners_module
10
11
12 def first(iterable):
13 for element in iterable:
14 return element
15
16
17 class OwnersFinder(object):
18 COLOR_LINK = '\033[4m'
M-A Ruel 2013/09/20 02:11:31 Please use colorama.
19 COLOR_BOLD = '\033[1;32m'
20 COLOR_GREY = '\033[0;37m'
21 COLOR_RESET = '\033[0m'
22
23 indentation = 0
24
25 def __init__(self, files, local_root, author,
26 fopen, os_path, glob,
27 email_postfix='@chromium.org',
28 disable_color=False):
29 self.email_postfix = email_postfix
30
31 if os.name == 'nt' or disable_color:
32 self.COLOR_LINK = ''
33 self.COLOR_BOLD = ''
34 self.COLOR_GREY = ''
35 self.COLOR_RESET = ''
36
37 self.db = owners_module.Database(local_root, fopen, os_path, glob)
38 self.db.load_data_needed_for(files)
39
40 self.os_path = os_path
41
42 self.author = author
43
44 filtered_files = files
45
46 # Eliminate files that author himself can review.
47 if author:
48 if author in self.db.owned_by:
49 for dir_name in self.db.owned_by[author]:
50 filtered_files = [
51 file_name for file_name in filtered_files
52 if not file_name.startswith(dir_name)]
53
54 filtered_files = list(filtered_files)
55
56 # Eliminate files that everyone can review.
57 if owners_module.EVERYONE in self.db.owned_by:
58 for dir_name in self.db.owned_by[owners_module.EVERYONE]:
59 filtered_files = filter(
60 lambda file_name: not file_name.startswith(dir_name),
61 filtered_files)
62
63 # If some files are eliminated.
64 if len(filtered_files) != len(files):
65 files = filtered_files
66 # Reload the database.
67 self.db = owners_module.Database(local_root, fopen, os_path, glob)
68 self.db.load_data_needed_for(files)
69
70 self.all_possible_owners = self.db.all_possible_owners(files, None)
71
72 self.owners_to_files = {}
73 self._map_owners_to_files(files)
74
75 self.files_to_owners = {}
76 self._map_files_to_owners()
77
78 self.owners_score = self.db.total_costs_by_owner(
79 self.all_possible_owners, files)
80
81 self.original_files_to_owners = copy.deepcopy(self.files_to_owners)
82 self.comments = self.db.comments
83
84 # This is the queue that will be shown in the interactive questions.
85 # It is initially sorted by the score in descending order. In the
86 # interactive questions a user can choose to "defer" its decision, then the
87 # owner will be put to the end of the queue and shown later.
88 self.owners_queue = []
89
90 self.unreviewed_files = set()
91 self.reviewed_by = {}
92 self.selected_owners = set()
93 self.deselected_owners = set()
94 self.reset()
95
96 def run(self):
97 self.reset()
98 while self.owners_queue and self.unreviewed_files:
99 owner = self.owners_queue[0]
100
101 if (owner in self.selected_owners) or (owner in self.deselected_owners):
102 continue
103
104 if not any((file_name in self.unreviewed_files)
105 for file_name in self.owners_to_files[owner]):
106 self.deselect_owner(owner)
107 continue
108
109 self.print_info(owner)
110
111 while True:
112 inp = self.input_command(owner)
113 if inp == 'y' or inp == 'yes':
114 self.select_owner(owner)
115 break
116 elif inp == 'n' or inp == 'no':
117 self.deselect_owner(owner)
118 break
119 elif inp == '' or inp == 'd' or inp == 'defer':
120 self.owners_queue.append(self.owners_queue.pop(0))
121 break
122 elif inp == 'f' or inp == 'files':
123 self.list_files()
124 break
125 elif inp == 'o' or inp == 'owners':
126 self.list_owners(self.owners_queue)
127 break
128 elif inp == 'p' or inp == 'pick':
129 self.pick_owner(raw_input('Pick an owner: '))
130 break
131 elif inp.startswith('p ') or inp.startswith('pick '):
132 self.pick_owner(inp.split(' ', 2)[1].strip())
133 break
134 elif inp == 'r' or inp == 'restart':
135 self.reset()
136 break
137 elif inp == 'q' or inp == 'quit':
138 # Exit with error
139 return 1
140
141 self.print_result()
142 return 0
143
144 def _map_owners_to_files(self, files):
145 for owner in self.all_possible_owners:
146 for dir_name, _ in self.all_possible_owners[owner]:
147 for file_name in files:
148 if file_name.startswith(dir_name):
149 self.owners_to_files.setdefault(owner, set())
150 self.owners_to_files[owner].add(file_name)
151
152 def _map_files_to_owners(self):
153 for owner in self.owners_to_files:
154 for file_name in self.owners_to_files[owner]:
155 self.files_to_owners.setdefault(file_name, set())
156 self.files_to_owners[file_name].add(owner)
157
158 def reset(self):
159 self.files_to_owners = copy.deepcopy(self.original_files_to_owners)
160 self.unreviewed_files = set(self.files_to_owners.keys())
161 self.reviewed_by = {}
162 self.selected_owners = set()
163 self.deselected_owners = set()
164
165 # Initialize owners queue, sort it by the score
166 self.owners_queue = list(sorted(self.owners_to_files.keys(),
167 key=lambda owner: self.owners_score[owner]))
168 self.find_mandatory_owners()
169
170 def select_owner(self, owner, findMandatoryOwners=True):
171 if owner in self.selected_owners or owner in self.deselected_owners\
172 or not (owner in self.owners_queue):
173 return
174 self.writeln('Selected: ' + owner)
175 self.owners_queue.remove(owner)
176 self.selected_owners.add(owner)
177 for file_name in filter(
178 lambda file_name: file_name in self.unreviewed_files,
179 self.owners_to_files[owner]):
180 self.unreviewed_files.remove(file_name)
181 self.reviewed_by[file_name] = owner
182 if findMandatoryOwners:
183 self.find_mandatory_owners()
184
185 def deselect_owner(self, owner, findMandatoryOwners=True):
186 if owner in self.selected_owners or owner in self.deselected_owners\
187 or not (owner in self.owners_queue):
188 return
189 self.writeln('Deselected: ' + owner)
190 self.owners_queue.remove(owner)
191 self.deselected_owners.add(owner)
192 for file_name in self.owners_to_files[owner] & self.unreviewed_files:
193 self.files_to_owners[file_name].remove(owner)
194 if findMandatoryOwners:
195 self.find_mandatory_owners()
196
197 def find_mandatory_owners(self):
198 continues = True
199 for owner in self.owners_queue:
200 if owner in self.selected_owners:
201 continue
202 if owner in self.deselected_owners:
203 continue
204 if len(self.owners_to_files[owner] & self.unreviewed_files) == 0:
205 self.deselect_owner(owner, False)
206
207 while continues:
208 continues = False
209 for file_name in filter(
210 lambda file_name: len(self.files_to_owners[file_name]) == 1,
211 self.unreviewed_files):
212 owner = first(self.files_to_owners[file_name])
213 self.select_owner(owner, False)
214 continues = True
215 break
216
217 def print_comments(self, owner):
218 if owner not in self.comments:
219 self.writeln(self.bold_name(owner))
220 else:
221 self.writeln(self.bold_name(owner) + ' is commented as:')
222 self.indent()
223 for path in self.comments[owner]:
224 if len(self.comments[owner][path]) > 0:
225 self.writeln(self.greyed(self.comments[owner][path]) +
226 ' (at ' + self.bold(path or '<root>') + ')')
227 else:
228 self.writeln(self.greyed('[No comment] ') + ' (at ' +
229 self.bold(path or '<root>') + ')')
230 self.unindent()
231
232 def print_file_info(self, file_name, except_owner=''):
233 if file_name not in self.unreviewed_files:
234 self.writeln(self.greyed(file_name +
235 ' (by ' +
236 self.bold_name(self.reviewed_by[file_name]) +
237 ')'))
238 else:
239 if len(self.files_to_owners[file_name]) <= 3:
240 other_owners = []
241 for ow in self.files_to_owners[file_name]:
242 if ow != except_owner:
243 other_owners.append(self.bold_name(ow))
244 self.writeln(file_name +
245 ' [' + (', '.join(other_owners)) + ']')
246 else:
247 self.writeln(file_name + ' [' +
248 self.bold(str(len(self.files_to_owners[file_name]))) +
249 ']')
250
251 def print_file_info_detailed(self, file_name):
252 self.writeln(file_name)
253 self.indent()
254 for ow in sorted(self.files_to_owners[file_name]):
255 if ow in self.deselected_owners:
256 self.writeln(self.bold_name(self.greyed(ow)))
257 elif ow in self.selected_owners:
258 self.writeln(self.bold_name(self.greyed(ow)))
259 else:
260 self.writeln(self.bold_name(ow))
261 self.unindent()
262
263 def print_owned_files_for(self, owner):
264 # Print owned files
265 self.print_comments(owner)
266 self.writeln(self.bold_name(owner) + ' owns ' +
267 str(len(self.owners_to_files[owner])) + ' file(s):')
268 self.indent()
269 for file_name in sorted(self.owners_to_files[owner]):
270 self.print_file_info(file_name, owner)
271 self.unindent()
272 self.writeln()
273
274 def list_owners(self, owners_queue):
275 if (len(self.owners_to_files) - len(self.deselected_owners) -
276 len(self.selected_owners)) > 3:
277 for ow in owners_queue:
278 if ow not in self.deselected_owners and ow not in self.selected_owners:
279 self.print_comments(ow)
280 else:
281 for ow in owners_queue:
282 if ow not in self.deselected_owners and ow not in self.selected_owners:
283 self.writeln()
284 self.print_owned_files_for(ow)
285
286 def list_files(self):
287 self.indent()
288 if len(self.unreviewed_files) > 5:
289 for file_name in sorted(self.unreviewed_files):
290 self.print_file_info(file_name)
291 else:
292 for file_name in self.unreviewed_files:
293 self.print_file_info_detailed(file_name)
294 self.unindent()
295
296 def pick_owner(self, ow):
297 # Allowing to omit domain suffixes
298 if ow not in self.owners_to_files:
299 if ow + self.email_postfix in self.owners_to_files:
300 ow += self.email_postfix
301
302 if ow not in self.owners_to_files:
303 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
304 'It\'s an invalid name or not related to the change list.')
305 return False
306 elif ow in self.selected_owners:
307 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
308 'It\'s already selected.')
309 return False
310 elif ow in self.deselected_owners:
311 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually.' +
312 'It\'s already unselected.')
313 return False
314
315 self.select_owner(ow)
316 return True
317
318 def print_result(self):
319 # Print results
320 self.writeln()
321 self.writeln()
322 self.writeln('** You selected these owners **')
323 self.writeln()
324 for owner in self.selected_owners:
325 self.writeln(self.bold_name(owner) + ':')
326 self.indent()
327 for file_name in sorted(self.owners_to_files[owner]):
328 self.writeln(file_name)
329 self.unindent()
330
331 def bold(self, text):
332 return self.COLOR_BOLD + text + self.COLOR_RESET
333
334 def bold_name(self, name):
335 return (self.COLOR_BOLD +
336 name.replace(self.email_postfix, '') + self.COLOR_RESET)
337
338 def greyed(self, text):
339 return self.COLOR_GREY + text + self.COLOR_RESET
340
341 def indent(self):
342 self.indentation += 1
343
344 def unindent(self):
345 self.indentation -= 1
346
347 def print_indent(self):
348 return ' ' * self.indentation
349
350 def writeln(self, text=''):
351 print self.print_indent() + text
352
353 def hr(self):
354 self.writeln('=====================')
355
356 def print_info(self, owner):
357 self.hr()
358 self.writeln(
359 self.bold(str(len(self.unreviewed_files))) + ' file(s) left.')
360 self.print_owned_files_for(owner)
361
362 def input_command(self, owner):
363 self.writeln('Add ' + self.bold_name(owner) + ' as your reviewer? ')
364 return raw_input(
365 '[yes/no/Defer/pick/files/owners/quit/restart]: ').lower()
OLDNEW
« no previous file with comments | « owners.py ('k') | tests/owners_finder_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698