command.py 3.7 KB

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