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