The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 15 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import sys |
Mike Frysinger | 0888a08 | 2021-04-13 20:22:01 -0400 | [diff] [blame] | 17 | import textwrap |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 19 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | from color import Coloring |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 21 | from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 22 | import gitc_utils |
Mike Frysinger | a1cd770 | 2021-04-20 23:38:04 -0400 | [diff] [blame] | 23 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 25 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 26 | class Help(PagedCommand, MirrorSafeCommand): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 27 | COMMON = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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: |
Mike Frysinger | bb93046 | 2020-02-25 15:18:31 -0500 | [diff] [blame] | 44 | command = all_commands[name]() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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:') |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 54 | commandNames = list(sorted(all_commands)) |
Mike Frysinger | 0b304c0 | 2019-12-02 16:49:13 -0500 | [diff] [blame] | 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 |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 66 | if self.client.isGitcClient: |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 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 |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 75 | for name, command in all_commands.items() |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [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( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 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.") |
Mike Frysinger | a1cd770 | 2021-04-20 23:38:04 -0400 | [diff] [blame] | 82 | print('Bug reports:', Wrapper().BUG_URL) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 84 | def _PrintCommandHelp(self, cmd, header_prefix=''): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | class _Out(Coloring): |
| 86 | def __init__(self, gc): |
| 87 | Coloring.__init__(self, gc, 'help') |
| 88 | self.heading = self.printer('heading', attr='bold') |
Mike Frysinger | 0888a08 | 2021-04-13 20:22:01 -0400 | [diff] [blame] | 89 | self._first = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Mike Frysinger | 0888a08 | 2021-04-13 20:22:01 -0400 | [diff] [blame] | 99 | if not self._first: |
| 100 | self.nl() |
| 101 | self._first = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 103 | self.heading('%s%s', header_prefix, heading) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | self.nl() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | self.nl() |
| 106 | |
| 107 | me = 'repo %s' % cmd.NAME |
| 108 | body = body.strip() |
| 109 | body = body.replace('%prog', me) |
| 110 | |
Mike Frysinger | 0888a08 | 2021-04-13 20:22:01 -0400 | [diff] [blame] | 111 | # Extract the title, but skip any trailing {#anchors}. |
| 112 | asciidoc_hdr = re.compile(r'^\n?#+ ([^{]+)(\{#.+\})?$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | for para in body.split("\n\n"): |
| 114 | if para.startswith(' '): |
| 115 | self.write('%s', para) |
| 116 | self.nl() |
| 117 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 118 | continue |
| 119 | |
| 120 | m = asciidoc_hdr.match(para) |
| 121 | if m: |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 122 | self.heading('%s%s', header_prefix, m.group(1)) |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 123 | self.nl() |
Shawn O. Pearce | 43c3d9e | 2009-03-04 14:26:50 -0800 | [diff] [blame] | 124 | self.nl() |
| 125 | continue |
| 126 | |
Mike Frysinger | 0888a08 | 2021-04-13 20:22:01 -0400 | [diff] [blame] | 127 | lines = textwrap.wrap(para.replace(' ', ' '), width=80, |
| 128 | break_long_words=False, break_on_hyphens=False) |
| 129 | for line in lines: |
| 130 | self.write('%s', line) |
| 131 | self.nl() |
| 132 | self.nl() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 134 | out = _Out(self.client.globalConfig) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | out._PrintSection('Summary', 'helpSummary') |
Shawn O. Pearce | 5da554f | 2009-04-18 11:44:00 -0700 | [diff] [blame] | 136 | cmd.OptionParser.print_help() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | out._PrintSection('Description', 'helpDescription') |
| 138 | |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 139 | def _PrintAllCommandHelp(self): |
Mike Frysinger | d3639c5 | 2020-02-25 15:12:37 -0500 | [diff] [blame] | 140 | for name in sorted(all_commands): |
Mike Frysinger | d58d0dd | 2021-06-14 16:17:27 -0400 | [diff] [blame^] | 141 | cmd = all_commands[name](manifest=self.manifest) |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 142 | self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,)) |
| 143 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 144 | def _Options(self, p): |
| 145 | p.add_option('-a', '--all', |
| 146 | dest='show_all', action='store_true', |
| 147 | help='show the complete list of commands') |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 148 | p.add_option('--help-all', |
| 149 | dest='show_all_help', action='store_true', |
| 150 | help='show the --help of all commands') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | |
| 152 | def Execute(self, opt, args): |
| 153 | if len(args) == 0: |
Mike Frysinger | 898f4e6 | 2019-07-31 18:17:44 -0400 | [diff] [blame] | 154 | if opt.show_all_help: |
| 155 | self._PrintAllCommandHelp() |
| 156 | elif opt.show_all: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 157 | self._PrintAllCommands() |
| 158 | else: |
| 159 | self._PrintCommonCommands() |
| 160 | |
| 161 | elif len(args) == 1: |
| 162 | name = args[0] |
| 163 | |
| 164 | try: |
Mike Frysinger | d58d0dd | 2021-06-14 16:17:27 -0400 | [diff] [blame^] | 165 | cmd = all_commands[name](manifest=self.manifest) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | self._PrintCommandHelp(cmd) |
| 171 | |
| 172 | else: |
| 173 | self._PrintCommandHelp(self) |