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