command.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # -*- coding:utf-8 -*-
  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. import os
  17. import optparse
  18. import platform
  19. import re
  20. import sys
  21. from event_log import EventLog
  22. from error import NoSuchProjectError
  23. from error import InvalidProjectGroupsError
  24. class Command(object):
  25. """Base class for any command line action in repo.
  26. """
  27. common = False
  28. event_log = EventLog()
  29. manifest = None
  30. _optparse = None
  31. def WantPager(self, _opt):
  32. return False
  33. def ReadEnvironmentOptions(self, opts):
  34. """ Set options from environment variables. """
  35. env_options = self._RegisteredEnvironmentOptions()
  36. for env_key, opt_key in env_options.items():
  37. # Get the user-set option value if any
  38. opt_value = getattr(opts, opt_key)
  39. # If the value is set, it means the user has passed it as a command
  40. # line option, and we should use that. Otherwise we can try to set it
  41. # with the value from the corresponding environment variable.
  42. if opt_value is not None:
  43. continue
  44. env_value = os.environ.get(env_key)
  45. if env_value is not None:
  46. setattr(opts, opt_key, env_value)
  47. return opts
  48. @property
  49. def OptionParser(self):
  50. if self._optparse is None:
  51. try:
  52. me = 'repo %s' % self.NAME
  53. usage = self.helpUsage.strip().replace('%prog', me)
  54. except AttributeError:
  55. usage = 'repo %s' % self.NAME
  56. self._optparse = optparse.OptionParser(usage=usage)
  57. self._Options(self._optparse)
  58. return self._optparse
  59. def _Options(self, p):
  60. """Initialize the option parser.
  61. """
  62. def _RegisteredEnvironmentOptions(self):
  63. """Get options that can be set from environment variables.
  64. Return a dictionary mapping environment variable name
  65. to option key name that it can override.
  66. Example: {'REPO_MY_OPTION': 'my_option'}
  67. Will allow the option with key value 'my_option' to be set
  68. from the value in the environment variable named 'REPO_MY_OPTION'.
  69. Note: This does not work properly for options that are explicitly
  70. set to None by the user, or options that are defined with a
  71. default value other than None.
  72. """
  73. return {}
  74. def Usage(self):
  75. """Display usage and terminate.
  76. """
  77. self.OptionParser.print_usage()
  78. sys.exit(1)
  79. def ValidateOptions(self, opt, args):
  80. """Validate the user options & arguments before executing.
  81. This is meant to help break the code up into logical steps. Some tips:
  82. * Use self.OptionParser.error to display CLI related errors.
  83. * Adjust opt member defaults as makes sense.
  84. * Adjust the args list, but do so inplace so the caller sees updates.
  85. * Try to avoid updating self state. Leave that to Execute.
  86. """
  87. def Execute(self, opt, args):
  88. """Perform the action, after option parsing is complete.
  89. """
  90. raise NotImplementedError
  91. def _ResetPathToProjectMap(self, projects):
  92. self._by_path = dict((p.worktree, p) for p in projects)
  93. def _UpdatePathToProjectMap(self, project):
  94. self._by_path[project.worktree] = project
  95. def _GetProjectByPath(self, manifest, path):
  96. project = None
  97. if os.path.exists(path):
  98. oldpath = None
  99. while (path and
  100. path != oldpath and
  101. path != manifest.topdir):
  102. try:
  103. project = self._by_path[path]
  104. break
  105. except KeyError:
  106. oldpath = path
  107. path = os.path.dirname(path)
  108. if not project and path == manifest.topdir:
  109. try:
  110. project = self._by_path[path]
  111. except KeyError:
  112. pass
  113. else:
  114. try:
  115. project = self._by_path[path]
  116. except KeyError:
  117. pass
  118. return project
  119. def GetProjects(self, args, manifest=None, groups='', missing_ok=False,
  120. submodules_ok=False):
  121. """A list of projects that match the arguments.
  122. """
  123. if not manifest:
  124. manifest = self.manifest
  125. all_projects_list = manifest.projects
  126. result = []
  127. mp = manifest.manifestProject
  128. if not groups:
  129. groups = mp.config.GetString('manifest.groups')
  130. if not groups:
  131. groups = 'default,platform-' + platform.system().lower()
  132. groups = [x for x in re.split(r'[,\s]+', groups) if x]
  133. if not args:
  134. derived_projects = {}
  135. for project in all_projects_list:
  136. if submodules_ok or project.sync_s:
  137. derived_projects.update((p.name, p)
  138. for p in project.GetDerivedSubprojects())
  139. all_projects_list.extend(derived_projects.values())
  140. for project in all_projects_list:
  141. if (missing_ok or project.Exists) and project.MatchesGroups(groups):
  142. result.append(project)
  143. else:
  144. self._ResetPathToProjectMap(all_projects_list)
  145. for arg in args:
  146. # We have to filter by manifest groups in case the requested project is
  147. # checked out multiple times or differently based on them.
  148. projects = [project for project in manifest.GetProjectsWithName(arg)
  149. if project.MatchesGroups(groups)]
  150. if not projects:
  151. path = os.path.abspath(arg).replace('\\', '/')
  152. project = self._GetProjectByPath(manifest, path)
  153. # If it's not a derived project, update path->project mapping and
  154. # search again, as arg might actually point to a derived subproject.
  155. if (project and not project.Derived and (submodules_ok or
  156. project.sync_s)):
  157. search_again = False
  158. for subproject in project.GetDerivedSubprojects():
  159. self._UpdatePathToProjectMap(subproject)
  160. search_again = True
  161. if search_again:
  162. project = self._GetProjectByPath(manifest, path) or project
  163. if project:
  164. projects = [project]
  165. if not projects:
  166. raise NoSuchProjectError(arg)
  167. for project in projects:
  168. if not missing_ok and not project.Exists:
  169. raise NoSuchProjectError('%s (%s)' % (arg, project.relpath))
  170. if not project.MatchesGroups(groups):
  171. raise InvalidProjectGroupsError(arg)
  172. result.extend(projects)
  173. def _getpath(x):
  174. return x.relpath
  175. result.sort(key=_getpath)
  176. return result
  177. def FindProjects(self, args, inverse=False):
  178. result = []
  179. patterns = [re.compile(r'%s' % a, re.IGNORECASE) for a in args]
  180. for project in self.GetProjects(''):
  181. for pattern in patterns:
  182. match = pattern.search(project.name) or pattern.search(project.relpath)
  183. if not inverse and match:
  184. result.append(project)
  185. break
  186. if inverse and match:
  187. break
  188. else:
  189. if inverse:
  190. result.append(project)
  191. result.sort(key=lambda project: project.relpath)
  192. return result
  193. class InteractiveCommand(Command):
  194. """Command which requires user interaction on the tty and
  195. must not run within a pager, even if the user asks to.
  196. """
  197. def WantPager(self, _opt):
  198. return False
  199. class PagedCommand(Command):
  200. """Command which defaults to output in a pager, as its
  201. display tends to be larger than one screen full.
  202. """
  203. def WantPager(self, _opt):
  204. return True
  205. class MirrorSafeCommand(object):
  206. """Command permits itself to run within a mirror,
  207. and does not require a working directory.
  208. """
  209. class GitcAvailableCommand(object):
  210. """Command that requires GITC to be available, but does
  211. not require the local client to be a GITC client.
  212. """
  213. class GitcClientCommand(object):
  214. """Command that requires the local client to be a GITC
  215. client.
  216. """