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