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 | |
| 15 | import os |
| 16 | import optparse |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 17 | import platform |
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 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 25 | |
Mike Frysinger | 7c87116 | 2021-02-16 01:45:39 -0500 | [diff] [blame] | 26 | # Number of projects to submit to a single worker process at a time. |
| 27 | # This number represents a tradeoff between the overhead of IPC and finer |
| 28 | # grained opportunity for parallelism. This particular value was chosen by |
| 29 | # iterating through powers of two until the overall performance no longer |
| 30 | # improved. The performance of this batch size is not a function of the |
| 31 | # number of cores on the system. |
| 32 | WORKER_BATCH_SIZE = 32 |
| 33 | |
| 34 | |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 35 | # How many jobs to run in parallel by default? This assumes the jobs are |
| 36 | # largely I/O bound and do not hit the network. |
| 37 | DEFAULT_LOCAL_JOBS = min(os.cpu_count(), 8) |
| 38 | |
| 39 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | class Command(object): |
| 41 | """Base class for any command line action in repo. |
| 42 | """ |
| 43 | |
| 44 | common = False |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 45 | event_log = EventLog() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | manifest = None |
| 47 | _optparse = None |
| 48 | |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 49 | # Whether this command supports running in parallel. If greater than 0, |
| 50 | # it is the number of parallel jobs to default to. |
| 51 | PARALLEL_JOBS = None |
| 52 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 53 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 54 | return False |
| 55 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 56 | def ReadEnvironmentOptions(self, opts): |
| 57 | """ Set options from environment variables. """ |
| 58 | |
| 59 | env_options = self._RegisteredEnvironmentOptions() |
| 60 | |
| 61 | for env_key, opt_key in env_options.items(): |
| 62 | # Get the user-set option value if any |
| 63 | opt_value = getattr(opts, opt_key) |
| 64 | |
| 65 | # If the value is set, it means the user has passed it as a command |
| 66 | # line option, and we should use that. Otherwise we can try to set it |
| 67 | # with the value from the corresponding environment variable. |
| 68 | if opt_value is not None: |
| 69 | continue |
| 70 | |
| 71 | env_value = os.environ.get(env_key) |
| 72 | if env_value is not None: |
| 73 | setattr(opts, opt_key, env_value) |
| 74 | |
| 75 | return opts |
| 76 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | @property |
| 78 | def OptionParser(self): |
| 79 | if self._optparse is None: |
| 80 | try: |
| 81 | me = 'repo %s' % self.NAME |
| 82 | usage = self.helpUsage.strip().replace('%prog', me) |
| 83 | except AttributeError: |
| 84 | usage = 'repo %s' % self.NAME |
Mike Frysinger | 72ebf19 | 2020-02-19 01:20:18 -0500 | [diff] [blame] | 85 | epilog = 'Run `repo help %s` to view the detailed manual.' % self.NAME |
| 86 | self._optparse = optparse.OptionParser(usage=usage, epilog=epilog) |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame^] | 87 | self._CommonOptions(self._optparse) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | self._Options(self._optparse) |
| 89 | return self._optparse |
| 90 | |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame^] | 91 | def _CommonOptions(self, p, opt_v=True): |
| 92 | """Initialize the option parser with common options. |
| 93 | |
| 94 | These will show up for *all* subcommands, so use sparingly. |
| 95 | NB: Keep in sync with repo:InitParser(). |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | """ |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame^] | 97 | g = p.add_option_group('Logging options') |
| 98 | opts = ['-v'] if opt_v else [] |
| 99 | g.add_option(*opts, '--verbose', |
| 100 | dest='output_mode', action='store_true', |
| 101 | help='show all output') |
| 102 | g.add_option('-q', '--quiet', |
| 103 | dest='output_mode', action='store_false', |
| 104 | help='only show errors') |
| 105 | |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 106 | if self.PARALLEL_JOBS is not None: |
| 107 | p.add_option( |
| 108 | '-j', '--jobs', |
| 109 | type=int, default=self.PARALLEL_JOBS, |
| 110 | help='number of jobs to run in parallel (default: %s)' % self.PARALLEL_JOBS) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame^] | 112 | def _Options(self, p): |
| 113 | """Initialize the option parser with subcommand-specific options.""" |
| 114 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 115 | def _RegisteredEnvironmentOptions(self): |
| 116 | """Get options that can be set from environment variables. |
| 117 | |
| 118 | Return a dictionary mapping environment variable name |
| 119 | to option key name that it can override. |
| 120 | |
| 121 | Example: {'REPO_MY_OPTION': 'my_option'} |
| 122 | |
| 123 | Will allow the option with key value 'my_option' to be set |
| 124 | from the value in the environment variable named 'REPO_MY_OPTION'. |
| 125 | |
| 126 | Note: This does not work properly for options that are explicitly |
| 127 | set to None by the user, or options that are defined with a |
| 128 | default value other than None. |
| 129 | |
| 130 | """ |
| 131 | return {} |
| 132 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | def Usage(self): |
| 134 | """Display usage and terminate. |
| 135 | """ |
| 136 | self.OptionParser.print_usage() |
| 137 | sys.exit(1) |
| 138 | |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame^] | 139 | def CommonValidateOptions(self, opt, args): |
| 140 | """Validate common options.""" |
| 141 | opt.quiet = opt.output_mode is False |
| 142 | opt.verbose = opt.output_mode is True |
| 143 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 144 | def ValidateOptions(self, opt, args): |
| 145 | """Validate the user options & arguments before executing. |
| 146 | |
| 147 | This is meant to help break the code up into logical steps. Some tips: |
| 148 | * Use self.OptionParser.error to display CLI related errors. |
| 149 | * Adjust opt member defaults as makes sense. |
| 150 | * Adjust the args list, but do so inplace so the caller sees updates. |
| 151 | * Try to avoid updating self state. Leave that to Execute. |
| 152 | """ |
| 153 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | def Execute(self, opt, args): |
| 155 | """Perform the action, after option parsing is complete. |
| 156 | """ |
| 157 | raise NotImplementedError |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 158 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 159 | def _ResetPathToProjectMap(self, projects): |
| 160 | self._by_path = dict((p.worktree, p) for p in projects) |
| 161 | |
| 162 | def _UpdatePathToProjectMap(self, project): |
| 163 | self._by_path[project.worktree] = project |
| 164 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 165 | def _GetProjectByPath(self, manifest, path): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 166 | project = None |
| 167 | if os.path.exists(path): |
| 168 | oldpath = None |
David Pursehouse | 5a2517f | 2020-02-12 14:55:01 +0900 | [diff] [blame] | 169 | while (path and |
| 170 | path != oldpath and |
| 171 | path != manifest.topdir): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 172 | try: |
| 173 | project = self._by_path[path] |
| 174 | break |
| 175 | except KeyError: |
| 176 | oldpath = path |
| 177 | path = os.path.dirname(path) |
Mark E. Hamilton | f9fe3e1 | 2016-02-23 18:10:42 -0700 | [diff] [blame] | 178 | if not project and path == manifest.topdir: |
| 179 | try: |
| 180 | project = self._by_path[path] |
| 181 | except KeyError: |
| 182 | pass |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 183 | else: |
| 184 | try: |
| 185 | project = self._by_path[path] |
| 186 | except KeyError: |
| 187 | pass |
| 188 | return project |
| 189 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 190 | def GetProjects(self, args, manifest=None, groups='', missing_ok=False, |
| 191 | submodules_ok=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | """A list of projects that match the arguments. |
| 193 | """ |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 194 | if not manifest: |
| 195 | manifest = self.manifest |
| 196 | all_projects_list = manifest.projects |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | result = [] |
| 198 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 199 | mp = manifest.manifestProject |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 200 | |
Graham Christensen | 0369a06 | 2015-07-29 17:02:54 -0500 | [diff] [blame] | 201 | if not groups: |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 202 | groups = manifest.GetGroupsStr() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 203 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 204 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | if not args: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 206 | derived_projects = {} |
| 207 | for project in all_projects_list: |
| 208 | if submodules_ok or project.sync_s: |
| 209 | derived_projects.update((p.name, p) |
| 210 | for p in project.GetDerivedSubprojects()) |
| 211 | all_projects_list.extend(derived_projects.values()) |
| 212 | for project in all_projects_list: |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 213 | 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] | 214 | result.append(project) |
| 215 | else: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 216 | self._ResetPathToProjectMap(all_projects_list) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 217 | |
| 218 | for arg in args: |
Mike Frysinger | e778e57 | 2019-10-04 14:21:41 -0400 | [diff] [blame] | 219 | # We have to filter by manifest groups in case the requested project is |
| 220 | # checked out multiple times or differently based on them. |
| 221 | projects = [project for project in manifest.GetProjectsWithName(arg) |
| 222 | if project.MatchesGroups(groups)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 223 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 224 | if not projects: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 225 | path = os.path.abspath(arg).replace('\\', '/') |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 226 | project = self._GetProjectByPath(manifest, path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 228 | # If it's not a derived project, update path->project mapping and |
| 229 | # search again, as arg might actually point to a derived subproject. |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 230 | if (project and not project.Derived and (submodules_ok or |
| 231 | project.sync_s)): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 232 | search_again = False |
| 233 | for subproject in project.GetDerivedSubprojects(): |
| 234 | self._UpdatePathToProjectMap(subproject) |
| 235 | search_again = True |
| 236 | if search_again: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 237 | project = self._GetProjectByPath(manifest, path) or project |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 239 | if project: |
| 240 | projects = [project] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 242 | if not projects: |
| 243 | raise NoSuchProjectError(arg) |
| 244 | |
| 245 | for project in projects: |
| 246 | if not missing_ok and not project.Exists: |
Mike Frysinger | e778e57 | 2019-10-04 14:21:41 -0400 | [diff] [blame] | 247 | raise NoSuchProjectError('%s (%s)' % (arg, project.relpath)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 248 | if not project.MatchesGroups(groups): |
| 249 | raise InvalidProjectGroupsError(arg) |
| 250 | |
| 251 | result.extend(projects) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | |
| 253 | def _getpath(x): |
| 254 | return x.relpath |
| 255 | result.sort(key=_getpath) |
| 256 | return result |
| 257 | |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 258 | def FindProjects(self, args, inverse=False): |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 259 | result = [] |
David Pursehouse | 84c4d3c | 2013-04-30 10:57:37 +0900 | [diff] [blame] | 260 | patterns = [re.compile(r'%s' % a, re.IGNORECASE) for a in args] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 261 | for project in self.GetProjects(''): |
David Pursehouse | 84c4d3c | 2013-04-30 10:57:37 +0900 | [diff] [blame] | 262 | for pattern in patterns: |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 263 | match = pattern.search(project.name) or pattern.search(project.relpath) |
| 264 | if not inverse and match: |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 265 | result.append(project) |
| 266 | break |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 267 | if inverse and match: |
| 268 | break |
| 269 | else: |
| 270 | if inverse: |
| 271 | result.append(project) |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 272 | result.sort(key=lambda project: project.relpath) |
| 273 | return result |
| 274 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 275 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 276 | class InteractiveCommand(Command): |
| 277 | """Command which requires user interaction on the tty and |
| 278 | must not run within a pager, even if the user asks to. |
| 279 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 280 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 281 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 282 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 284 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 285 | class PagedCommand(Command): |
| 286 | """Command which defaults to output in a pager, as its |
| 287 | display tends to be larger than one screen full. |
| 288 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 289 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 290 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 291 | return True |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 292 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 293 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 294 | class MirrorSafeCommand(object): |
| 295 | """Command permits itself to run within a mirror, |
| 296 | and does not require a working directory. |
| 297 | """ |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 298 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 299 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 300 | class GitcAvailableCommand(object): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 301 | """Command that requires GITC to be available, but does |
| 302 | not require the local client to be a GITC client. |
| 303 | """ |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 304 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 305 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 306 | class GitcClientCommand(object): |
| 307 | """Command that requires the local client to be a GITC |
| 308 | client. |
| 309 | """ |