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