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) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | self._Options(self._optparse) |
| 88 | return self._optparse |
| 89 | |
| 90 | def _Options(self, p): |
| 91 | """Initialize the option parser. |
| 92 | """ |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 93 | if self.PARALLEL_JOBS is not None: |
| 94 | p.add_option( |
| 95 | '-j', '--jobs', |
| 96 | type=int, default=self.PARALLEL_JOBS, |
| 97 | 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] | 98 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 99 | def _RegisteredEnvironmentOptions(self): |
| 100 | """Get options that can be set from environment variables. |
| 101 | |
| 102 | Return a dictionary mapping environment variable name |
| 103 | to option key name that it can override. |
| 104 | |
| 105 | Example: {'REPO_MY_OPTION': 'my_option'} |
| 106 | |
| 107 | Will allow the option with key value 'my_option' to be set |
| 108 | from the value in the environment variable named 'REPO_MY_OPTION'. |
| 109 | |
| 110 | Note: This does not work properly for options that are explicitly |
| 111 | set to None by the user, or options that are defined with a |
| 112 | default value other than None. |
| 113 | |
| 114 | """ |
| 115 | return {} |
| 116 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | def Usage(self): |
| 118 | """Display usage and terminate. |
| 119 | """ |
| 120 | self.OptionParser.print_usage() |
| 121 | sys.exit(1) |
| 122 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 123 | def ValidateOptions(self, opt, args): |
| 124 | """Validate the user options & arguments before executing. |
| 125 | |
| 126 | This is meant to help break the code up into logical steps. Some tips: |
| 127 | * Use self.OptionParser.error to display CLI related errors. |
| 128 | * Adjust opt member defaults as makes sense. |
| 129 | * Adjust the args list, but do so inplace so the caller sees updates. |
| 130 | * Try to avoid updating self state. Leave that to Execute. |
| 131 | """ |
| 132 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | def Execute(self, opt, args): |
| 134 | """Perform the action, after option parsing is complete. |
| 135 | """ |
| 136 | raise NotImplementedError |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 137 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 138 | def _ResetPathToProjectMap(self, projects): |
| 139 | self._by_path = dict((p.worktree, p) for p in projects) |
| 140 | |
| 141 | def _UpdatePathToProjectMap(self, project): |
| 142 | self._by_path[project.worktree] = project |
| 143 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 144 | def _GetProjectByPath(self, manifest, path): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 145 | project = None |
| 146 | if os.path.exists(path): |
| 147 | oldpath = None |
David Pursehouse | 5a2517f | 2020-02-12 14:55:01 +0900 | [diff] [blame] | 148 | while (path and |
| 149 | path != oldpath and |
| 150 | path != manifest.topdir): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 151 | try: |
| 152 | project = self._by_path[path] |
| 153 | break |
| 154 | except KeyError: |
| 155 | oldpath = path |
| 156 | path = os.path.dirname(path) |
Mark E. Hamilton | f9fe3e1 | 2016-02-23 18:10:42 -0700 | [diff] [blame] | 157 | if not project and path == manifest.topdir: |
| 158 | try: |
| 159 | project = self._by_path[path] |
| 160 | except KeyError: |
| 161 | pass |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 162 | else: |
| 163 | try: |
| 164 | project = self._by_path[path] |
| 165 | except KeyError: |
| 166 | pass |
| 167 | return project |
| 168 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 169 | def GetProjects(self, args, manifest=None, groups='', missing_ok=False, |
| 170 | submodules_ok=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | """A list of projects that match the arguments. |
| 172 | """ |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 173 | if not manifest: |
| 174 | manifest = self.manifest |
| 175 | all_projects_list = manifest.projects |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 176 | result = [] |
| 177 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 178 | mp = manifest.manifestProject |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 179 | |
Graham Christensen | 0369a06 | 2015-07-29 17:02:54 -0500 | [diff] [blame] | 180 | if not groups: |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 181 | groups = mp.config.GetString('manifest.groups') |
Colin Cross | c39864f | 2012-04-23 13:41:58 -0700 | [diff] [blame] | 182 | if not groups: |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 183 | groups = 'default,platform-' + platform.system().lower() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 184 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 185 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | if not args: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 187 | derived_projects = {} |
| 188 | for project in all_projects_list: |
| 189 | if submodules_ok or project.sync_s: |
| 190 | derived_projects.update((p.name, p) |
| 191 | for p in project.GetDerivedSubprojects()) |
| 192 | all_projects_list.extend(derived_projects.values()) |
| 193 | for project in all_projects_list: |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 194 | 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] | 195 | result.append(project) |
| 196 | else: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 197 | self._ResetPathToProjectMap(all_projects_list) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | |
| 199 | for arg in args: |
Mike Frysinger | e778e57 | 2019-10-04 14:21:41 -0400 | [diff] [blame] | 200 | # We have to filter by manifest groups in case the requested project is |
| 201 | # checked out multiple times or differently based on them. |
| 202 | projects = [project for project in manifest.GetProjectsWithName(arg) |
| 203 | if project.MatchesGroups(groups)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 205 | if not projects: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 206 | path = os.path.abspath(arg).replace('\\', '/') |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 207 | project = self._GetProjectByPath(manifest, path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 208 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 209 | # If it's not a derived project, update path->project mapping and |
| 210 | # search again, as arg might actually point to a derived subproject. |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 211 | if (project and not project.Derived and (submodules_ok or |
| 212 | project.sync_s)): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 213 | search_again = False |
| 214 | for subproject in project.GetDerivedSubprojects(): |
| 215 | self._UpdatePathToProjectMap(subproject) |
| 216 | search_again = True |
| 217 | if search_again: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 218 | project = self._GetProjectByPath(manifest, path) or project |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 220 | if project: |
| 221 | projects = [project] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 223 | if not projects: |
| 224 | raise NoSuchProjectError(arg) |
| 225 | |
| 226 | for project in projects: |
| 227 | if not missing_ok and not project.Exists: |
Mike Frysinger | e778e57 | 2019-10-04 14:21:41 -0400 | [diff] [blame] | 228 | raise NoSuchProjectError('%s (%s)' % (arg, project.relpath)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 229 | if not project.MatchesGroups(groups): |
| 230 | raise InvalidProjectGroupsError(arg) |
| 231 | |
| 232 | result.extend(projects) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | |
| 234 | def _getpath(x): |
| 235 | return x.relpath |
| 236 | result.sort(key=_getpath) |
| 237 | return result |
| 238 | |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 239 | def FindProjects(self, args, inverse=False): |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 240 | result = [] |
David Pursehouse | 84c4d3c | 2013-04-30 10:57:37 +0900 | [diff] [blame] | 241 | patterns = [re.compile(r'%s' % a, re.IGNORECASE) for a in args] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 242 | for project in self.GetProjects(''): |
David Pursehouse | 84c4d3c | 2013-04-30 10:57:37 +0900 | [diff] [blame] | 243 | for pattern in patterns: |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 244 | match = pattern.search(project.name) or pattern.search(project.relpath) |
| 245 | if not inverse and match: |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 246 | result.append(project) |
| 247 | break |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 248 | if inverse and match: |
| 249 | break |
| 250 | else: |
| 251 | if inverse: |
| 252 | result.append(project) |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 253 | result.sort(key=lambda project: project.relpath) |
| 254 | return result |
| 255 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 256 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 257 | class InteractiveCommand(Command): |
| 258 | """Command which requires user interaction on the tty and |
| 259 | must not run within a pager, even if the user asks to. |
| 260 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 261 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 262 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 263 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 265 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | class PagedCommand(Command): |
| 267 | """Command which defaults to output in a pager, as its |
| 268 | display tends to be larger than one screen full. |
| 269 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 270 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 271 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 272 | return True |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 273 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 274 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 275 | class MirrorSafeCommand(object): |
| 276 | """Command permits itself to run within a mirror, |
| 277 | and does not require a working directory. |
| 278 | """ |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 279 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 280 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 281 | class GitcAvailableCommand(object): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 282 | """Command that requires GITC to be available, but does |
| 283 | not require the local client to be a GITC client. |
| 284 | """ |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 285 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 286 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 287 | class GitcClientCommand(object): |
| 288 | """Command that requires the local client to be a GITC |
| 289 | client. |
| 290 | """ |