help.py 5.1 KB

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