command.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. @property
  26. def OptionParser(self):
  27. if self._optparse is None:
  28. try:
  29. me = 'repo %s' % self.NAME
  30. usage = self.helpUsage.strip().replace('%prog', me)
  31. except AttributeError:
  32. usage = 'repo %s' % self.NAME
  33. self._optparse = optparse.OptionParser(usage = usage)
  34. self._Options(self._optparse)
  35. return self._optparse
  36. def _Options(self, p):
  37. """Initialize the option parser.
  38. """
  39. def Usage(self):
  40. """Display usage and terminate.
  41. """
  42. self.OptionParser.print_usage()
  43. sys.exit(1)
  44. def Execute(self, opt, args):
  45. """Perform the action, after option parsing is complete.
  46. """
  47. raise NotImplementedError
  48. def GetProjects(self, args, missing_ok=False):
  49. """A list of projects that match the arguments.
  50. """
  51. all = self.manifest.projects
  52. result = []
  53. if not args:
  54. for project in all.values():
  55. if missing_ok or project.Exists:
  56. result.append(project)
  57. else:
  58. by_path = None
  59. for arg in args:
  60. project = all.get(arg)
  61. if not project:
  62. path = os.path.abspath(arg)
  63. if not by_path:
  64. by_path = dict()
  65. for p in all.values():
  66. by_path[p.worktree] = p
  67. if os.path.exists(path):
  68. while path \
  69. and path != '/' \
  70. and path != self.manifest.topdir:
  71. try:
  72. project = by_path[path]
  73. break
  74. except KeyError:
  75. path = os.path.dirname(path)
  76. else:
  77. try:
  78. project = by_path[path]
  79. except KeyError:
  80. pass
  81. if not project:
  82. raise NoSuchProjectError(arg)
  83. if not missing_ok and not project.Exists:
  84. raise NoSuchProjectError(arg)
  85. result.append(project)
  86. def _getpath(x):
  87. return x.relpath
  88. result.sort(key=_getpath)
  89. return result
  90. class InteractiveCommand(Command):
  91. """Command which requires user interaction on the tty and
  92. must not run within a pager, even if the user asks to.
  93. """
  94. class PagedCommand(Command):
  95. """Command which defaults to output in a pager, as its
  96. display tends to be larger than one screen full.
  97. """