summaryrefslogtreecommitdiffstats
path: root/subcmds/help.py
diff options
context:
space:
mode:
authorGavin Mak <gavinmak@google.com>2023-03-11 06:46:20 +0000
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-22 17:46:28 +0000
commitea2e330e43c182dc16b0111ebc69ee5a71ee4ce1 (patch)
treedc33ba0e56825b3e007d0589891756724725a465 /subcmds/help.py
parent1604cf255f8c1786a23388db6d5277ac7949a24a (diff)
downloadgit-repo-ea2e330e43c182dc16b0111ebc69ee5a71ee4ce1.tar.gz
Format codebase with black and check formatting in CQ
Apply rules set by https://gerrit-review.googlesource.com/c/git-repo/+/362954/ across the codebase and fix any lingering errors caught by flake8. Also check black formatting in run_tests (and CQ). Bug: b/267675342 Change-Id: I972d77649dac351150dcfeb1cd1ad0ea2efc1956 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/363474 Reviewed-by: Mike Frysinger <vapier@google.com> Tested-by: Gavin Mak <gavinmak@google.com> Commit-Queue: Gavin Mak <gavinmak@google.com>
Diffstat (limited to 'subcmds/help.py')
-rw-r--r--subcmds/help.py330
1 files changed, 180 insertions, 150 deletions
diff --git a/subcmds/help.py b/subcmds/help.py
index 1ad391db..50a48047 100644
--- a/subcmds/help.py
+++ b/subcmds/help.py
@@ -18,163 +18,193 @@ import textwrap
18 18
19from subcmds import all_commands 19from subcmds import all_commands
20from color import Coloring 20from color import Coloring
21from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand 21from command import (
22 PagedCommand,
23 MirrorSafeCommand,
24 GitcAvailableCommand,
25 GitcClientCommand,
26)
22import gitc_utils 27import gitc_utils
23from wrapper import Wrapper 28from wrapper import Wrapper
24 29
25 30
26class Help(PagedCommand, MirrorSafeCommand): 31class Help(PagedCommand, MirrorSafeCommand):
27 COMMON = False 32 COMMON = False
28 helpSummary = "Display detailed help on a command" 33 helpSummary = "Display detailed help on a command"
29 helpUsage = """ 34 helpUsage = """
30%prog [--all|command] 35%prog [--all|command]
31""" 36"""
32 helpDescription = """ 37 helpDescription = """
33Displays detailed usage information about a command. 38Displays detailed usage information about a command.
34""" 39"""
35 40
36 def _PrintCommands(self, commandNames): 41 def _PrintCommands(self, commandNames):
37 """Helper to display |commandNames| summaries.""" 42 """Helper to display |commandNames| summaries."""
38 maxlen = 0 43 maxlen = 0
39 for name in commandNames: 44 for name in commandNames:
40 maxlen = max(maxlen, len(name)) 45 maxlen = max(maxlen, len(name))
41 fmt = ' %%-%ds %%s' % maxlen 46 fmt = " %%-%ds %%s" % maxlen
42 47
43 for name in commandNames: 48 for name in commandNames:
44 command = all_commands[name]() 49 command = all_commands[name]()
45 try: 50 try:
46 summary = command.helpSummary.strip() 51 summary = command.helpSummary.strip()
47 except AttributeError: 52 except AttributeError:
48 summary = '' 53 summary = ""
49 print(fmt % (name, summary)) 54 print(fmt % (name, summary))
50 55
51 def _PrintAllCommands(self): 56 def _PrintAllCommands(self):
52 print('usage: repo COMMAND [ARGS]') 57 print("usage: repo COMMAND [ARGS]")
53 self.PrintAllCommandsBody() 58 self.PrintAllCommandsBody()
54 59
55 def PrintAllCommandsBody(self): 60 def PrintAllCommandsBody(self):
56 print('The complete list of recognized repo commands is:') 61 print("The complete list of recognized repo commands is:")
57 commandNames = list(sorted(all_commands)) 62 commandNames = list(sorted(all_commands))
58 self._PrintCommands(commandNames) 63 self._PrintCommands(commandNames)
59 print("See 'repo help <command>' for more information on a " 64 print(
60 'specific command.') 65 "See 'repo help <command>' for more information on a "
61 print('Bug reports:', Wrapper().BUG_URL) 66 "specific command."
62 67 )
63 def _PrintCommonCommands(self): 68 print("Bug reports:", Wrapper().BUG_URL)
64 print('usage: repo COMMAND [ARGS]') 69
65 self.PrintCommonCommandsBody() 70 def _PrintCommonCommands(self):
66 71 print("usage: repo COMMAND [ARGS]")
67 def PrintCommonCommandsBody(self): 72 self.PrintCommonCommandsBody()
68 print('The most commonly used repo commands are:') 73
69 74 def PrintCommonCommandsBody(self):
70 def gitc_supported(cmd): 75 print("The most commonly used repo commands are:")
71 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand): 76
72 return True 77 def gitc_supported(cmd):
73 if self.client.isGitcClient: 78 if not isinstance(cmd, GitcAvailableCommand) and not isinstance(
74 return True 79 cmd, GitcClientCommand
75 if isinstance(cmd, GitcClientCommand): 80 ):
76 return False 81 return True
77 if gitc_utils.get_gitc_manifest_dir(): 82 if self.client.isGitcClient:
78 return True 83 return True
79 return False 84 if isinstance(cmd, GitcClientCommand):
80 85 return False
81 commandNames = list(sorted([name 86 if gitc_utils.get_gitc_manifest_dir():
82 for name, command in all_commands.items() 87 return True
83 if command.COMMON and gitc_supported(command)])) 88 return False
84 self._PrintCommands(commandNames) 89
85 90 commandNames = list(
86 print( 91 sorted(
87 "See 'repo help <command>' for more information on a specific command.\n" 92 [
88 "See 'repo help --all' for a complete list of recognized commands.") 93 name
89 print('Bug reports:', Wrapper().BUG_URL) 94 for name, command in all_commands.items()
90 95 if command.COMMON and gitc_supported(command)
91 def _PrintCommandHelp(self, cmd, header_prefix=''): 96 ]
92 class _Out(Coloring): 97 )
93 def __init__(self, gc): 98 )
94 Coloring.__init__(self, gc, 'help') 99 self._PrintCommands(commandNames)
95 self.heading = self.printer('heading', attr='bold') 100
96 self._first = True 101 print(
97 102 "See 'repo help <command>' for more information on a specific "
98 def _PrintSection(self, heading, bodyAttr): 103 "command.\nSee 'repo help --all' for a complete list of recognized "
99 try: 104 "commands."
100 body = getattr(cmd, bodyAttr) 105 )
101 except AttributeError: 106 print("Bug reports:", Wrapper().BUG_URL)
102 return 107
103 if body == '' or body is None: 108 def _PrintCommandHelp(self, cmd, header_prefix=""):
104 return 109 class _Out(Coloring):
105 110 def __init__(self, gc):
106 if not self._first: 111 Coloring.__init__(self, gc, "help")
107 self.nl() 112 self.heading = self.printer("heading", attr="bold")
108 self._first = False 113 self._first = True
109 114
110 self.heading('%s%s', header_prefix, heading) 115 def _PrintSection(self, heading, bodyAttr):
111 self.nl() 116 try:
112 self.nl() 117 body = getattr(cmd, bodyAttr)
113 118 except AttributeError:
114 me = 'repo %s' % cmd.NAME 119 return
115 body = body.strip() 120 if body == "" or body is None:
116 body = body.replace('%prog', me) 121 return
117 122
118 # Extract the title, but skip any trailing {#anchors}. 123 if not self._first:
119 asciidoc_hdr = re.compile(r'^\n?#+ ([^{]+)(\{#.+\})?$') 124 self.nl()
120 for para in body.split("\n\n"): 125 self._first = False
121 if para.startswith(' '): 126
122 self.write('%s', para) 127 self.heading("%s%s", header_prefix, heading)
123 self.nl() 128 self.nl()
124 self.nl() 129 self.nl()
125 continue 130
126 131 me = "repo %s" % cmd.NAME
127 m = asciidoc_hdr.match(para) 132 body = body.strip()
128 if m: 133 body = body.replace("%prog", me)
129 self.heading('%s%s', header_prefix, m.group(1)) 134
130 self.nl() 135 # Extract the title, but skip any trailing {#anchors}.
131 self.nl() 136 asciidoc_hdr = re.compile(r"^\n?#+ ([^{]+)(\{#.+\})?$")
132 continue 137 for para in body.split("\n\n"):
133 138 if para.startswith(" "):
134 lines = textwrap.wrap(para.replace(' ', ' '), width=80, 139 self.write("%s", para)
135 break_long_words=False, break_on_hyphens=False) 140 self.nl()
136 for line in lines: 141 self.nl()
137 self.write('%s', line) 142 continue
138 self.nl() 143
139 self.nl() 144 m = asciidoc_hdr.match(para)
140 145 if m:
141 out = _Out(self.client.globalConfig) 146 self.heading("%s%s", header_prefix, m.group(1))
142 out._PrintSection('Summary', 'helpSummary') 147 self.nl()
143 cmd.OptionParser.print_help() 148 self.nl()
144 out._PrintSection('Description', 'helpDescription') 149 continue
145 150
146 def _PrintAllCommandHelp(self): 151 lines = textwrap.wrap(
147 for name in sorted(all_commands): 152 para.replace(" ", " "),
148 cmd = all_commands[name](manifest=self.manifest) 153 width=80,
149 self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,)) 154 break_long_words=False,
150 155 break_on_hyphens=False,
151 def _Options(self, p): 156 )
152 p.add_option('-a', '--all', 157 for line in lines:
153 dest='show_all', action='store_true', 158 self.write("%s", line)
154 help='show the complete list of commands') 159 self.nl()
155 p.add_option('--help-all', 160 self.nl()
156 dest='show_all_help', action='store_true', 161
157 help='show the --help of all commands') 162 out = _Out(self.client.globalConfig)
158 163 out._PrintSection("Summary", "helpSummary")
159 def Execute(self, opt, args): 164 cmd.OptionParser.print_help()
160 if len(args) == 0: 165 out._PrintSection("Description", "helpDescription")
161 if opt.show_all_help: 166
162 self._PrintAllCommandHelp() 167 def _PrintAllCommandHelp(self):
163 elif opt.show_all: 168 for name in sorted(all_commands):
164 self._PrintAllCommands() 169 cmd = all_commands[name](manifest=self.manifest)
165 else: 170 self._PrintCommandHelp(cmd, header_prefix="[%s] " % (name,))
166 self._PrintCommonCommands() 171
167 172 def _Options(self, p):
168 elif len(args) == 1: 173 p.add_option(
169 name = args[0] 174 "-a",
170 175 "--all",
171 try: 176 dest="show_all",
172 cmd = all_commands[name](manifest=self.manifest) 177 action="store_true",
173 except KeyError: 178 help="show the complete list of commands",
174 print("repo: '%s' is not a repo command." % name, file=sys.stderr) 179 )
175 sys.exit(1) 180 p.add_option(
176 181 "--help-all",
177 self._PrintCommandHelp(cmd) 182 dest="show_all_help",
178 183 action="store_true",
179 else: 184 help="show the --help of all commands",
180 self._PrintCommandHelp(self) 185 )
186
187 def Execute(self, opt, args):
188 if len(args) == 0:
189 if opt.show_all_help:
190 self._PrintAllCommandHelp()
191 elif opt.show_all:
192 self._PrintAllCommands()
193 else:
194 self._PrintCommonCommands()
195
196 elif len(args) == 1:
197 name = args[0]
198
199 try:
200 cmd = all_commands[name](manifest=self.manifest)
201 except KeyError:
202 print(
203 "repo: '%s' is not a repo command." % name, file=sys.stderr
204 )
205 sys.exit(1)
206
207 self._PrintCommandHelp(cmd)
208
209 else:
210 self._PrintCommandHelp(self)