help.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2008 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import re
  18. import sys
  19. from formatter import AbstractFormatter, DumbWriter
  20. from color import Coloring
  21. from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
  22. import gitc_utils
  23. class Help(PagedCommand, MirrorSafeCommand):
  24. common = False
  25. helpSummary = "Display detailed help on a command"
  26. helpUsage = """
  27. %prog [--all|command]
  28. """
  29. helpDescription = """
  30. Displays detailed usage information about a command.
  31. """
  32. def _PrintAllCommands(self):
  33. print('usage: repo COMMAND [ARGS]')
  34. print('The complete list of recognized repo commands are:')
  35. commandNames = list(sorted(self.commands))
  36. maxlen = 0
  37. for name in commandNames:
  38. maxlen = max(maxlen, len(name))
  39. fmt = ' %%-%ds %%s' % maxlen
  40. for name in commandNames:
  41. command = self.commands[name]
  42. try:
  43. summary = command.helpSummary.strip()
  44. except AttributeError:
  45. summary = ''
  46. print(fmt % (name, summary))
  47. print("See 'repo help <command>' for more information on a "
  48. 'specific command.')
  49. def _PrintCommonCommands(self):
  50. print('usage: repo COMMAND [ARGS]')
  51. print('The most commonly used repo commands are:')
  52. def gitc_supported(cmd):
  53. if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
  54. return True
  55. if self.manifest.isGitcClient:
  56. return True
  57. if isinstance(cmd, GitcClientCommand):
  58. return False
  59. if gitc_utils.get_gitc_manifest_dir():
  60. return True
  61. return False
  62. commandNames = list(sorted([name
  63. for name, command in self.commands.items()
  64. if command.common and gitc_supported(command)]))
  65. maxlen = 0
  66. for name in commandNames:
  67. maxlen = max(maxlen, len(name))
  68. fmt = ' %%-%ds %%s' % maxlen
  69. for name in commandNames:
  70. command = self.commands[name]
  71. try:
  72. summary = command.helpSummary.strip()
  73. except AttributeError:
  74. summary = ''
  75. print(fmt % (name, summary))
  76. print(
  77. "See 'repo help <command>' for more information on a specific command.\n"
  78. "See 'repo help --all' for a complete list of recognized commands.")
  79. def _PrintCommandHelp(self, cmd, header_prefix=''):
  80. class _Out(Coloring):
  81. def __init__(self, gc):
  82. Coloring.__init__(self, gc, 'help')
  83. self.heading = self.printer('heading', attr='bold')
  84. self.wrap = AbstractFormatter(DumbWriter())
  85. def _PrintSection(self, heading, bodyAttr):
  86. try:
  87. body = getattr(cmd, bodyAttr)
  88. except AttributeError:
  89. return
  90. if body == '' or body is None:
  91. return
  92. self.nl()
  93. self.heading('%s%s', header_prefix, heading)
  94. self.nl()
  95. self.nl()
  96. me = 'repo %s' % cmd.NAME
  97. body = body.strip()
  98. body = body.replace('%prog', me)
  99. asciidoc_hdr = re.compile(r'^\n?#+ (.+)$')
  100. for para in body.split("\n\n"):
  101. if para.startswith(' '):
  102. self.write('%s', para)
  103. self.nl()
  104. self.nl()
  105. continue
  106. m = asciidoc_hdr.match(para)
  107. if m:
  108. self.heading('%s%s', header_prefix, m.group(1))
  109. self.nl()
  110. self.nl()
  111. continue
  112. self.wrap.add_flowing_data(para)
  113. self.wrap.end_paragraph(1)
  114. self.wrap.end_paragraph(0)
  115. out = _Out(self.manifest.globalConfig)
  116. out._PrintSection('Summary', 'helpSummary')
  117. cmd.OptionParser.print_help()
  118. out._PrintSection('Description', 'helpDescription')
  119. def _PrintAllCommandHelp(self):
  120. for name in sorted(self.commands):
  121. cmd = self.commands[name]
  122. cmd.manifest = self.manifest
  123. self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
  124. def _Options(self, p):
  125. p.add_option('-a', '--all',
  126. dest='show_all', action='store_true',
  127. help='show the complete list of commands')
  128. p.add_option('--help-all',
  129. dest='show_all_help', action='store_true',
  130. help='show the --help of all commands')
  131. def Execute(self, opt, args):
  132. if len(args) == 0:
  133. if opt.show_all_help:
  134. self._PrintAllCommandHelp()
  135. elif opt.show_all:
  136. self._PrintAllCommands()
  137. else:
  138. self._PrintCommonCommands()
  139. elif len(args) == 1:
  140. name = args[0]
  141. try:
  142. cmd = self.commands[name]
  143. except KeyError:
  144. print("repo: '%s' is not a repo command." % name, file=sys.stderr)
  145. sys.exit(1)
  146. cmd.manifest = self.manifest
  147. self._PrintCommandHelp(cmd)
  148. else:
  149. self._PrintCommandHelp(self)