command.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. import os
  16. import optparse
  17. import re
  18. import sys
  19. from error import NoSuchProjectError
  20. from error import InvalidProjectGroupsError
  21. class Command(object):
  22. """Base class for any command line action in repo.
  23. """
  24. common = False
  25. manifest = None
  26. _optparse = None
  27. def WantPager(self, opt):
  28. return False
  29. @property
  30. def OptionParser(self):
  31. if self._optparse is None:
  32. try:
  33. me = 'repo %s' % self.NAME
  34. usage = self.helpUsage.strip().replace('%prog', me)
  35. except AttributeError:
  36. usage = 'repo %s' % self.NAME
  37. self._optparse = optparse.OptionParser(usage = usage)
  38. self._Options(self._optparse)
  39. return self._optparse
  40. def _Options(self, p):
  41. """Initialize the option parser.
  42. """
  43. def Usage(self):
  44. """Display usage and terminate.
  45. """
  46. self.OptionParser.print_usage()
  47. sys.exit(1)
  48. def Execute(self, opt, args):
  49. """Perform the action, after option parsing is complete.
  50. """
  51. raise NotImplementedError
  52. def GetProjects(self, args, missing_ok=False):
  53. """A list of projects that match the arguments.
  54. """
  55. all = self.manifest.projects
  56. result = []
  57. mp = self.manifest.manifestProject
  58. groups = mp.config.GetString('manifest.groups')
  59. if groups is None:
  60. groups = 'default'
  61. groups = [x for x in re.split('[,\s]+', groups) if x]
  62. if not args:
  63. for project in all.values():
  64. if ((missing_ok or project.Exists) and
  65. project.MatchesGroups(groups)):
  66. result.append(project)
  67. else:
  68. by_path = None
  69. for arg in args:
  70. project = all.get(arg)
  71. if not project:
  72. path = os.path.abspath(arg).replace('\\', '/')
  73. if not by_path:
  74. by_path = dict()
  75. for p in all.values():
  76. by_path[p.worktree] = p
  77. if os.path.exists(path):
  78. oldpath = None
  79. while path \
  80. and path != oldpath \
  81. and path != self.manifest.topdir:
  82. try:
  83. project = by_path[path]
  84. break
  85. except KeyError:
  86. oldpath = path
  87. path = os.path.dirname(path)
  88. else:
  89. try:
  90. project = by_path[path]
  91. except KeyError:
  92. pass
  93. if not project:
  94. raise NoSuchProjectError(arg)
  95. if not missing_ok and not project.Exists:
  96. raise NoSuchProjectError(arg)
  97. if not project.MatchesGroups(groups):
  98. raise InvalidProjectGroupsError(arg)
  99. result.append(project)
  100. def _getpath(x):
  101. return x.relpath
  102. result.sort(key=_getpath)
  103. return result
  104. class InteractiveCommand(Command):
  105. """Command which requires user interaction on the tty and
  106. must not run within a pager, even if the user asks to.
  107. """
  108. def WantPager(self, opt):
  109. return False
  110. class PagedCommand(Command):
  111. """Command which defaults to output in a pager, as its
  112. display tends to be larger than one screen full.
  113. """
  114. def WantPager(self, opt):
  115. return True
  116. class MirrorSafeCommand(object):
  117. """Command permits itself to run within a mirror,
  118. and does not require a working directory.
  119. """