blob: 971f968b0b6815fc13e266f25ded65f9682ca930 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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
16import os
17import optparse
Conley Owensd21720d2012-04-16 11:02:21 -070018import platform
Colin Cross5acde752012-03-28 20:15:45 -070019import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
21
David Rileye0684ad2017-04-05 00:02:59 -070022from event_log import EventLog
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from error import NoSuchProjectError
Colin Cross5acde752012-03-28 20:15:45 -070024from error import InvalidProjectGroupsError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025
David Pursehouseb148ac92012-11-16 09:33:39 +090026
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027class Command(object):
28 """Base class for any command line action in repo.
29 """
30
31 common = False
David Rileye0684ad2017-04-05 00:02:59 -070032 event_log = EventLog()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033 manifest = None
34 _optparse = None
35
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -070036 def WantPager(self, _opt):
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070037 return False
38
David Pursehouseb148ac92012-11-16 09:33:39 +090039 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 Projectcf31fe92008-10-21 07:00:00 -070060 @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. Hamilton8ccfa742016-02-10 10:44:30 -070068 self._optparse = optparse.OptionParser(usage=usage)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070069 self._Options(self._optparse)
70 return self._optparse
71
72 def _Options(self, p):
73 """Initialize the option parser.
74 """
75
David Pursehouseb148ac92012-11-16 09:33:39 +090076 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 Projectcf31fe92008-10-21 07:00:00 -070094 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 Owens971de8e2012-04-16 10:36:08 -0700104
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800105 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 Basib9a1b732015-08-20 12:19:28 -0700111 def _GetProjectByPath(self, manifest, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800112 project = None
113 if os.path.exists(path):
114 oldpath = None
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700115 while path and \
116 path != oldpath and \
117 path != manifest.topdir:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800118 try:
119 project = self._by_path[path]
120 break
121 except KeyError:
122 oldpath = path
123 path = os.path.dirname(path)
Mark E. Hamiltonf9fe3e12016-02-23 18:10:42 -0700124 if not project and path == manifest.topdir:
125 try:
126 project = self._by_path[path]
127 except KeyError:
128 pass
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800129 else:
130 try:
131 project = self._by_path[path]
132 except KeyError:
133 pass
134 return project
135
Simran Basib9a1b732015-08-20 12:19:28 -0700136 def GetProjects(self, args, manifest=None, groups='', missing_ok=False,
137 submodules_ok=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 """A list of projects that match the arguments.
139 """
Simran Basib9a1b732015-08-20 12:19:28 -0700140 if not manifest:
141 manifest = self.manifest
142 all_projects_list = manifest.projects
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 result = []
144
Simran Basib9a1b732015-08-20 12:19:28 -0700145 mp = manifest.manifestProject
Colin Cross5acde752012-03-28 20:15:45 -0700146
Graham Christensen0369a062015-07-29 17:02:54 -0500147 if not groups:
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700148 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700149 if not groups:
David Holmer0a1c6a12012-11-14 19:19:00 -0500150 groups = 'default,platform-' + platform.system().lower()
David Pursehouse1d947b32012-10-25 12:23:11 +0900151 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700152
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153 if not args:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800154 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. Hamilton8ccfa742016-02-10 10:44:30 -0700161 if (missing_ok or project.Exists) and project.MatchesGroups(groups):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700162 result.append(project)
163 else:
David James8d201162013-10-11 17:03:19 -0700164 self._ResetPathToProjectMap(all_projects_list)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700165
166 for arg in args:
Simran Basib9a1b732015-08-20 12:19:28 -0700167 projects = manifest.GetProjectsWithName(arg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168
David James8d201162013-10-11 17:03:19 -0700169 if not projects:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800170 path = os.path.abspath(arg).replace('\\', '/')
Simran Basib9a1b732015-08-20 12:19:28 -0700171 project = self._GetProjectByPath(manifest, path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700172
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800173 # 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. Hamilton8ccfa742016-02-10 10:44:30 -0700175 if (project and not project.Derived and (submodules_ok or
176 project.sync_s)):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800177 search_again = False
178 for subproject in project.GetDerivedSubprojects():
179 self._UpdatePathToProjectMap(subproject)
180 search_again = True
181 if search_again:
Simran Basib9a1b732015-08-20 12:19:28 -0700182 project = self._GetProjectByPath(manifest, path) or project
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700183
David James8d201162013-10-11 17:03:19 -0700184 if project:
185 projects = [project]
186
187 if not projects:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700188 raise NoSuchProjectError(arg)
David James8d201162013-10-11 17:03:19 -0700189
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)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195
David James8d201162013-10-11 17:03:19 -0700196 result.extend(projects)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700197
198 def _getpath(x):
199 return x.relpath
200 result.sort(key=_getpath)
201 return result
202
Takeshi Kanemoto1f056442016-01-26 14:11:35 +0900203 def FindProjects(self, args, inverse=False):
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800204 result = []
David Pursehouse84c4d3c2013-04-30 10:57:37 +0900205 patterns = [re.compile(r'%s' % a, re.IGNORECASE) for a in args]
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800206 for project in self.GetProjects(''):
David Pursehouse84c4d3c2013-04-30 10:57:37 +0900207 for pattern in patterns:
Takeshi Kanemoto1f056442016-01-26 14:11:35 +0900208 match = pattern.search(project.name) or pattern.search(project.relpath)
209 if not inverse and match:
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800210 result.append(project)
211 break
Takeshi Kanemoto1f056442016-01-26 14:11:35 +0900212 if inverse and match:
213 break
214 else:
215 if inverse:
216 result.append(project)
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800217 result.sort(key=lambda project: project.relpath)
218 return result
219
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700220
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900221# pylint: disable=W0223
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900222# 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 Projectcf31fe92008-10-21 07:00:00 -0700226class 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. Hamilton8ccfa742016-02-10 10:44:30 -0700230 def WantPager(self, _opt):
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700231 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700233
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700234class 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. Hamilton8ccfa742016-02-10 10:44:30 -0700238 def WantPager(self, _opt):
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700239 return True
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800240
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900241# pylint: enable=W0223
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900242
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700243
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800244class MirrorSafeCommand(object):
245 """Command permits itself to run within a mirror,
246 and does not require a working directory.
247 """
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700248
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700249
Dan Willemsen79360642015-08-31 15:45:06 -0700250class GitcAvailableCommand(object):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700251 """Command that requires GITC to be available, but does
252 not require the local client to be a GITC client.
253 """
Dan Willemsen79360642015-08-31 15:45:06 -0700254
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700255
Dan Willemsen79360642015-08-31 15:45:06 -0700256class GitcClientCommand(object):
257 """Command that requires the local client to be a GITC
258 client.
259 """