blob: afc364033ef36dc86c0541db3a31ff7202812d78 [file] [log] [blame]
[email protected]faf3fdf2013-09-20 02:11:481# Copyright 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
7import os
8import copy
9import owners as owners_module
10
11
12def first(iterable):
13 for element in iterable:
14 return element
15
16
17class OwnersFinder(object):
18 COLOR_LINK = '\033[4m'
19 COLOR_BOLD = '\033[1;32m'
20 COLOR_GREY = '\033[0;37m'
21 COLOR_RESET = '\033[0m'
22
23 indentation = 0
24
Jochen Eisingereb744762017-04-05 09:00:0525 def __init__(self, files, local_root, author,
dtu944b6052016-07-14 21:48:2126 fopen, os_path,
[email protected]faf3fdf2013-09-20 02:11:4827 email_postfix='@chromium.org',
Jochen Eisingerd0573ec2017-04-13 08:55:0628 disable_color=False,
29 override_files=None):
[email protected]faf3fdf2013-09-20 02:11:4830 self.email_postfix = email_postfix
31
32 if os.name == 'nt' or disable_color:
33 self.COLOR_LINK = ''
34 self.COLOR_BOLD = ''
35 self.COLOR_GREY = ''
36 self.COLOR_RESET = ''
37
Jochen Eisingereb744762017-04-05 09:00:0538 self.db = owners_module.Database(local_root, fopen, os_path)
Jochen Eisingerd0573ec2017-04-13 08:55:0639 self.db.override_files = override_files or {}
[email protected]faf3fdf2013-09-20 02:11:4840 self.db.load_data_needed_for(files)
41
42 self.os_path = os_path
43
44 self.author = author
45
46 filtered_files = files
47
dtu944b6052016-07-14 21:48:2148 # Eliminate files that the author can review.
49 filtered_files = list(self.db.files_not_covered_by(
50 filtered_files, [author] if author else []))
[email protected]faf3fdf2013-09-20 02:11:4851
52 # If some files are eliminated.
53 if len(filtered_files) != len(files):
54 files = filtered_files
55 # Reload the database.
Jochen Eisingereb744762017-04-05 09:00:0556 self.db = owners_module.Database(local_root, fopen, os_path)
Jochen Eisingerd0573ec2017-04-13 08:55:0657 self.db.override_files = override_files or {}
[email protected]faf3fdf2013-09-20 02:11:4858 self.db.load_data_needed_for(files)
59
60 self.all_possible_owners = self.db.all_possible_owners(files, None)
61
62 self.owners_to_files = {}
63 self._map_owners_to_files(files)
64
65 self.files_to_owners = {}
66 self._map_files_to_owners()
67
68 self.owners_score = self.db.total_costs_by_owner(
69 self.all_possible_owners, files)
70
71 self.original_files_to_owners = copy.deepcopy(self.files_to_owners)
72 self.comments = self.db.comments
73
74 # This is the queue that will be shown in the interactive questions.
75 # It is initially sorted by the score in descending order. In the
76 # interactive questions a user can choose to "defer" its decision, then the
77 # owner will be put to the end of the queue and shown later.
78 self.owners_queue = []
79
80 self.unreviewed_files = set()
81 self.reviewed_by = {}
82 self.selected_owners = set()
83 self.deselected_owners = set()
84 self.reset()
85
86 def run(self):
87 self.reset()
88 while self.owners_queue and self.unreviewed_files:
89 owner = self.owners_queue[0]
90
91 if (owner in self.selected_owners) or (owner in self.deselected_owners):
92 continue
93
94 if not any((file_name in self.unreviewed_files)
95 for file_name in self.owners_to_files[owner]):
96 self.deselect_owner(owner)
97 continue
98
99 self.print_info(owner)
100
101 while True:
102 inp = self.input_command(owner)
103 if inp == 'y' or inp == 'yes':
104 self.select_owner(owner)
105 break
106 elif inp == 'n' or inp == 'no':
107 self.deselect_owner(owner)
108 break
109 elif inp == '' or inp == 'd' or inp == 'defer':
110 self.owners_queue.append(self.owners_queue.pop(0))
111 break
112 elif inp == 'f' or inp == 'files':
113 self.list_files()
114 break
115 elif inp == 'o' or inp == 'owners':
116 self.list_owners(self.owners_queue)
117 break
118 elif inp == 'p' or inp == 'pick':
119 self.pick_owner(raw_input('Pick an owner: '))
120 break
121 elif inp.startswith('p ') or inp.startswith('pick '):
122 self.pick_owner(inp.split(' ', 2)[1].strip())
123 break
124 elif inp == 'r' or inp == 'restart':
125 self.reset()
126 break
127 elif inp == 'q' or inp == 'quit':
128 # Exit with error
129 return 1
130
131 self.print_result()
132 return 0
133
134 def _map_owners_to_files(self, files):
135 for owner in self.all_possible_owners:
136 for dir_name, _ in self.all_possible_owners[owner]:
137 for file_name in files:
138 if file_name.startswith(dir_name):
139 self.owners_to_files.setdefault(owner, set())
140 self.owners_to_files[owner].add(file_name)
141
142 def _map_files_to_owners(self):
143 for owner in self.owners_to_files:
144 for file_name in self.owners_to_files[owner]:
145 self.files_to_owners.setdefault(file_name, set())
146 self.files_to_owners[file_name].add(owner)
147
148 def reset(self):
149 self.files_to_owners = copy.deepcopy(self.original_files_to_owners)
150 self.unreviewed_files = set(self.files_to_owners.keys())
151 self.reviewed_by = {}
152 self.selected_owners = set()
153 self.deselected_owners = set()
154
155 # Initialize owners queue, sort it by the score
156 self.owners_queue = list(sorted(self.owners_to_files.keys(),
157 key=lambda owner: self.owners_score[owner]))
158 self.find_mandatory_owners()
159
160 def select_owner(self, owner, findMandatoryOwners=True):
161 if owner in self.selected_owners or owner in self.deselected_owners\
162 or not (owner in self.owners_queue):
163 return
164 self.writeln('Selected: ' + owner)
165 self.owners_queue.remove(owner)
166 self.selected_owners.add(owner)
167 for file_name in filter(
168 lambda file_name: file_name in self.unreviewed_files,
169 self.owners_to_files[owner]):
170 self.unreviewed_files.remove(file_name)
171 self.reviewed_by[file_name] = owner
172 if findMandatoryOwners:
173 self.find_mandatory_owners()
174
175 def deselect_owner(self, owner, findMandatoryOwners=True):
176 if owner in self.selected_owners or owner in self.deselected_owners\
177 or not (owner in self.owners_queue):
178 return
179 self.writeln('Deselected: ' + owner)
180 self.owners_queue.remove(owner)
181 self.deselected_owners.add(owner)
182 for file_name in self.owners_to_files[owner] & self.unreviewed_files:
183 self.files_to_owners[file_name].remove(owner)
184 if findMandatoryOwners:
185 self.find_mandatory_owners()
186
187 def find_mandatory_owners(self):
188 continues = True
189 for owner in self.owners_queue:
190 if owner in self.selected_owners:
191 continue
192 if owner in self.deselected_owners:
193 continue
194 if len(self.owners_to_files[owner] & self.unreviewed_files) == 0:
195 self.deselect_owner(owner, False)
196
197 while continues:
198 continues = False
199 for file_name in filter(
200 lambda file_name: len(self.files_to_owners[file_name]) == 1,
201 self.unreviewed_files):
202 owner = first(self.files_to_owners[file_name])
203 self.select_owner(owner, False)
204 continues = True
205 break
206
207 def print_comments(self, owner):
208 if owner not in self.comments:
209 self.writeln(self.bold_name(owner))
210 else:
211 self.writeln(self.bold_name(owner) + ' is commented as:')
212 self.indent()
Jochen Eisinger72606f82017-04-04 08:44:18213 if owners_module.GLOBAL_STATUS in self.comments[owner]:
214 self.writeln(
215 self.greyed(self.comments[owner][owners_module.GLOBAL_STATUS]) +
216 ' (global status)')
217 if len(self.comments[owner]) == 1:
218 self.unindent()
219 return
[email protected]faf3fdf2013-09-20 02:11:48220 for path in self.comments[owner]:
Jochen Eisinger72606f82017-04-04 08:44:18221 if path == owners_module.GLOBAL_STATUS:
222 continue
223 elif len(self.comments[owner][path]) > 0:
[email protected]faf3fdf2013-09-20 02:11:48224 self.writeln(self.greyed(self.comments[owner][path]) +
225 ' (at ' + self.bold(path or '<root>') + ')')
226 else:
227 self.writeln(self.greyed('[No comment] ') + ' (at ' +
228 self.bold(path or '<root>') + ')')
229 self.unindent()
230
231 def print_file_info(self, file_name, except_owner=''):
232 if file_name not in self.unreviewed_files:
233 self.writeln(self.greyed(file_name +
234 ' (by ' +
235 self.bold_name(self.reviewed_by[file_name]) +
236 ')'))
237 else:
238 if len(self.files_to_owners[file_name]) <= 3:
239 other_owners = []
240 for ow in self.files_to_owners[file_name]:
241 if ow != except_owner:
242 other_owners.append(self.bold_name(ow))
243 self.writeln(file_name +
244 ' [' + (', '.join(other_owners)) + ']')
245 else:
246 self.writeln(file_name + ' [' +
247 self.bold(str(len(self.files_to_owners[file_name]))) +
248 ']')
249
250 def print_file_info_detailed(self, file_name):
251 self.writeln(file_name)
252 self.indent()
253 for ow in sorted(self.files_to_owners[file_name]):
254 if ow in self.deselected_owners:
255 self.writeln(self.bold_name(self.greyed(ow)))
256 elif ow in self.selected_owners:
257 self.writeln(self.bold_name(self.greyed(ow)))
258 else:
259 self.writeln(self.bold_name(ow))
260 self.unindent()
261
262 def print_owned_files_for(self, owner):
263 # Print owned files
264 self.print_comments(owner)
265 self.writeln(self.bold_name(owner) + ' owns ' +
266 str(len(self.owners_to_files[owner])) + ' file(s):')
267 self.indent()
268 for file_name in sorted(self.owners_to_files[owner]):
269 self.print_file_info(file_name, owner)
270 self.unindent()
271 self.writeln()
272
273 def list_owners(self, owners_queue):
274 if (len(self.owners_to_files) - len(self.deselected_owners) -
275 len(self.selected_owners)) > 3:
276 for ow in owners_queue:
277 if ow not in self.deselected_owners and ow not in self.selected_owners:
278 self.print_comments(ow)
279 else:
280 for ow in owners_queue:
281 if ow not in self.deselected_owners and ow not in self.selected_owners:
282 self.writeln()
283 self.print_owned_files_for(ow)
284
285 def list_files(self):
286 self.indent()
287 if len(self.unreviewed_files) > 5:
288 for file_name in sorted(self.unreviewed_files):
289 self.print_file_info(file_name)
290 else:
291 for file_name in self.unreviewed_files:
292 self.print_file_info_detailed(file_name)
293 self.unindent()
294
295 def pick_owner(self, ow):
296 # Allowing to omit domain suffixes
297 if ow not in self.owners_to_files:
298 if ow + self.email_postfix in self.owners_to_files:
299 ow += self.email_postfix
300
301 if ow not in self.owners_to_files:
302 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
303 'It\'s an invalid name or not related to the change list.')
304 return False
305 elif ow in self.selected_owners:
306 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
307 'It\'s already selected.')
308 return False
309 elif ow in self.deselected_owners:
310 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually.' +
311 'It\'s already unselected.')
312 return False
313
314 self.select_owner(ow)
315 return True
316
317 def print_result(self):
318 # Print results
319 self.writeln()
320 self.writeln()
321 self.writeln('** You selected these owners **')
322 self.writeln()
323 for owner in self.selected_owners:
324 self.writeln(self.bold_name(owner) + ':')
325 self.indent()
326 for file_name in sorted(self.owners_to_files[owner]):
327 self.writeln(file_name)
328 self.unindent()
329
330 def bold(self, text):
331 return self.COLOR_BOLD + text + self.COLOR_RESET
332
333 def bold_name(self, name):
334 return (self.COLOR_BOLD +
335 name.replace(self.email_postfix, '') + self.COLOR_RESET)
336
337 def greyed(self, text):
338 return self.COLOR_GREY + text + self.COLOR_RESET
339
340 def indent(self):
341 self.indentation += 1
342
343 def unindent(self):
344 self.indentation -= 1
345
346 def print_indent(self):
347 return ' ' * self.indentation
348
349 def writeln(self, text=''):
350 print self.print_indent() + text
351
352 def hr(self):
353 self.writeln('=====================')
354
355 def print_info(self, owner):
356 self.hr()
357 self.writeln(
358 self.bold(str(len(self.unreviewed_files))) + ' file(s) left.')
359 self.print_owned_files_for(owner)
360
361 def input_command(self, owner):
362 self.writeln('Add ' + self.bold_name(owner) + ' as your reviewer? ')
363 return raw_input(
364 '[yes/no/Defer/pick/files/owners/quit/restart]: ').lower()