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