The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 15 | import multiprocessing |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import optparse |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 17 | import os |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 18 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 20 | from error import InvalidProjectGroupsError |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 21 | from error import NoSuchProjectError |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 22 | from error import RepoExitError |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 23 | from event_log import EventLog |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 24 | import progress |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 26 | |
Mike Frysinger | df8b1cb | 2021-07-26 15:59:20 -0400 | [diff] [blame] | 27 | # Are we generating man-pages? |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 28 | GENERATE_MANPAGES = os.environ.get("_REPO_GENERATE_MANPAGES_") == " indeed! " |
Mike Frysinger | df8b1cb | 2021-07-26 15:59:20 -0400 | [diff] [blame] | 29 | |
| 30 | |
Mike Frysinger | 7c87116 | 2021-02-16 01:45:39 -0500 | [diff] [blame] | 31 | # Number of projects to submit to a single worker process at a time. |
| 32 | # This number represents a tradeoff between the overhead of IPC and finer |
| 33 | # grained opportunity for parallelism. This particular value was chosen by |
| 34 | # iterating through powers of two until the overall performance no longer |
| 35 | # improved. The performance of this batch size is not a function of the |
| 36 | # number of cores on the system. |
| 37 | WORKER_BATCH_SIZE = 32 |
| 38 | |
| 39 | |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 40 | # How many jobs to run in parallel by default? This assumes the jobs are |
| 41 | # largely I/O bound and do not hit the network. |
| 42 | DEFAULT_LOCAL_JOBS = min(os.cpu_count(), 8) |
| 43 | |
| 44 | |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 45 | class UsageError(RepoExitError): |
| 46 | """Exception thrown with invalid command usage.""" |
| 47 | |
| 48 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 49 | class Command: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 50 | """Base class for any command line action in repo.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 52 | # Singleton for all commands to track overall repo command execution and |
| 53 | # provide event summary to callers. Only used by sync subcommand currently. |
| 54 | # |
| 55 | # NB: This is being replaced by git trace2 events. See git_trace2_event_log. |
| 56 | event_log = EventLog() |
Mike Frysinger | d88b369 | 2021-06-14 16:09:29 -0400 | [diff] [blame] | 57 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 58 | # Whether this command is a "common" one, i.e. whether the user would |
| 59 | # commonly use it or it's a more uncommon command. This is used by the help |
| 60 | # command to show short-vs-full summaries. |
| 61 | COMMON = False |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 62 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 63 | # Whether this command supports running in parallel. If greater than 0, |
| 64 | # it is the number of parallel jobs to default to. |
| 65 | PARALLEL_JOBS = None |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 66 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 67 | # Whether this command supports Multi-manifest. If False, then main.py will |
| 68 | # iterate over the manifests and invoke the command once per (sub)manifest. |
| 69 | # This is only checked after calling ValidateOptions, so that partially |
| 70 | # migrated subcommands can set it to False. |
| 71 | MULTI_MANIFEST_SUPPORT = True |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 72 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 73 | def __init__( |
| 74 | self, |
| 75 | repodir=None, |
| 76 | client=None, |
| 77 | manifest=None, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 78 | git_event_log=None, |
| 79 | outer_client=None, |
| 80 | outer_manifest=None, |
| 81 | ): |
| 82 | self.repodir = repodir |
| 83 | self.client = client |
| 84 | self.outer_client = outer_client or client |
| 85 | self.manifest = manifest |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 86 | self.git_event_log = git_event_log |
| 87 | self.outer_manifest = outer_manifest |
Mike Frysinger | d58d0dd | 2021-06-14 16:17:27 -0400 | [diff] [blame] | 88 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 89 | # Cache for the OptionParser property. |
| 90 | self._optparse = None |
Mike Frysinger | d58d0dd | 2021-06-14 16:17:27 -0400 | [diff] [blame] | 91 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 92 | def WantPager(self, _opt): |
| 93 | return False |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 94 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 95 | def ReadEnvironmentOptions(self, opts): |
| 96 | """Set options from environment variables.""" |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 97 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 98 | env_options = self._RegisteredEnvironmentOptions() |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 99 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 100 | for env_key, opt_key in env_options.items(): |
| 101 | # Get the user-set option value if any |
| 102 | opt_value = getattr(opts, opt_key) |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 103 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 104 | # If the value is set, it means the user has passed it as a command |
| 105 | # line option, and we should use that. Otherwise we can try to set |
| 106 | # it with the value from the corresponding environment variable. |
| 107 | if opt_value is not None: |
| 108 | continue |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 109 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 110 | env_value = os.environ.get(env_key) |
| 111 | if env_value is not None: |
| 112 | setattr(opts, opt_key, env_value) |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 113 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 114 | return opts |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 115 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 116 | @property |
| 117 | def OptionParser(self): |
| 118 | if self._optparse is None: |
| 119 | try: |
| 120 | me = "repo %s" % self.NAME |
| 121 | usage = self.helpUsage.strip().replace("%prog", me) |
| 122 | except AttributeError: |
| 123 | usage = "repo %s" % self.NAME |
| 124 | epilog = ( |
| 125 | "Run `repo help %s` to view the detailed manual." % self.NAME |
| 126 | ) |
| 127 | self._optparse = optparse.OptionParser(usage=usage, epilog=epilog) |
| 128 | self._CommonOptions(self._optparse) |
| 129 | self._Options(self._optparse) |
| 130 | return self._optparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 132 | def _CommonOptions(self, p, opt_v=True): |
| 133 | """Initialize the option parser with common options. |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 134 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 135 | These will show up for *all* subcommands, so use sparingly. |
| 136 | NB: Keep in sync with repo:InitParser(). |
| 137 | """ |
| 138 | g = p.add_option_group("Logging options") |
| 139 | opts = ["-v"] if opt_v else [] |
| 140 | g.add_option( |
| 141 | *opts, |
| 142 | "--verbose", |
| 143 | dest="output_mode", |
| 144 | action="store_true", |
| 145 | help="show all output", |
| 146 | ) |
| 147 | g.add_option( |
| 148 | "-q", |
| 149 | "--quiet", |
| 150 | dest="output_mode", |
| 151 | action="store_false", |
| 152 | help="only show errors", |
| 153 | ) |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 154 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 155 | if self.PARALLEL_JOBS is not None: |
| 156 | default = "based on number of CPU cores" |
| 157 | if not GENERATE_MANPAGES: |
| 158 | # Only include active cpu count if we aren't generating man |
| 159 | # pages. |
| 160 | default = f"%default; {default}" |
| 161 | p.add_option( |
| 162 | "-j", |
| 163 | "--jobs", |
| 164 | type=int, |
| 165 | default=self.PARALLEL_JOBS, |
| 166 | help=f"number of jobs to run in parallel (default: {default})", |
| 167 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 168 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 169 | m = p.add_option_group("Multi-manifest options") |
| 170 | m.add_option( |
| 171 | "--outer-manifest", |
| 172 | action="store_true", |
| 173 | default=None, |
| 174 | help="operate starting at the outermost manifest", |
| 175 | ) |
| 176 | m.add_option( |
| 177 | "--no-outer-manifest", |
| 178 | dest="outer_manifest", |
| 179 | action="store_false", |
| 180 | help="do not operate on outer manifests", |
| 181 | ) |
| 182 | m.add_option( |
| 183 | "--this-manifest-only", |
| 184 | action="store_true", |
| 185 | default=None, |
| 186 | help="only operate on this (sub)manifest", |
| 187 | ) |
| 188 | m.add_option( |
| 189 | "--no-this-manifest-only", |
| 190 | "--all-manifests", |
| 191 | dest="this_manifest_only", |
| 192 | action="store_false", |
| 193 | help="operate on this manifest and its submanifests", |
| 194 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 195 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 196 | def _Options(self, p): |
| 197 | """Initialize the option parser with subcommand-specific options.""" |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 198 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 199 | def _RegisteredEnvironmentOptions(self): |
| 200 | """Get options that can be set from environment variables. |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 201 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 202 | Return a dictionary mapping environment variable name |
| 203 | to option key name that it can override. |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 204 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 205 | Example: {'REPO_MY_OPTION': 'my_option'} |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 206 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 207 | Will allow the option with key value 'my_option' to be set |
| 208 | from the value in the environment variable named 'REPO_MY_OPTION'. |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 209 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 210 | Note: This does not work properly for options that are explicitly |
| 211 | set to None by the user, or options that are defined with a |
| 212 | default value other than None. |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 213 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 214 | """ |
| 215 | return {} |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 216 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 217 | def Usage(self): |
| 218 | """Display usage and terminate.""" |
| 219 | self.OptionParser.print_usage() |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 220 | raise UsageError() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 222 | def CommonValidateOptions(self, opt, args): |
| 223 | """Validate common options.""" |
| 224 | opt.quiet = opt.output_mode is False |
| 225 | opt.verbose = opt.output_mode is True |
| 226 | if opt.outer_manifest is None: |
| 227 | # By default, treat multi-manifest instances as a single manifest |
| 228 | # from the user's perspective. |
| 229 | opt.outer_manifest = True |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 230 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 231 | def ValidateOptions(self, opt, args): |
| 232 | """Validate the user options & arguments before executing. |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 233 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 234 | This is meant to help break the code up into logical steps. Some tips: |
| 235 | * Use self.OptionParser.error to display CLI related errors. |
| 236 | * Adjust opt member defaults as makes sense. |
| 237 | * Adjust the args list, but do so inplace so the caller sees updates. |
| 238 | * Try to avoid updating self state. Leave that to Execute. |
| 239 | """ |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 240 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 241 | def Execute(self, opt, args): |
| 242 | """Perform the action, after option parsing is complete.""" |
| 243 | raise NotImplementedError |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 244 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 245 | @staticmethod |
| 246 | def ExecuteInParallel( |
| 247 | jobs, func, inputs, callback, output=None, ordered=False |
| 248 | ): |
| 249 | """Helper for managing parallel execution boiler plate. |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 250 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 251 | For subcommands that can easily split their work up. |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 252 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 253 | Args: |
| 254 | jobs: How many parallel processes to use. |
| 255 | func: The function to apply to each of the |inputs|. Usually a |
| 256 | functools.partial for wrapping additional arguments. It will be |
| 257 | run in a separate process, so it must be pickalable, so nested |
| 258 | functions won't work. Methods on the subcommand Command class |
| 259 | should work. |
| 260 | inputs: The list of items to process. Must be a list. |
| 261 | callback: The function to pass the results to for processing. It |
| 262 | will be executed in the main thread and process the results of |
| 263 | |func| as they become available. Thus it may be a local nested |
| 264 | function. Its return value is passed back directly. It takes |
| 265 | three arguments: |
| 266 | - The processing pool (or None with one job). |
| 267 | - The |output| argument. |
| 268 | - An iterator for the results. |
| 269 | output: An output manager. May be progress.Progess or |
| 270 | color.Coloring. |
| 271 | ordered: Whether the jobs should be processed in order. |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 272 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 273 | Returns: |
| 274 | The |callback| function's results are returned. |
| 275 | """ |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 276 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 277 | # NB: Multiprocessing is heavy, so don't spin it up for one job. |
| 278 | if len(inputs) == 1 or jobs == 1: |
| 279 | return callback(None, output, (func(x) for x in inputs)) |
| 280 | else: |
| 281 | with multiprocessing.Pool(jobs) as pool: |
| 282 | submit = pool.imap if ordered else pool.imap_unordered |
| 283 | return callback( |
| 284 | pool, |
| 285 | output, |
| 286 | submit(func, inputs, chunksize=WORKER_BATCH_SIZE), |
| 287 | ) |
| 288 | finally: |
| 289 | if isinstance(output, progress.Progress): |
| 290 | output.end() |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 291 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 292 | def _ResetPathToProjectMap(self, projects): |
| 293 | self._by_path = dict((p.worktree, p) for p in projects) |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 294 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 295 | def _UpdatePathToProjectMap(self, project): |
| 296 | self._by_path[project.worktree] = project |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 297 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 298 | def _GetProjectByPath(self, manifest, path): |
| 299 | project = None |
| 300 | if os.path.exists(path): |
| 301 | oldpath = None |
| 302 | while path and path != oldpath and path != manifest.topdir: |
| 303 | try: |
| 304 | project = self._by_path[path] |
| 305 | break |
| 306 | except KeyError: |
| 307 | oldpath = path |
| 308 | path = os.path.dirname(path) |
| 309 | if not project and path == manifest.topdir: |
| 310 | try: |
| 311 | project = self._by_path[path] |
| 312 | except KeyError: |
| 313 | pass |
| 314 | else: |
| 315 | try: |
| 316 | project = self._by_path[path] |
| 317 | except KeyError: |
| 318 | pass |
| 319 | return project |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 320 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 321 | def GetProjects( |
| 322 | self, |
| 323 | args, |
| 324 | manifest=None, |
| 325 | groups="", |
| 326 | missing_ok=False, |
| 327 | submodules_ok=False, |
| 328 | all_manifests=False, |
| 329 | ): |
| 330 | """A list of projects that match the arguments. |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 331 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 332 | Args: |
| 333 | args: a list of (case-insensitive) strings, projects to search for. |
| 334 | manifest: an XmlManifest, the manifest to use, or None for default. |
| 335 | groups: a string, the manifest groups in use. |
| 336 | missing_ok: a boolean, whether to allow missing projects. |
| 337 | submodules_ok: a boolean, whether to allow submodules. |
| 338 | all_manifests: a boolean, if True then all manifests and |
| 339 | submanifests are used. If False, then only the local |
| 340 | (sub)manifest is used. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 341 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 342 | Returns: |
| 343 | A list of matching Project instances. |
| 344 | """ |
| 345 | if all_manifests: |
| 346 | if not manifest: |
| 347 | manifest = self.manifest.outer_client |
| 348 | all_projects_list = manifest.all_projects |
| 349 | else: |
| 350 | if not manifest: |
| 351 | manifest = self.manifest |
| 352 | all_projects_list = manifest.projects |
| 353 | result = [] |
| 354 | |
| 355 | if not groups: |
| 356 | groups = manifest.GetGroupsStr() |
| 357 | groups = [x for x in re.split(r"[,\s]+", groups) if x] |
| 358 | |
| 359 | if not args: |
| 360 | derived_projects = {} |
| 361 | for project in all_projects_list: |
| 362 | if submodules_ok or project.sync_s: |
| 363 | derived_projects.update( |
| 364 | (p.name, p) for p in project.GetDerivedSubprojects() |
| 365 | ) |
| 366 | all_projects_list.extend(derived_projects.values()) |
| 367 | for project in all_projects_list: |
| 368 | if (missing_ok or project.Exists) and project.MatchesGroups( |
| 369 | groups |
| 370 | ): |
| 371 | result.append(project) |
| 372 | else: |
| 373 | self._ResetPathToProjectMap(all_projects_list) |
| 374 | |
| 375 | for arg in args: |
| 376 | # We have to filter by manifest groups in case the requested |
| 377 | # project is checked out multiple times or differently based on |
| 378 | # them. |
| 379 | projects = [ |
| 380 | project |
Sergiy Belozorov | 78e82ec | 2023-01-05 18:57:31 +0100 | [diff] [blame] | 381 | for project in manifest.GetProjectsWithName( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 382 | arg, all_manifests=all_manifests |
| 383 | ) |
| 384 | if project.MatchesGroups(groups) |
| 385 | ] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 386 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 387 | if not projects: |
| 388 | path = os.path.abspath(arg).replace("\\", "/") |
| 389 | tree = manifest |
| 390 | if all_manifests: |
| 391 | # Look for the deepest matching submanifest. |
| 392 | for tree in reversed(list(manifest.all_manifests)): |
| 393 | if path.startswith(tree.topdir): |
| 394 | break |
| 395 | project = self._GetProjectByPath(tree, path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 396 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 397 | # If it's not a derived project, update path->project |
| 398 | # mapping and search again, as arg might actually point to |
| 399 | # a derived subproject. |
| 400 | if ( |
| 401 | project |
| 402 | and not project.Derived |
| 403 | and (submodules_ok or project.sync_s) |
| 404 | ): |
| 405 | search_again = False |
| 406 | for subproject in project.GetDerivedSubprojects(): |
| 407 | self._UpdatePathToProjectMap(subproject) |
| 408 | search_again = True |
| 409 | if search_again: |
| 410 | project = ( |
| 411 | self._GetProjectByPath(manifest, path) |
| 412 | or project |
| 413 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 414 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 415 | if project: |
| 416 | projects = [project] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 417 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 418 | if not projects: |
| 419 | raise NoSuchProjectError(arg) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 420 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 421 | for project in projects: |
| 422 | if not missing_ok and not project.Exists: |
| 423 | raise NoSuchProjectError( |
| 424 | "%s (%s)" |
| 425 | % (arg, project.RelPath(local=not all_manifests)) |
| 426 | ) |
| 427 | if not project.MatchesGroups(groups): |
| 428 | raise InvalidProjectGroupsError(arg) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 429 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 430 | result.extend(projects) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 431 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 432 | def _getpath(x): |
| 433 | return x.relpath |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 434 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 435 | result.sort(key=_getpath) |
| 436 | return result |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 437 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 438 | def FindProjects(self, args, inverse=False, all_manifests=False): |
| 439 | """Find projects from command line arguments. |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 440 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 441 | Args: |
| 442 | args: a list of (case-insensitive) strings, projects to search for. |
| 443 | inverse: a boolean, if True, then projects not matching any |args| |
| 444 | are returned. |
| 445 | all_manifests: a boolean, if True then all manifests and |
| 446 | submanifests are used. If False, then only the local |
| 447 | (sub)manifest is used. |
| 448 | """ |
| 449 | result = [] |
| 450 | patterns = [re.compile(r"%s" % a, re.IGNORECASE) for a in args] |
| 451 | for project in self.GetProjects("", all_manifests=all_manifests): |
| 452 | paths = [project.name, project.RelPath(local=not all_manifests)] |
| 453 | for pattern in patterns: |
| 454 | match = any(pattern.search(x) for x in paths) |
| 455 | if not inverse and match: |
| 456 | result.append(project) |
| 457 | break |
| 458 | if inverse and match: |
| 459 | break |
| 460 | else: |
| 461 | if inverse: |
| 462 | result.append(project) |
| 463 | result.sort( |
| 464 | key=lambda project: (project.manifest.path_prefix, project.relpath) |
| 465 | ) |
| 466 | return result |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 467 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 468 | def ManifestList(self, opt): |
| 469 | """Yields all of the manifests to traverse. |
| 470 | |
| 471 | Args: |
| 472 | opt: The command options. |
| 473 | """ |
| 474 | top = self.outer_manifest |
| 475 | if not opt.outer_manifest or opt.this_manifest_only: |
| 476 | top = self.manifest |
| 477 | yield top |
| 478 | if not opt.this_manifest_only: |
Jason R. Coombs | 8dd8521 | 2023-10-20 06:48:20 -0400 | [diff] [blame^] | 479 | yield from top.all_children |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 480 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 481 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | class InteractiveCommand(Command): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 483 | """Command which requires user interaction on the tty and must not run |
| 484 | within a pager, even if the user asks to. |
| 485 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 486 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 487 | def WantPager(self, _opt): |
| 488 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 489 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 490 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 491 | class PagedCommand(Command): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 492 | """Command which defaults to output in a pager, as its display tends to be |
| 493 | larger than one screen full. |
| 494 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 495 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 496 | def WantPager(self, _opt): |
| 497 | return True |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 498 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 499 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 500 | class MirrorSafeCommand: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 501 | """Command permits itself to run within a mirror, and does not require a |
| 502 | working directory. |
| 503 | """ |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 504 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 505 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 506 | class GitcClientCommand: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 507 | """Command that requires the local client to be a GITC client.""" |