help.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, RequiresGitcCommand
  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, RequiresGitcCommand):
  53. return True
  54. if gitc_utils.get_gitc_manifest_dir():
  55. return True
  56. return False
  57. commandNames = list(sorted([name
  58. for name, command in self.commands.items()
  59. if command.common and gitc_supported(command)]))
  60. maxlen = 0
  61. for name in commandNames:
  62. maxlen = max(maxlen, len(name))
  63. fmt = ' %%-%ds %%s' % maxlen
  64. for name in commandNames:
  65. command = self.commands[name]
  66. try:
  67. summary = command.helpSummary.strip()
  68. except AttributeError:
  69. summary = ''
  70. print(fmt % (name, summary))
  71. print(
  72. "See 'repo help <command>' for more information on a specific command.\n"
  73. "See 'repo help --all' for a complete list of recognized commands.")
  74. def _PrintCommandHelp(self, cmd):
  75. class _Out(Coloring):
  76. def __init__(self, gc):
  77. Coloring.__init__(self, gc, 'help')
  78. self.heading = self.printer('heading', attr='bold')
  79. self.wrap = AbstractFormatter(DumbWriter())
  80. def _PrintSection(self, heading, bodyAttr):
  81. try:
  82. body = getattr(cmd, bodyAttr)
  83. except AttributeError:
  84. return
  85. if body == '' or body is None:
  86. return
  87. self.nl()
  88. self.heading('%s', heading)
  89. self.nl()
  90. self.heading('%s', ''.ljust(len(heading), '-'))
  91. self.nl()
  92. me = 'repo %s' % cmd.NAME
  93. body = body.strip()
  94. body = body.replace('%prog', me)
  95. asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$')
  96. for para in body.split("\n\n"):
  97. if para.startswith(' '):
  98. self.write('%s', para)
  99. self.nl()
  100. self.nl()
  101. continue
  102. m = asciidoc_hdr.match(para)
  103. if m:
  104. title = m.group(1)
  105. section_type = m.group(2)
  106. if section_type[0] in ('=', '-'):
  107. p = self.heading
  108. else:
  109. def _p(fmt, *args):
  110. self.write(' ')
  111. self.heading(fmt, *args)
  112. p = _p
  113. p('%s', title)
  114. self.nl()
  115. p('%s', ''.ljust(len(title), section_type[0]))
  116. self.nl()
  117. continue
  118. self.wrap.add_flowing_data(para)
  119. self.wrap.end_paragraph(1)
  120. self.wrap.end_paragraph(0)
  121. out = _Out(self.manifest.globalConfig)
  122. out._PrintSection('Summary', 'helpSummary')
  123. cmd.OptionParser.print_help()
  124. out._PrintSection('Description', 'helpDescription')
  125. def _Options(self, p):
  126. p.add_option('-a', '--all',
  127. dest='show_all', action='store_true',
  128. help='show the complete list of commands')
  129. def Execute(self, opt, args):
  130. if len(args) == 0:
  131. if opt.show_all:
  132. self._PrintAllCommands()
  133. else:
  134. self._PrintCommonCommands()
  135. elif len(args) == 1:
  136. name = args[0]
  137. try:
  138. cmd = self.commands[name]
  139. except KeyError:
  140. print("repo: '%s' is not a repo command." % name, file=sys.stderr)
  141. sys.exit(1)
  142. cmd.manifest = self.manifest
  143. self._PrintCommandHelp(cmd)
  144. else:
  145. self._PrintCommandHelp(self)