command.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. from error import NoSuchProjectError
  19. class Command(object):
  20. """Base class for any command line action in repo.
  21. """
  22. common = False
  23. manifest = None
  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. def GetProjects(self, args, missing_ok=False):
  51. """A list of projects that match the arguments.
  52. """
  53. all = self.manifest.projects
  54. result = []
  55. if not args:
  56. for project in all.values():
  57. if missing_ok or project.Exists:
  58. result.append(project)
  59. else:
  60. by_path = None
  61. for arg in args:
  62. project = all.get(arg)
  63. if not project:
  64. path = os.path.abspath(arg).replace('\\', '/')
  65. if not by_path:
  66. by_path = dict()
  67. for p in all.values():
  68. by_path[p.worktree] = p
  69. if os.path.exists(path):
  70. oldpath = None
  71. while path \
  72. and path != oldpath \
  73. and path != self.manifest.topdir:
  74. try:
  75. project = by_path[path]
  76. break
  77. except KeyError:
  78. oldpath = path
  79. path = os.path.dirname(path)
  80. else:
  81. try:
  82. project = by_path[path]
  83. except KeyError:
  84. pass
  85. if not project:
  86. raise NoSuchProjectError(arg)
  87. if not missing_ok and not project.Exists:
  88. raise NoSuchProjectError(arg)
  89. result.append(project)
  90. def _getpath(x):
  91. return x.relpath
  92. result.sort(key=_getpath)
  93. return result
  94. class InteractiveCommand(Command):
  95. """Command which requires user interaction on the tty and
  96. must not run within a pager, even if the user asks to.
  97. """
  98. def WantPager(self, opt):
  99. return False
  100. class PagedCommand(Command):
  101. """Command which defaults to output in a pager, as its
  102. display tends to be larger than one screen full.
  103. """
  104. def WantPager(self, opt):
  105. return True
  106. class MirrorSafeCommand(object):
  107. """Command permits itself to run within a mirror,
  108. and does not require a working directory.
  109. """