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