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