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