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