help.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.heading('%s', ''.ljust(len(heading), '-'))
  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?([^\n]{1,})\n([=~-]{2,})$')
  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. title = m.group(1)
  109. section_type = m.group(2)
  110. if section_type[0] in ('=', '-'):
  111. p = self.heading
  112. else:
  113. def _p(fmt, *args):
  114. self.write(' ')
  115. self.heading(fmt, *args)
  116. p = _p
  117. p('%s', title)
  118. self.nl()
  119. p('%s', ''.ljust(len(title), section_type[0]))
  120. self.nl()
  121. continue
  122. self.wrap.add_flowing_data(para)
  123. self.wrap.end_paragraph(1)
  124. self.wrap.end_paragraph(0)
  125. out = _Out(self.manifest.globalConfig)
  126. out._PrintSection('Summary', 'helpSummary')
  127. cmd.OptionParser.print_help()
  128. out._PrintSection('Description', 'helpDescription')
  129. def _Options(self, p):
  130. p.add_option('-a', '--all',
  131. dest='show_all', action='store_true',
  132. help='show the complete list of commands')
  133. def Execute(self, opt, args):
  134. if len(args) == 0:
  135. if 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)