Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import os |
| 18 | import optparse |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 19 | import platform |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 20 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import sys |
| 22 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 23 | from event_log import EventLog |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | from error import NoSuchProjectError |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 25 | from error import InvalidProjectGroupsError |
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 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | class Command(object): |
| 29 | """Base class for any command line action in repo. |
| 30 | """ |
| 31 | |
| 32 | common = False |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 33 | event_log = EventLog() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | manifest = None |
| 35 | _optparse = None |
| 36 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 37 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 38 | return False |
| 39 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 40 | def ReadEnvironmentOptions(self, opts): |
| 41 | """ Set options from environment variables. """ |
| 42 | |
| 43 | env_options = self._RegisteredEnvironmentOptions() |
| 44 | |
| 45 | for env_key, opt_key in env_options.items(): |
| 46 | # Get the user-set option value if any |
| 47 | opt_value = getattr(opts, opt_key) |
| 48 | |
| 49 | # If the value is set, it means the user has passed it as a command |
| 50 | # line option, and we should use that. Otherwise we can try to set it |
| 51 | # with the value from the corresponding environment variable. |
| 52 | if opt_value is not None: |
| 53 | continue |
| 54 | |
| 55 | env_value = os.environ.get(env_key) |
| 56 | if env_value is not None: |
| 57 | setattr(opts, opt_key, env_value) |
| 58 | |
| 59 | return opts |
| 60 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | @property |
| 62 | def OptionParser(self): |
| 63 | if self._optparse is None: |
| 64 | try: |
| 65 | me = 'repo %s' % self.NAME |
| 66 | usage = self.helpUsage.strip().replace('%prog', me) |
| 67 | except AttributeError: |
| 68 | usage = 'repo %s' % self.NAME |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 69 | self._optparse = optparse.OptionParser(usage=usage) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | self._Options(self._optparse) |
| 71 | return self._optparse |
| 72 | |
| 73 | def _Options(self, p): |
| 74 | """Initialize the option parser. |
| 75 | """ |
| 76 | |
David Pursehouse | b148ac9 | 2012-11-16 09:33:39 +0900 | [diff] [blame] | 77 | def _RegisteredEnvironmentOptions(self): |
| 78 | """Get options that can be set from environment variables. |
| 79 | |
| 80 | Return a dictionary mapping environment variable name |
| 81 | to option key name that it can override. |
| 82 | |
| 83 | Example: {'REPO_MY_OPTION': 'my_option'} |
| 84 | |
| 85 | Will allow the option with key value 'my_option' to be set |
| 86 | from the value in the environment variable named 'REPO_MY_OPTION'. |
| 87 | |
| 88 | Note: This does not work properly for options that are explicitly |
| 89 | set to None by the user, or options that are defined with a |
| 90 | default value other than None. |
| 91 | |
| 92 | """ |
| 93 | return {} |
| 94 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | def Usage(self): |
| 96 | """Display usage and terminate. |
| 97 | """ |
| 98 | self.OptionParser.print_usage() |
| 99 | sys.exit(1) |
| 100 | |
| 101 | def Execute(self, opt, args): |
| 102 | """Perform the action, after option parsing is complete. |
| 103 | """ |
| 104 | raise NotImplementedError |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 105 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 106 | def _ResetPathToProjectMap(self, projects): |
| 107 | self._by_path = dict((p.worktree, p) for p in projects) |
| 108 | |
| 109 | def _UpdatePathToProjectMap(self, project): |
| 110 | self._by_path[project.worktree] = project |
| 111 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 112 | def _GetProjectByPath(self, manifest, path): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 113 | project = None |
| 114 | if os.path.exists(path): |
| 115 | oldpath = None |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 116 | while path and \ |
| 117 | path != oldpath and \ |
| 118 | path != manifest.topdir: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 119 | try: |
| 120 | project = self._by_path[path] |
| 121 | break |
| 122 | except KeyError: |
| 123 | oldpath = path |
| 124 | path = os.path.dirname(path) |
Mark E. Hamilton | f9fe3e1 | 2016-02-23 18:10:42 -0700 | [diff] [blame] | 125 | if not project and path == manifest.topdir: |
| 126 | try: |
| 127 | project = self._by_path[path] |
| 128 | except KeyError: |
| 129 | pass |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 130 | else: |
| 131 | try: |
| 132 | project = self._by_path[path] |
| 133 | except KeyError: |
| 134 | pass |
| 135 | return project |
| 136 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 137 | def GetProjects(self, args, manifest=None, groups='', missing_ok=False, |
| 138 | submodules_ok=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | """A list of projects that match the arguments. |
| 140 | """ |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 141 | if not manifest: |
| 142 | manifest = self.manifest |
| 143 | all_projects_list = manifest.projects |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 144 | result = [] |
| 145 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 146 | mp = manifest.manifestProject |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 147 | |
Graham Christensen | 0369a06 | 2015-07-29 17:02:54 -0500 | [diff] [blame] | 148 | if not groups: |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 149 | groups = mp.config.GetString('manifest.groups') |
Colin Cross | c39864f | 2012-04-23 13:41:58 -0700 | [diff] [blame] | 150 | if not groups: |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 151 | groups = 'default,platform-' + platform.system().lower() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 152 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 153 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | if not args: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 155 | derived_projects = {} |
| 156 | for project in all_projects_list: |
| 157 | if submodules_ok or project.sync_s: |
| 158 | derived_projects.update((p.name, p) |
| 159 | for p in project.GetDerivedSubprojects()) |
| 160 | all_projects_list.extend(derived_projects.values()) |
| 161 | for project in all_projects_list: |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 162 | 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] | 163 | result.append(project) |
| 164 | else: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 165 | self._ResetPathToProjectMap(all_projects_list) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 166 | |
| 167 | for arg in args: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 168 | projects = manifest.GetProjectsWithName(arg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 169 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 170 | if not projects: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 171 | path = os.path.abspath(arg).replace('\\', '/') |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 172 | project = self._GetProjectByPath(manifest, path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 174 | # If it's not a derived project, update path->project mapping and |
| 175 | # search again, as arg might actually point to a derived subproject. |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 176 | if (project and not project.Derived and (submodules_ok or |
| 177 | project.sync_s)): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 178 | search_again = False |
| 179 | for subproject in project.GetDerivedSubprojects(): |
| 180 | self._UpdatePathToProjectMap(subproject) |
| 181 | search_again = True |
| 182 | if search_again: |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 183 | project = self._GetProjectByPath(manifest, path) or project |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 185 | if project: |
| 186 | projects = [project] |
| 187 | |
| 188 | if not projects: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 189 | raise NoSuchProjectError(arg) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 190 | |
| 191 | for project in projects: |
| 192 | if not missing_ok and not project.Exists: |
| 193 | raise NoSuchProjectError(arg) |
| 194 | if not project.MatchesGroups(groups): |
| 195 | raise InvalidProjectGroupsError(arg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 196 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 197 | result.extend(projects) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | |
| 199 | def _getpath(x): |
| 200 | return x.relpath |
| 201 | result.sort(key=_getpath) |
| 202 | return result |
| 203 | |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 204 | def FindProjects(self, args, inverse=False): |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 205 | result = [] |
David Pursehouse | 84c4d3c | 2013-04-30 10:57:37 +0900 | [diff] [blame] | 206 | patterns = [re.compile(r'%s' % a, re.IGNORECASE) for a in args] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 207 | for project in self.GetProjects(''): |
David Pursehouse | 84c4d3c | 2013-04-30 10:57:37 +0900 | [diff] [blame] | 208 | for pattern in patterns: |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 209 | match = pattern.search(project.name) or pattern.search(project.relpath) |
| 210 | if not inverse and match: |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 211 | result.append(project) |
| 212 | break |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 213 | if inverse and match: |
| 214 | break |
| 215 | else: |
| 216 | if inverse: |
| 217 | result.append(project) |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 218 | result.sort(key=lambda project: project.relpath) |
| 219 | return result |
| 220 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 221 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | class InteractiveCommand(Command): |
| 223 | """Command which requires user interaction on the tty and |
| 224 | must not run within a pager, even if the user asks to. |
| 225 | """ |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 226 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 227 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 228 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 229 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 230 | class PagedCommand(Command): |
| 231 | """Command which defaults to output in a pager, as its |
| 232 | display tends to be larger than one screen full. |
| 233 | """ |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 234 | def WantPager(self, _opt): |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 235 | return True |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 236 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 237 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 238 | class MirrorSafeCommand(object): |
| 239 | """Command permits itself to run within a mirror, |
| 240 | and does not require a working directory. |
| 241 | """ |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 242 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 243 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 244 | class GitcAvailableCommand(object): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 245 | """Command that requires GITC to be available, but does |
| 246 | not require the local client to be a GITC client. |
| 247 | """ |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 248 | |
Mark E. Hamilton | 8ccfa74 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 249 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 250 | class GitcClientCommand(object): |
| 251 | """Command that requires the local client to be a GITC |
| 252 | client. |
| 253 | """ |