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

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: Reuse scoring Created 7 years, 4 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
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/16 20:33:13 Use colorama, it works on Windows.
Bei Zhang 2013/09/19 23:21:43 Is that installed by default? I will probably addr
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 eliminated_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 eliminated_files = [
51 file_name for file_name in eliminated_files
52 if not file_name.startswith(dir_name)]
53
54 eliminated_files = list(eliminated_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 eliminated_files = filter(
60 lambda file_name: not file_name.startswith(dir_name),
61 eliminated_files)
62
63 # If some files are eliminated.
64 if len(eliminated_files) != len(files):
65 files = eliminated_files
Dirk Pranke 2013/09/16 23:56:11 This is confusing ... if I'm reading this correctl
Bei Zhang 2013/09/19 23:21:43 This should be named "filterd_files". On 2013/09/
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 len(self.owners_queue) > 0 and len(self.unreviewed_files) > 0:
Dirk Pranke 2013/09/16 23:56:11 Nit: this could be just: while len(self.owners_qu
Bei Zhang 2013/09/19 23:21:43 Done.
99 owner = self.owners_queue[0]
100
101 if owner in self.selected_owners:
102 continue
103
104 if len(self.unreviewed_files) == 0:
105 self.writeln('Finished.\n\n')
106 break
Dirk Pranke 2013/09/16 23:56:11 Will lines 104-106 ever execute, given the check o
Bei Zhang 2013/09/19 23:21:43 Done.
107 if owner in self.deselected_owners:
108 # If this owner is already deselected.
109 continue
Dirk Pranke 2013/09/16 23:56:11 Nit: can you merge lines 107-109 with lines 101-10
Bei Zhang 2013/09/19 23:21:43 Done.
110 if not any((file_name in self.unreviewed_files)
111 for file_name in self.owners_to_files[owner]):
112 self.deselect_owner(owner)
113 continue
114
115 self.print_info(owner)
116
117 while True:
118 inp = self.input_command(owner)
119 if inp == 'y' or inp == 'yes':
120 self.select_owner(owner)
121 break
122 elif inp == 'n' or inp == 'no':
123 self.deselect_owner(owner)
124 break
125 elif inp == '' or inp == 'd' or inp == 'defer':
126 self.owners_queue.append(self.owners_queue.pop(0))
127 break
128 elif inp == 'f' or inp == 'files':
129 self.list_files()
130 break
131 elif inp == 'o' or inp == 'owners':
132 self.list_owners(self.owners_queue)
133 break
134 elif inp == 'p' or inp == 'pick':
135 self.pick_owner(raw_input('Pick an owner: '))
136 break
137 elif inp.startswith('p ') or inp.startswith('pick '):
138 self.pick_owner(inp.split(' ', 2)[1].strip())
139 break
140 elif inp == 'r' or inp == 'restart':
141 self.reset()
142 break
143 elif inp == 'q' or inp == 'quit':
144 # Exit with error
145 return 1
146
147 self.print_result()
148 return 0
149
150 def _map_owners_to_files(self, files):
151 for owner in self.all_possible_owners:
152 for dir_name, _ in self.all_possible_owners[owner]:
153 for file_name in files:
154 if file_name.startswith(dir_name):
155 self.owners_to_files.setdefault(owner, set())
156 self.owners_to_files[owner].add(file_name)
Dirk Pranke 2013/09/16 23:56:11 Is this different from self.owners.owned_by ?
Bei Zhang 2013/09/19 23:21:43 There is no self.owners.owned_by. There is a self.
157
158 def _map_files_to_owners(self):
159 for owner in self.owners_to_files:
160 for file_name in self.owners_to_files[owner]:
161 self.files_to_owners.setdefault(file_name, set())
162 self.files_to_owners[file_name].add(owner)
Dirk Pranke 2013/09/16 23:56:11 Is this different from self.owners.owners_for ?
Bei Zhang 2013/09/19 23:21:43 Likewise. Only interesting files are included. On
Dirk Pranke 2013/09/20 02:06:40 Got it, thanks.
163
164 def reset(self):
165 self.files_to_owners = copy.deepcopy(self.original_files_to_owners)
166 self.unreviewed_files = set(self.files_to_owners.keys())
167 self.reviewed_by = {}
168 self.selected_owners = set()
169 self.deselected_owners = set()
170
171 # Initialize owners queue, sort it by the score
172 self.owners_queue = list(sorted(self.owners_to_files.keys(),
173 key=lambda owner: self.owners_score[owner]))
174 self.find_mandatory_owners()
175
176 def select_owner(self, owner, findMandatoryOwners=True):
177 if owner in self.selected_owners:
178 return
179 if owner in self.deselected_owners:
180 return
181 if not (owner in self.owners_queue):
182 return
Dirk Pranke 2013/09/16 23:56:11 Nit: I'd probably collapse these into one 'if' blo
Bei Zhang 2013/09/19 23:21:43 Done.
183 self.writeln('Selected: ' + owner)
184 self.owners_queue.remove(owner)
185 self.selected_owners.add(owner)
186 for file_name in filter(
187 lambda file_name: file_name in self.unreviewed_files,
188 self.owners_to_files[owner]):
189 self.unreviewed_files.remove(file_name)
190 self.reviewed_by[file_name] = owner
191 if findMandatoryOwners:
192 self.find_mandatory_owners()
193
194 def deselect_owner(self, owner, findMandatoryOwners=True):
195 if owner in self.selected_owners:
196 return
197 if owner in self.deselected_owners:
198 return
199 if not (owner in self.owners_queue):
200 return
Dirk Pranke 2013/09/16 23:56:11 Nit: ditto.
Bei Zhang 2013/09/19 23:21:43 Done.
201 self.writeln('Deselected: ' + owner)
202 self.owners_queue.remove(owner)
203 self.deselected_owners.add(owner)
204 for file_name in self.owners_to_files[owner] & self.unreviewed_files:
205 self.files_to_owners[file_name].remove(owner)
206 if findMandatoryOwners:
207 self.find_mandatory_owners()
208
209 def find_mandatory_owners(self):
210 continues = True
211 for owner in self.owners_queue:
212 if owner in self.selected_owners:
213 continue
214 if owner in self.deselected_owners:
215 continue
216 if len(self.owners_to_files[owner] & self.unreviewed_files) == 0:
217 self.deselect_owner(owner, False)
218
219 while continues:
220 continues = False
221 for file_name in filter(
222 lambda file_name: len(self.files_to_owners[file_name]) == 1,
223 self.unreviewed_files):
224 owner = first(self.files_to_owners[file_name])
225 self.select_owner(owner, False)
226 continues = True
227 break
Dirk Pranke 2013/09/16 23:56:11 I think you can re-work lines 219-227 to: single_
Bei Zhang 2013/09/19 23:21:43 There won't be such a problem because once self.se
Dirk Pranke 2013/09/20 02:06:40 I see. I suppose it could be the case that if you
228
229 def print_comments(self, owner):
230 if owner not in self.comments:
231 self.writeln(self.bold_name(owner))
232 else:
233 self.writeln(self.bold_name(owner) + ' is commented as:')
234 self.indent()
235 for path in self.comments[owner]:
236 if len(self.comments[owner][path]) > 0:
237 self.writeln(self.greyed(self.comments[owner][path]) +
238 ' (at ' + self.bold(path or '<root>') + ')')
239 else:
240 self.writeln(self.greyed('[No comment] ') + ' (at ' +
241 self.bold(path or '<root>') + ')')
242 self.unindent()
243
244 def print_file_info(self, file_name, except_owner=''):
245 if file_name not in self.unreviewed_files:
246 self.writeln(self.greyed(file_name +
247 ' (by ' +
248 self.bold_name(self.reviewed_by[file_name]) +
249 ')'))
250 else:
251 if len(self.files_to_owners[file_name]) <= 3:
252 other_owners = []
253 for ow in self.files_to_owners[file_name]:
254 if ow != except_owner:
255 other_owners.append(self.bold_name(ow))
256 self.writeln(file_name +
257 ' [' + (', '.join(other_owners)) + ']')
258 else:
259 self.writeln(file_name + ' [' +
260 self.bold(str(len(self.files_to_owners[file_name]))) +
261 ']')
262
263 def print_file_info_detailed(self, file_name):
264 self.writeln(file_name)
265 self.indent()
266 for ow in sorted(self.files_to_owners[file_name]):
267 if ow in self.deselected_owners:
268 self.writeln(self.bold_name(self.greyed(ow)))
269 elif ow in self.selected_owners:
270 self.writeln(self.bold_name(self.greyed(ow)))
271 else:
272 self.writeln(self.bold_name(ow))
273 self.unindent()
274
275 def print_owned_files_for(self, owner):
276 # Print owned files
277 self.print_comments(owner)
278 self.writeln(self.bold_name(owner) + ' owns ' +
279 str(len(self.owners_to_files[owner])) + ' file(s):')
280 self.indent()
281 for file_name in sorted(self.owners_to_files[owner]):
282 self.print_file_info(file_name, owner)
283 self.unindent()
284 self.writeln()
285
286 def list_owners(self, owners_queue):
287 if (len(self.owners_to_files) - len(self.deselected_owners) -
288 len(self.selected_owners)) > 3:
289 for ow in owners_queue:
290 if ow not in self.deselected_owners and ow not in self.selected_owners:
291 self.print_comments(ow)
292 else:
293 for ow in owners_queue:
294 if ow not in self.deselected_owners and ow not in self.selected_owners:
295 self.writeln()
296 self.print_owned_files_for(ow)
297
298 def list_files(self):
299 self.indent()
300 if len(self.unreviewed_files) > 5:
301 for file_name in sorted(self.unreviewed_files):
302 self.print_file_info(file_name)
303 else:
304 for file_name in self.unreviewed_files:
305 self.print_file_info_detailed(file_name)
306 self.unindent()
307
308 def pick_owner(self, ow):
309 # Allowing to omit domain suffixes
310 if ow not in self.owners_to_files:
311 if ow + self.email_postfix in self.owners_to_files:
312 ow += self.email_postfix
313
314 if ow not in self.owners_to_files:
315 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
316 'It\'s an invalid name or not related to the change list.')
317 return False
318 elif ow in self.selected_owners:
319 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
320 'It\'s already selected.')
321 return False
322 elif ow in self.deselected_owners:
323 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually.' +
324 'It\'s already unselected.')
325 return False
326
327 self.select_owner(ow)
328 return True
329
330 def print_result(self):
331 # Print results
332 self.writeln()
333 self.writeln()
334 self.writeln('** You selected these owners **')
335 self.writeln()
336 for owner in self.selected_owners:
337 self.writeln(self.bold_name(owner) + ':')
338 self.indent()
339 for file_name in sorted(self.owners_to_files[owner]):
340 self.writeln(file_name)
341 self.unindent()
342
343 def bold(self, text):
344 return self.COLOR_BOLD + text + self.COLOR_RESET
345
346 def bold_name(self, name):
347 return (self.COLOR_BOLD +
348 name.replace(self.email_postfix, '') + self.COLOR_RESET)
349
350 def greyed(self, text):
351 return self.COLOR_GREY + text + self.COLOR_RESET
352
353 def indent(self):
354 self.indentation += 1
355
356 def unindent(self):
357 self.indentation -= 1
358
359 def print_indent(self):
360 return ' ' * self.indentation
361
362 def writeln(self, text=''):
363 print self.print_indent() + text
364
365 def hr(self):
366 self.writeln('=====================')
367
368 def print_info(self, owner):
369 self.hr()
370 self.writeln(
371 self.bold(str(len(self.unreviewed_files))) + ' file(s) left.')
372 self.print_owned_files_for(owner)
373
374 def input_command(self, owner):
375 self.writeln('Add ' + self.bold_name(owner) + ' as your reviewer? ')
376 return raw_input(
377 '[yes/no/Defer/pick/files/owners/quit/restart]: ').lower()
OLDNEW
« git_cl.py ('K') | « owners.py ('k') | tests/owners_finder_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698