help.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. from __future__ import print_function
  16. import re
  17. import sys
  18. from formatter import AbstractFormatter, DumbWriter
  19. from color import Coloring
  20. from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
  21. import gitc_utils
  22. class Help(PagedCommand, MirrorSafeCommand):
  23. common = False
  24. helpSummary = "Display detailed help on a command"
  25. helpUsage = """
  26. %prog [--all|command]
  27. """
  28. helpDescription = """
  29. Displays detailed usage information about a command.
  30. """
  31. def _PrintAllCommands(self):
  32. print('usage: repo COMMAND [ARGS]')
  33. print('The complete list of recognized repo commands are:')
  34. commandNames = list(sorted(self.commands))
  35. maxlen = 0
  36. for name in commandNames:
  37. maxlen = max(maxlen, len(name))
  38. fmt = ' %%-%ds %%s' % maxlen
  39. for name in commandNames:
  40. command = self.commands[name]
  41. try:
  42. summary = command.helpSummary.strip()
  43. except AttributeError:
  44. summary = ''
  45. print(fmt % (name, summary))
  46. print("See 'repo help <command>' for more information on a "
  47. 'specific command.')
  48. def _PrintCommonCommands(self):
  49. print('usage: repo COMMAND [ARGS]')
  50. print('The most commonly used repo commands are:')
  51. def gitc_supported(cmd):
  52. if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
  53. return True
  54. if self.manifest.isGitcClient:
  55. return True
  56. if isinstance(cmd, GitcClientCommand):
  57. return False
  58. if gitc_utils.get_gitc_manifest_dir():
  59. return True
  60. return False
  61. commandNames = list(sorted([name
  62. for name, command in self.commands.items()
  63. if command.common and gitc_supported(command)]))
  64. maxlen = 0
  65. for name in commandNames:
  66. maxlen = max(maxlen, len(name))
  67. fmt = ' %%-%ds %%s' % maxlen
  68. for name in commandNames:
  69. command = self.commands[name]
  70. try:
  71. summary = command.helpSummary.strip()
  72. except AttributeError:
  73. summary = ''
  74. print(fmt % (name, summary))
  75. print(
  76. "See 'repo help <command>' for more information on a specific command.\n"
  77. "See 'repo help --all' for a complete list of recognized commands.")
  78. def _PrintCommandHelp(self, cmd):
  79. class _Out(Coloring):
  80. def __init__(self, gc):
  81. Coloring.__init__(self, gc, 'help')
  82. self.heading = self.printer('heading', attr='bold')
  83. self.wrap = AbstractFormatter(DumbWriter())
  84. def _PrintSection(self, heading, bodyAttr):
  85. try:
  86. body = getattr(cmd, bodyAttr)
  87. except AttributeError:
  88. return
  89. if body == '' or body is None:
  90. return
  91. self.nl()
  92. self.heading('%s', heading)
  93. self.nl()
  94. self.nl()
  95. me = 'repo %s' % cmd.NAME
  96. body = body.strip()
  97. body = body.replace('%prog', me)
  98. asciidoc_hdr = re.compile(r'^\n?#+ (.+)$')
  99. for para in body.split("\n\n"):
  100. if para.startswith(' '):
  101. self.write('%s', para)
  102. self.nl()
  103. self.nl()
  104. continue
  105. m = asciidoc_hdr.match(para)
  106. if m:
  107. self.heading(m.group(1))
  108. self.nl()
  109. self.nl()
  110. continue
  111. self.wrap.add_flowing_data(para)
  112. self.wrap.end_paragraph(1)
  113. self.wrap.end_paragraph(0)
  114. out = _Out(self.manifest.globalConfig)
  115. out._PrintSection('Summary', 'helpSummary')
  116. cmd.OptionParser.print_help()
  117. out._PrintSection('Description', 'helpDescription')
  118. def _Options(self, p):
  119. p.add_option('-a', '--all',
  120. dest='show_all', action='store_true',
  121. help='show the complete list of commands')
  122. def Execute(self, opt, args):
  123. if len(args) == 0:
  124. if opt.show_all:
  125. self._PrintAllCommands()
  126. else:
  127. self._PrintCommonCommands()
  128. elif len(args) == 1:
  129. name = args[0]
  130. try:
  131. cmd = self.commands[name]
  132. except KeyError:
  133. print("repo: '%s' is not a repo command." % name, file=sys.stderr)
  134. sys.exit(1)
  135. cmd.manifest = self.manifest
  136. self._PrintCommandHelp(cmd)
  137. else:
  138. self._PrintCommandHelp(self)