help.py 5.1 KB

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