The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 17 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import sys |
| 19 | from formatter import AbstractFormatter, DumbWriter |
| 20 | |
| 21 | from color import Coloring |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 22 | from command import PagedCommand, MirrorSafeCommand, RequiresGitcCommand |
| 23 | import gitc_utils |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 25 | class Help(PagedCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | common = False |
| 27 | helpSummary = "Display detailed help on a command" |
| 28 | helpUsage = """ |
| 29 | %prog [--all|command] |
| 30 | """ |
| 31 | helpDescription = """ |
| 32 | Displays detailed usage information about a command. |
| 33 | """ |
| 34 | |
| 35 | def _PrintAllCommands(self): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 36 | print('usage: repo COMMAND [ARGS]') |
| 37 | print('The complete list of recognized repo commands are:') |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 38 | commandNames = list(sorted(self.commands)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | |
| 40 | maxlen = 0 |
| 41 | for name in commandNames: |
| 42 | maxlen = max(maxlen, len(name)) |
| 43 | fmt = ' %%-%ds %%s' % maxlen |
| 44 | |
| 45 | for name in commandNames: |
| 46 | command = self.commands[name] |
| 47 | try: |
| 48 | summary = command.helpSummary.strip() |
| 49 | except AttributeError: |
| 50 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 51 | print(fmt % (name, summary)) |
David Pursehouse | 2f9e7e4 | 2013-03-05 17:26:46 +0900 | [diff] [blame] | 52 | print("See 'repo help <command>' for more information on a " |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 53 | 'specific command.') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
| 55 | def _PrintCommonCommands(self): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 56 | print('usage: repo COMMAND [ARGS]') |
| 57 | print('The most commonly used repo commands are:') |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 58 | |
| 59 | def gitc_supported(cmd): |
| 60 | if not isinstance(cmd, RequiresGitcCommand): |
| 61 | return True |
| 62 | if gitc_utils.get_gitc_manifest_dir(): |
| 63 | return True |
| 64 | return False |
| 65 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 66 | commandNames = list(sorted([name |
| 67 | for name, command in self.commands.items() |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 68 | if command.common and gitc_supported(command)])) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 69 | |
| 70 | maxlen = 0 |
| 71 | for name in commandNames: |
| 72 | maxlen = max(maxlen, len(name)) |
| 73 | fmt = ' %%-%ds %%s' % maxlen |
| 74 | |
| 75 | for name in commandNames: |
| 76 | command = self.commands[name] |
| 77 | try: |
| 78 | summary = command.helpSummary.strip() |
| 79 | except AttributeError: |
| 80 | summary = '' |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 81 | print(fmt % (name, summary)) |
| 82 | print( |
| 83 | "See 'repo help <command>' for more information on a specific command.\n" |
| 84 | "See 'repo help --all' for a complete list of recognized commands.") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | |
| 86 | def _PrintCommandHelp(self, cmd): |
| 87 | class _Out(Coloring): |
| 88 | def __init__(self, gc): |
| 89 | Coloring.__init__(self, gc, 'help') |
| 90 | self.heading = self.printer('heading', attr='bold') |
| 91 | |
| 92 | self.wrap = AbstractFormatter(DumbWriter()) |
| 93 | |
| 94 | def _PrintSection(self, heading, bodyAttr): |
| 95 | try: |
| 96 | body = getattr(cmd, bodyAttr) |
| 97 | except AttributeError: |
| 98 | return |
Shawn O. Pearce | c7c57e3 | 2009-06-03 17:43:16 -0700 | [diff] [blame] | 99 | if body == '' or body is None: |
| 100 | return |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | |
| 102 | self.nl() |
| 103 | |
| 104 | self.heading('%s', heading) |
| 105 | self.nl() |
| 106 | |
| 107 | self.heading('%s', ''.ljust(len(heading), '-')) |
| 108 | self.nl() |
| 109 | |
| 110 | me = 'repo %s' % cmd.NAME |
| 111 | body = body.strip() |
| 112 | body = body.replace('%prog', me) |
| 113 | |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 114 | asciidoc_hdr = re.compile(r'^\n?([^\n]{1,})\n([=~-]{2,})$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | for para in body.split("\n\n"): |
| 116 | if para.startswith(' '): |
| 117 | self.write('%s', para) |
| 118 | self.nl() |
| 119 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 120 | continue |
| 121 | |
| 122 | m = asciidoc_hdr.match(para) |
| 123 | if m: |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 124 | title = m.group(1) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 125 | section_type = m.group(2) |
| 126 | if section_type[0] in ('=', '-'): |
Shawn O. Pearce | 938d608 | 2009-04-21 08:01:17 -0700 | [diff] [blame] | 127 | p = self.heading |
| 128 | else: |
| 129 | def _p(fmt, *args): |
| 130 | self.write(' ') |
| 131 | self.heading(fmt, *args) |
| 132 | p = _p |
| 133 | |
| 134 | p('%s', title) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 135 | self.nl() |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 136 | p('%s', ''.ljust(len(title), section_type[0])) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 137 | self.nl() |
| 138 | continue |
| 139 | |
| 140 | self.wrap.add_flowing_data(para) |
| 141 | self.wrap.end_paragraph(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | self.wrap.end_paragraph(0) |
| 143 | |
| 144 | out = _Out(self.manifest.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 145 | out._PrintSection('Summary', 'helpSummary') |
Shawn O. Pearce | 5da554f | 2009-04-18 11:44:00 -0700 | [diff] [blame] | 146 | cmd.OptionParser.print_help() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | out._PrintSection('Description', 'helpDescription') |
| 148 | |
| 149 | def _Options(self, p): |
| 150 | p.add_option('-a', '--all', |
| 151 | dest='show_all', action='store_true', |
| 152 | help='show the complete list of commands') |
| 153 | |
| 154 | def Execute(self, opt, args): |
| 155 | if len(args) == 0: |
| 156 | if opt.show_all: |
| 157 | self._PrintAllCommands() |
| 158 | else: |
| 159 | self._PrintCommonCommands() |
| 160 | |
| 161 | elif len(args) == 1: |
| 162 | name = args[0] |
| 163 | |
| 164 | try: |
| 165 | cmd = self.commands[name] |
| 166 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 167 | print("repo: '%s' is not a repo command." % name, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 168 | sys.exit(1) |
| 169 | |
Shawn O. Pearce | 752371d | 2011-10-11 15:23:37 -0700 | [diff] [blame] | 170 | cmd.manifest = self.manifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | self._PrintCommandHelp(cmd) |
| 172 | |
| 173 | else: |
| 174 | self._PrintCommandHelp(self) |