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