blob: 18c42b8f519a62df53e3dcfa14bb6d722921ee70 [file] [log] [blame]
pam@chromium.orgf46aed92012-03-08 09:18:17 +00001# Copyright (c) 2012 The Chromium Authors. All rights reserved.
dpranke@chromium.org2a009622011-03-01 02:43:31 +00002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""A database of OWNERS files."""
6
dpranke@chromium.orgfdecfb72011-03-16 23:27:23 +00007import collections
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +00008import re
9
10
11# If this is present by itself on a line, this means that everyone can review.
12EVERYONE = '*'
13
14
15# Recognizes 'X@Y' email addresses. Very simplistic.
16BASIC_EMAIL_REGEXP = r'^[\w\-\+\%\.]+\@[\w\-\+\%\.]+$'
dpranke@chromium.org2a009622011-03-01 02:43:31 +000017
dpranke@chromium.org2a009622011-03-01 02:43:31 +000018
dpranke@chromium.org923950f2011-03-17 23:40:00 +000019def _assert_is_collection(obj):
dpranke@chromium.orge6a4ab32011-03-31 01:23:08 +000020 assert not isinstance(obj, basestring)
maruel@chromium.org725f1c32011-04-01 20:24:54 +000021 # Module 'collections' has no 'Iterable' member
22 # pylint: disable=E1101
dpranke@chromium.orge6a4ab32011-03-31 01:23:08 +000023 if hasattr(collections, 'Iterable') and hasattr(collections, 'Sized'):
24 assert (isinstance(obj, collections.Iterable) and
25 isinstance(obj, collections.Sized))
dpranke@chromium.org923950f2011-03-17 23:40:00 +000026
27
dpranke@chromium.org898a10e2011-03-04 21:54:43 +000028class SyntaxErrorInOwnersFile(Exception):
dpranke@chromium.org86bbf192011-03-09 21:37:06 +000029 def __init__(self, path, lineno, msg):
30 super(SyntaxErrorInOwnersFile, self).__init__((path, lineno, msg))
dpranke@chromium.org898a10e2011-03-04 21:54:43 +000031 self.path = path
dpranke@chromium.org86bbf192011-03-09 21:37:06 +000032 self.lineno = lineno
dpranke@chromium.org898a10e2011-03-04 21:54:43 +000033 self.msg = msg
34
35 def __str__(self):
dpranke@chromium.org86bbf192011-03-09 21:37:06 +000036 return "%s:%d syntax error: %s" % (self.path, self.lineno, self.msg)
dpranke@chromium.org898a10e2011-03-04 21:54:43 +000037
38
dpranke@chromium.org898a10e2011-03-04 21:54:43 +000039class Database(object):
40 """A database of OWNERS files for a repository.
41
42 This class allows you to find a suggested set of reviewers for a list
43 of changed files, and see if a list of changed files is covered by a
44 list of reviewers."""
45
46 def __init__(self, root, fopen, os_path):
47 """Args:
dpranke@chromium.org2a009622011-03-01 02:43:31 +000048 root: the path to the root of the Repository
dpranke@chromium.org2a009622011-03-01 02:43:31 +000049 open: function callback to open a text file for reading
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +000050 os_path: module/object callback with fields for 'abspath', 'dirname',
51 'exists', and 'join'
dpranke@chromium.org2a009622011-03-01 02:43:31 +000052 """
53 self.root = root
54 self.fopen = fopen
55 self.os_path = os_path
56
dpranke@chromium.org627ea672011-03-11 23:29:03 +000057 # Pick a default email regexp to use; callers can override as desired.
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +000058 self.email_regexp = re.compile(BASIC_EMAIL_REGEXP)
dpranke@chromium.org2a009622011-03-01 02:43:31 +000059
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +000060 # Mapping of owners to the paths they own.
61 self.owned_by = {EVERYONE: set()}
62
63 # Mapping of paths to authorized owners.
dpranke@chromium.org2a009622011-03-01 02:43:31 +000064 self.owners_for = {}
65
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +000066 # Set of paths that stop us from looking above them for owners.
67 # (This is implicitly true for the root directory).
68 self.stop_looking = set([''])
dpranke@chromium.org2a009622011-03-01 02:43:31 +000069
dpranke@chromium.org7eea2592011-03-09 21:35:46 +000070 def reviewers_for(self, files):
dpranke@chromium.orgfdecfb72011-03-16 23:27:23 +000071 """Returns a suggested set of reviewers that will cover the files.
dpranke@chromium.org2a009622011-03-01 02:43:31 +000072
dpranke@chromium.orgfdecfb72011-03-16 23:27:23 +000073 files is a sequence of paths relative to (and under) self.root."""
dpranke@chromium.org7eea2592011-03-09 21:35:46 +000074 self._check_paths(files)
75 self._load_data_needed_for(files)
76 return self._covering_set_of_owners_for(files)
dpranke@chromium.org2a009622011-03-01 02:43:31 +000077
pam@chromium.orgf46aed92012-03-08 09:18:17 +000078 def directories_not_covered_by(self, files, reviewers):
79 """Returns the set of directories that are not owned by a reviewer.
dpranke@chromium.org2a009622011-03-01 02:43:31 +000080
pam@chromium.orgf46aed92012-03-08 09:18:17 +000081 Determines which of the given files are not owned by at least one of the
82 reviewers, then returns a set containing the applicable enclosing
83 directories, i.e. the ones upward from the files that have OWNERS files.
dpranke@chromium.orgfdecfb72011-03-16 23:27:23 +000084
85 Args:
86 files is a sequence of paths relative to (and under) self.root.
pam@chromium.orgf46aed92012-03-08 09:18:17 +000087 reviewers is a sequence of strings matching self.email_regexp.
88 """
dpranke@chromium.org7eea2592011-03-09 21:35:46 +000089 self._check_paths(files)
90 self._check_reviewers(reviewers)
dpranke@chromium.org7eea2592011-03-09 21:35:46 +000091 self._load_data_needed_for(files)
pam@chromium.orgf46aed92012-03-08 09:18:17 +000092
93 dirs = set([self.os_path.dirname(f) for f in files])
dpranke@chromium.org7eea2592011-03-09 21:35:46 +000094 covered_dirs = self._dirs_covered_by(reviewers)
pam@chromium.orgf46aed92012-03-08 09:18:17 +000095 uncovered_dirs = [self._enclosing_dir_with_owners(d) for d in dirs
96 if not self._is_dir_covered_by(d, covered_dirs)]
97
98 return set(uncovered_dirs)
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +000099
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000100 def _check_paths(self, files):
101 def _is_under(f, pfx):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000102 return self.os_path.abspath(self.os_path.join(pfx, f)).startswith(pfx)
dpranke@chromium.org923950f2011-03-17 23:40:00 +0000103 _assert_is_collection(files)
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000104 assert all(_is_under(f, self.os_path.abspath(self.root)) for f in files)
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000105
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000106 def _check_reviewers(self, reviewers):
dpranke@chromium.org923950f2011-03-17 23:40:00 +0000107 _assert_is_collection(reviewers)
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000108 assert all(self.email_regexp.match(r) for r in reviewers)
109
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000110 def _dirs_covered_by(self, reviewers):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000111 dirs = self.owned_by[EVERYONE]
112 for r in reviewers:
113 dirs = dirs | self.owned_by.get(r, set())
114 return dirs
115
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000116 def _stop_looking(self, dirname):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000117 return dirname in self.stop_looking
118
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000119 def _is_dir_covered_by(self, dirname, covered_dirs):
120 while not dirname in covered_dirs and not self._stop_looking(dirname):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000121 dirname = self.os_path.dirname(dirname)
122 return dirname in covered_dirs
dpranke@chromium.org2a009622011-03-01 02:43:31 +0000123
pam@chromium.orgf46aed92012-03-08 09:18:17 +0000124 def _enclosing_dir_with_owners(self, directory):
125 """Returns the innermost enclosing directory that has an OWNERS file."""
126 dirpath = directory
127 while not dirpath in self.owners_for:
128 if self._stop_looking(dirpath):
129 break
130 dirpath = self.os_path.dirname(dirpath)
131 return dirpath
132
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000133 def _load_data_needed_for(self, files):
dpranke@chromium.org2a009622011-03-01 02:43:31 +0000134 for f in files:
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000135 dirpath = self.os_path.dirname(f)
136 while not dirpath in self.owners_for:
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000137 self._read_owners_in_dir(dirpath)
138 if self._stop_looking(dirpath):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000139 break
140 dirpath = self.os_path.dirname(dirpath)
dpranke@chromium.org2a009622011-03-01 02:43:31 +0000141
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000142 def _read_owners_in_dir(self, dirpath):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000143 owners_path = self.os_path.join(self.root, dirpath, 'OWNERS')
144 if not self.os_path.exists(owners_path):
145 return
dpranke@chromium.org2a009622011-03-01 02:43:31 +0000146
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000147 lineno = 0
148 for line in self.fopen(owners_path):
149 lineno += 1
150 line = line.strip()
bauerb@chromium.org20d19432011-06-08 16:34:18 +0000151 if line.startswith('#') or line == '':
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000152 continue
153 if line == 'set noparent':
154 self.stop_looking.add(dirpath)
155 continue
dpranke@chromium.org86bbf192011-03-09 21:37:06 +0000156 if line.startswith('set '):
157 raise SyntaxErrorInOwnersFile(owners_path, lineno,
158 'unknown option: "%s"' % line[4:].strip())
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000159 if self.email_regexp.match(line) or line == EVERYONE:
160 self.owned_by.setdefault(line, set()).add(dirpath)
161 self.owners_for.setdefault(dirpath, set()).add(line)
162 continue
dpranke@chromium.org86bbf192011-03-09 21:37:06 +0000163 raise SyntaxErrorInOwnersFile(owners_path, lineno,
164 ('line is not a comment, a "set" directive, '
165 'or an email address: "%s"' % line))
dpranke@chromium.org2a009622011-03-01 02:43:31 +0000166
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000167 def _covering_set_of_owners_for(self, files):
zork@chromium.org046e1752012-05-07 05:56:12 +0000168 # Get the set of directories from the files.
169 dirs = set()
dpranke@chromium.org2a009622011-03-01 02:43:31 +0000170 for f in files:
zork@chromium.org046e1752012-05-07 05:56:12 +0000171 dirs.add(self.os_path.dirname(f))
172
173 owned_dirs = {}
174 dir_owners = {}
175
176 for current_dir in dirs:
177 # Get the list of owners for each directory.
178 current_owners = set()
179 dirname = current_dir
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000180 while dirname in self.owners_for:
zork@chromium.org04704f72012-05-15 01:15:30 +0000181 current_owners |= self.owners_for[dirname]
dpranke@chromium.org7eea2592011-03-09 21:35:46 +0000182 if self._stop_looking(dirname):
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000183 break
zork@chromium.org04704f72012-05-15 01:15:30 +0000184 prev_parent = dirname
dpranke@chromium.org6dada4e2011-03-08 22:32:40 +0000185 dirname = self.os_path.dirname(dirname)
zork@chromium.org04704f72012-05-15 01:15:30 +0000186 if prev_parent == dirname:
187 break
zork@chromium.org046e1752012-05-07 05:56:12 +0000188
189 # Map each directory to a list of its owners.
190 dir_owners[current_dir] = current_owners
191
192 # Add the directory to the list of each owner.
193 for owner in current_owners:
zork@chromium.org04704f72012-05-15 01:15:30 +0000194 owned_dirs.setdefault(owner, set()).add(current_dir)
zork@chromium.org046e1752012-05-07 05:56:12 +0000195
196 final_owners = set()
197 while dirs:
198 # Find the owner that has the most directories.
199 max_count = 0
200 max_owner = None
201 owner_count = {}
202 for dirname in dirs:
203 for owner in dir_owners[dirname]:
204 count = owner_count.get(owner, 0) + 1
205 owner_count[owner] = count
206 if count >= max_count:
207 max_owner = owner
zork@chromium.org04704f72012-05-15 01:15:30 +0000208 max_count = count
zork@chromium.org046e1752012-05-07 05:56:12 +0000209
210 # If no more directories have OWNERS, we're done.
211 if not max_owner:
212 break
213
214 final_owners.add(max_owner)
215
216 # Remove all directories owned by the current owner from the remaining
217 # list.
218 for dirname in owned_dirs[max_owner]:
zork@chromium.org04704f72012-05-15 01:15:30 +0000219 dirs.discard(dirname)
zork@chromium.org046e1752012-05-07 05:56:12 +0000220
221 return final_owners