Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 5 | """Watchlists |
| 6 | |
| 7 | Watchlists is a mechanism that allow a developer (a "watcher") to watch over |
Chris Hall | ba97f60 | 2019-10-15 15:22:16 +0000 | [diff] [blame] | 8 | portions of code that they are interested in. A "watcher" will be cc-ed to |
| 9 | changes that modify that portion of code, thereby giving them an opportunity |
James Zern | fdd8946 | 2021-01-13 01:38:34 +0000 | [diff] [blame] | 10 | to make comments on chromium-review.googlesource.com even before the change is |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 11 | committed. |
James Zern | fdd8946 | 2021-01-13 01:38:34 +0000 | [diff] [blame] | 12 | Refer: |
| 13 | https://chromium.googlesource.com/chromium/src/+/HEAD/docs/infra/watchlists.md |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 14 | |
| 15 | When invoked directly from the base of a repository, this script lists out |
| 16 | the watchers for files given on the command line. This is useful to verify |
| 17 | changes to WATCHLISTS files. |
| 18 | """ |
| 19 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 22 | import logging |
| 23 | import os |
| 24 | import re |
| 25 | import sys |
| 26 | |
| 27 | |
| 28 | class Watchlists(object): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 29 | """Manage Watchlists. |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 30 | |
| 31 | This class provides mechanism to load watchlists for a repo and identify |
| 32 | watchers. |
| 33 | Usage: |
| 34 | wl = Watchlists("/path/to/repo/root") |
| 35 | watchers = wl.GetWatchersForPaths(["/path/to/file1", |
| 36 | "/path/to/file2",]) |
| 37 | """ |
| 38 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 39 | _RULES = "WATCHLISTS" |
| 40 | _RULES_FILENAME = _RULES |
| 41 | _repo_root = None |
| 42 | _defns = {} # Definitions |
| 43 | _path_regexps = {} # Name -> Regular expression mapping |
| 44 | _watchlists = {} # name to email mapping |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 45 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 46 | def __init__(self, repo_root): |
| 47 | self._repo_root = repo_root |
| 48 | self._LoadWatchlistRules() |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 49 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 50 | def _GetRulesFilePath(self): |
| 51 | """Returns path to WATCHLISTS file.""" |
| 52 | return os.path.join(self._repo_root, self._RULES_FILENAME) |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 53 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 54 | def _HasWatchlistsFile(self): |
| 55 | """Determine if watchlists are available for this repo.""" |
| 56 | return os.path.exists(self._GetRulesFilePath()) |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 57 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 58 | def _ContentsOfWatchlistsFile(self): |
| 59 | """Read the WATCHLISTS file and return its contents.""" |
| 60 | try: |
| 61 | watchlists_file = open(self._GetRulesFilePath()) |
| 62 | contents = watchlists_file.read() |
| 63 | watchlists_file.close() |
| 64 | return contents |
| 65 | except IOError as e: |
| 66 | logging.error("Cannot read %s: %s" % (self._GetRulesFilePath(), e)) |
| 67 | return '' |
nirnimesh@chromium.org | b82e409 | 2009-06-18 14:24:08 +0000 | [diff] [blame] | 68 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 69 | def _LoadWatchlistRules(self): |
| 70 | """Load watchlists from WATCHLISTS file. Does nothing if not present.""" |
| 71 | if not self._HasWatchlistsFile(): |
| 72 | return |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 73 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 74 | contents = self._ContentsOfWatchlistsFile() |
| 75 | watchlists_data = None |
| 76 | try: |
| 77 | watchlists_data = eval(contents, {'__builtins__': None}, None) |
| 78 | except SyntaxError as e: |
| 79 | logging.error("Cannot parse %s. %s" % (self._GetRulesFilePath(), e)) |
| 80 | return |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 81 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 82 | defns = watchlists_data.get("WATCHLIST_DEFINITIONS") |
| 83 | if not defns: |
| 84 | logging.error("WATCHLIST_DEFINITIONS not defined in %s" % |
| 85 | self._GetRulesFilePath()) |
| 86 | return |
| 87 | watchlists = watchlists_data.get("WATCHLISTS") |
| 88 | if not watchlists: |
| 89 | logging.error("WATCHLISTS not defined in %s" % |
| 90 | self._GetRulesFilePath()) |
| 91 | return |
| 92 | self._defns = defns |
| 93 | self._watchlists = watchlists |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 94 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 95 | # Compile the regular expressions ahead of time to avoid creating them |
| 96 | # on-the-fly multiple times per file. |
| 97 | self._path_regexps = {} |
| 98 | for name, rule in defns.items(): |
| 99 | filepath = rule.get('filepath') |
| 100 | if not filepath: |
| 101 | continue |
| 102 | self._path_regexps[name] = re.compile(filepath) |
Raphael Kubo da Costa | ae97943 | 2017-11-02 19:49:57 +0100 | [diff] [blame] | 103 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 104 | # Verify that all watchlist names are defined |
| 105 | for name in watchlists: |
| 106 | if name not in defns: |
| 107 | logging.error("%s not defined in %s" % |
| 108 | (name, self._GetRulesFilePath())) |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 109 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 110 | def GetWatchersForPaths(self, paths): |
| 111 | """Fetch the list of watchers for |paths| |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 112 | |
| 113 | Args: |
| 114 | paths: [path1, path2, ...] |
| 115 | |
| 116 | Returns: |
| 117 | [u1@chromium.org, u2@gmail.com, ...] |
| 118 | """ |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 119 | watchers = set() # A set, to avoid duplicates |
| 120 | for path in paths: |
| 121 | path = path.replace(os.sep, '/') |
| 122 | for name, rule in self._path_regexps.items(): |
| 123 | if name not in self._watchlists: |
| 124 | continue |
| 125 | if rule.search(path): |
| 126 | for watchlist in self._watchlists[name]: |
| 127 | watchers.add(watchlist) |
| 128 | return sorted(watchers) |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def main(argv): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 132 | # Confirm that watchlists can be parsed and spew out the watchers |
| 133 | if len(argv) < 2: |
| 134 | print("Usage (from the base of repo):") |
| 135 | print(" %s [file-1] [file-2] ...." % argv[0]) |
| 136 | return 1 |
| 137 | wl = Watchlists(os.getcwd()) |
| 138 | watchers = wl.GetWatchersForPaths(argv[1:]) |
| 139 | print(watchers) |
nirnimesh@chromium.org | b2ab494 | 2009-06-11 21:39:19 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 143 | main(sys.argv) |