blob: d4d86798089be20d9f190daa80410a118bdafbce [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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
17import os
18import optparse
Conley Owensd21720d2012-04-16 11:02:21 -070019import platform
Colin Cross5acde752012-03-28 20:15:45 -070020import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import sys
22
David Rileye0684ad2017-04-05 00:02:59 -070023from event_log import EventLog
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024from error import NoSuchProjectError
Colin Cross5acde752012-03-28 20:15:45 -070025from error import InvalidProjectGroupsError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
David Pursehouseb148ac92012-11-16 09:33:39 +090027
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028class Command(object):
29 """Base class for any command line action in repo.
30 """
31
32 common = False
David Rileye0684ad2017-04-05 00:02:59 -070033 event_log = EventLog()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034 manifest = None
35 _optparse = None
36
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -070037 def WantPager(self, _opt):
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070038 return False
39
David Pursehouseb148ac92012-11-16 09:33:39 +090040 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 Projectcf31fe92008-10-21 07:00:00 -070061 @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. Hamilton8ccfa742016-02-10 10:44:30 -070069 self._optparse = optparse.OptionParser(usage=usage)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070070 self._Options(self._optparse)
71 return self._optparse
72
73 def _Options(self, p):
74 """Initialize the option parser.
75 """
76
David Pursehouseb148ac92012-11-16 09:33:39 +090077 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 Projectcf31fe92008-10-21 07:00:00 -070095 def Usage(self):
96 """Display usage and terminate.
97 """
98 self.OptionParser.print_usage()
99 sys.exit(1)
100
Mike Frysingerae6cb082019-08-27 01:10:59 -0400101 def ValidateOptions(self, opt, args):
102 """Validate the user options & arguments before executing.
103
104 This is meant to help break the code up into logical steps. Some tips:
105 * Use self.OptionParser.error to display CLI related errors.
106 * Adjust opt member defaults as makes sense.
107 * Adjust the args list, but do so inplace so the caller sees updates.
108 * Try to avoid updating self state. Leave that to Execute.
109 """
110
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 def Execute(self, opt, args):
112 """Perform the action, after option parsing is complete.
113 """
114 raise NotImplementedError
Conley Owens971de8e2012-04-16 10:36:08 -0700115
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800116 def _ResetPathToProjectMap(self, projects):
117 self._by_path = dict((p.worktree, p) for p in projects)
118
119 def _UpdatePathToProjectMap(self, project):
120 self._by_path[project.worktree] = project
121
Simran Basib9a1b732015-08-20 12:19:28 -0700122 def _GetProjectByPath(self, manifest, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800123 project = None
124 if os.path.exists(path):
125 oldpath = None
David Pursehouse5a2517f2020-02-12 14:55:01 +0900126 while (path and
127 path != oldpath and
128 path != manifest.topdir):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800129 try:
130 project = self._by_path[path]
131 break
132 except KeyError:
133 oldpath = path
134 path = os.path.dirname(path)
Mark E. Hamiltonf9fe3e12016-02-23 18:10:42 -0700135 if not project and path == manifest.topdir:
136 try:
137 project = self._by_path[path]
138 except KeyError:
139 pass
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800140 else:
141 try:
142 project = self._by_path[path]
143 except KeyError:
144 pass
145 return project
146
Simran Basib9a1b732015-08-20 12:19:28 -0700147 def GetProjects(self, args, manifest=None, groups='', missing_ok=False,
148 submodules_ok=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149 """A list of projects that match the arguments.
150 """
Simran Basib9a1b732015-08-20 12:19:28 -0700151 if not manifest:
152 manifest = self.manifest
153 all_projects_list = manifest.projects
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154 result = []
155
Simran Basib9a1b732015-08-20 12:19:28 -0700156 mp = manifest.manifestProject
Colin Cross5acde752012-03-28 20:15:45 -0700157
Graham Christensen0369a062015-07-29 17:02:54 -0500158 if not groups:
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700159 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700160 if not groups:
David Holmer0a1c6a12012-11-14 19:19:00 -0500161 groups = 'default,platform-' + platform.system().lower()
David Pursehouse1d947b32012-10-25 12:23:11 +0900162 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700163
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 if not args:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800165 derived_projects = {}
166 for project in all_projects_list:
167 if submodules_ok or project.sync_s:
168 derived_projects.update((p.name, p)
169 for p in project.GetDerivedSubprojects())
170 all_projects_list.extend(derived_projects.values())
171 for project in all_projects_list:
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700172 if (missing_ok or project.Exists) and project.MatchesGroups(groups):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700173 result.append(project)
174 else:
David James8d201162013-10-11 17:03:19 -0700175 self._ResetPathToProjectMap(all_projects_list)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176
177 for arg in args:
Mike Frysingere778e572019-10-04 14:21:41 -0400178 # We have to filter by manifest groups in case the requested project is
179 # checked out multiple times or differently based on them.
180 projects = [project for project in manifest.GetProjectsWithName(arg)
181 if project.MatchesGroups(groups)]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700182
David James8d201162013-10-11 17:03:19 -0700183 if not projects:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800184 path = os.path.abspath(arg).replace('\\', '/')
Simran Basib9a1b732015-08-20 12:19:28 -0700185 project = self._GetProjectByPath(manifest, path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700186
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800187 # If it's not a derived project, update path->project mapping and
188 # search again, as arg might actually point to a derived subproject.
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700189 if (project and not project.Derived and (submodules_ok or
190 project.sync_s)):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800191 search_again = False
192 for subproject in project.GetDerivedSubprojects():
193 self._UpdatePathToProjectMap(subproject)
194 search_again = True
195 if search_again:
Simran Basib9a1b732015-08-20 12:19:28 -0700196 project = self._GetProjectByPath(manifest, path) or project
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700197
David James8d201162013-10-11 17:03:19 -0700198 if project:
199 projects = [project]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200
David James8d201162013-10-11 17:03:19 -0700201 if not projects:
202 raise NoSuchProjectError(arg)
203
204 for project in projects:
205 if not missing_ok and not project.Exists:
Mike Frysingere778e572019-10-04 14:21:41 -0400206 raise NoSuchProjectError('%s (%s)' % (arg, project.relpath))
David James8d201162013-10-11 17:03:19 -0700207 if not project.MatchesGroups(groups):
208 raise InvalidProjectGroupsError(arg)
209
210 result.extend(projects)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211
212 def _getpath(x):
213 return x.relpath
214 result.sort(key=_getpath)
215 return result
216
Takeshi Kanemoto1f056442016-01-26 14:11:35 +0900217 def FindProjects(self, args, inverse=False):
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800218 result = []
David Pursehouse84c4d3c2013-04-30 10:57:37 +0900219 patterns = [re.compile(r'%s' % a, re.IGNORECASE) for a in args]
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800220 for project in self.GetProjects(''):
David Pursehouse84c4d3c2013-04-30 10:57:37 +0900221 for pattern in patterns:
Takeshi Kanemoto1f056442016-01-26 14:11:35 +0900222 match = pattern.search(project.name) or pattern.search(project.relpath)
223 if not inverse and match:
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800224 result.append(project)
225 break
Takeshi Kanemoto1f056442016-01-26 14:11:35 +0900226 if inverse and match:
227 break
228 else:
229 if inverse:
230 result.append(project)
Zhiguang Lia8864fb2013-03-15 10:32:10 +0800231 result.sort(key=lambda project: project.relpath)
232 return result
233
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700234
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700235class InteractiveCommand(Command):
236 """Command which requires user interaction on the tty and
237 must not run within a pager, even if the user asks to.
238 """
David Pursehouse819827a2020-02-12 15:20:19 +0900239
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700240 def WantPager(self, _opt):
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700241 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700242
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700243
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244class PagedCommand(Command):
245 """Command which defaults to output in a pager, as its
246 display tends to be larger than one screen full.
247 """
David Pursehouse819827a2020-02-12 15:20:19 +0900248
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700249 def WantPager(self, _opt):
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700250 return True
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800251
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700252
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800253class MirrorSafeCommand(object):
254 """Command permits itself to run within a mirror,
255 and does not require a working directory.
256 """
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700257
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700258
Dan Willemsen79360642015-08-31 15:45:06 -0700259class GitcAvailableCommand(object):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700260 """Command that requires GITC to be available, but does
261 not require the local client to be a GITC client.
262 """
Dan Willemsen79360642015-08-31 15:45:06 -0700263
Mark E. Hamilton8ccfa742016-02-10 10:44:30 -0700264
Dan Willemsen79360642015-08-31 15:45:06 -0700265class GitcClientCommand(object):
266 """Command that requires the local client to be a GITC
267 client.
268 """