Debian Codegraph @

scripts/generate_command_help.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2020 Arm Limited
4 # SPDX-License-Identifier: Apache-2.0
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 import sys
19
20 if sys.version_info[:2] < (3, 9):
21 print(f"This script requires Python 3.9 or later")
22 sys.exit(1)
23
24 from pyocd.commands.base import (
25 ALL_COMMANDS,
26 CommandBase,
27 ValueBase,
28 )
29
30 # These modules must be imported in order to load the commands into the ALL_COMMANDS table.
31 import pyocd.commands.commands
32 import pyocd.commands.values
33
34 ACCESS_DESC = {
35 'r': "read-only",
36 "w": "write-only",
37 "rw": "read-write",
38 }
39
40 GROUP_DOCS = {
41 'bringup': "These commands are meant to be used when starting up Commander in no-init mode. They are primarily useful for low-level debugging of debug infrastructure on a new chip.",
42 'symbols': "These commands require an ELF to be set.",
43 }
44
45 def gen_command(info):
46 names = info['names']
47 usage = info['usage']
48 help = info['help']
49 extra_help = info.get('extra_help')
50 print("""<tr><td>""")
51 name_docs = []
52 for name in names:
53 name_docs.append(f"""<a href="#{names[0]}"><tt>{name}</tt></a>""")
54 print(",\n".join(name_docs))
55 print("""</td><td>""")
56 if usage:
57 print(usage)
58 print("""</td><td>""")
59 print(help)
60 print("""</td></tr>""")
61 print()
62
63 def gen_value(info):
64 names = info['names']
65 access = info['access']
66 help = info['help']
67 extra_help = info.get('extra_help')
68 print("""<tr><td>""")
69 name_docs = []
70 for name in names:
71 name_docs.append(f"""<a href="#{names[0]}"><tt>{name}</tt></a>""")
72 print(",\n".join(name_docs))
73 print("""</td><td>""")
74 print(ACCESS_DESC[access])
75 print("""</td><td>""")
76 print(help)
77 print("""</td></tr>""")
78 print()
79
80 def build_categories(commands):
81 categories = {}
82 for cmdlist in commands.values():
83 for cmd in cmdlist:
84 categories.setdefault(cmd.INFO['category'], []).append(cmd)
85 return categories
86
87 def get_all_values(commands: dict[str, list[ValueBase]]) -> list[ValueBase]:
88 """Converts dict of group name to list of commands"""
89 return [
90 c
91 for group_commands in commands.values()
92 for c in group_commands
93 ]
94
95 def gen_cmd_groups(commands):
96 categories = build_categories(commands)
97
98 for group in sorted(categories.keys()):
99 # Filter out the base classes that have empty 'names'.
100 filtered_cmds = [c for c in categories[group] if c.INFO['names']]
101 group_cmds = sorted(filtered_cmds, key=lambda c: c.INFO['names'][0])
102
103 # Skip empty groups.
104 if not group_cmds:
105 continue
106
107 print(f"""<tr><td colspan="3"><b>{group.capitalize()}</b></td></tr>
108 """)
109
110 for cmd in group_cmds:
111 gen_command(cmd.INFO)
112
113 def gen_value_groups(commands: list[ValueBase]) -> None:
114 # Filter out the base classes that have empty 'names'.
115 filtered_cmds = [c for c in commands if c.INFO['names']]
116 group_cmds = sorted(filtered_cmds, key=lambda c: c.INFO['names'][0])
117
118 # print(f"""<tr><td colspan="3"><b>{group.capitalize()}</b></td></tr>""")
119
120 for cmd in group_cmds:
121 gen_value(cmd.INFO)
122
123 def format_group_name(group: str) -> str:
124 return group.replace('_', ' ').capitalize()
125
126 def gen_command_docs(commands):
127 nl = "\\"
128 categories = build_categories(commands)
129 for group in sorted(categories.keys()):
130 # Filter out the base classes that have empty 'names'.
131 filtered_cmds = [c for c in categories[group] if c.INFO['names']]
132 group_cmds = sorted(filtered_cmds, key=lambda c: c.INFO['names'][0])
133
134 # Skip empty groups.
135 if not group_cmds:
136 continue
137
138 group_docs = GROUP_DOCS.get(group, '')
139 print(f"""
140 ### {format_group_name(group)}""")
141 if group_docs:
142 print(group_docs)
143
144 for cmd in group_cmds:
145 info = cmd.INFO
146 print(f"""
147 ##### `{info['names'][0]}`
148 """)
149 if len(info['names']) > 1:
150 print(f"""**Aliases**: {', '.join("`%s`" % n for n in info['names'][1:])} """ + nl)
151
152 help = info['help']
153 if 'extra_help' in info:
154 help += " " + info['extra_help']
155 print(f"""**Usage**: {info['names'][0]} {info['usage']} {nl}
156 {help}
157 """)
158
159 def gen_value_docs(commands: list[ValueBase]) -> None:
160 nl = "\\"
161 # Filter out the base classes that have empty 'names'.
162 filtered_cmds = [c for c in commands if c.INFO['names']]
163 group_cmds = sorted(filtered_cmds, key=lambda c: c.INFO['names'][0])
164
165 for cmd in group_cmds:
166 info = cmd.INFO
167 names = info['names']
168 first_name = names[0]
169 access = info['access']
170 access_desc = ACCESS_DESC[access]
171 help = info['help']
172 if 'extra_help' in info:
173 help += " " + info['extra_help']
174
175 print(f"""
176 ##### `{first_name}`
177 """)
178 if len(names) > 1:
179 print(f"""**Aliases**: {', '.join("`%s`" % n for n in names[1:])} """ + nl)
180
181 print(f"**Access**: {access_desc} {nl}")
182 print(f"**Usage**: ", end='')
183 if 'r' in access:
184 print(f"show {first_name}", end='')
185 if access == 'rw':
186 print(", ", end='')
187 if 'w' in access:
188 print(f"set {first_name} VALUE", end='')
189 print(f" {nl}")
190
191 print(help)
192
193 def get_all_command_classes():
194 klasses = set()
195 for cmds in ALL_COMMANDS.values():
196 klasses.update(cmds)
197 return klasses
198
199 def split_into_commands_and_values():
200 commands = get_all_command_classes()
201 value_classes = {klass for klass in commands if issubclass(klass, ValueBase)}
202 cmd_classes = commands - value_classes
203 cmd_groups = {}
204 value_groups = {}
205 for cmd in cmd_classes:
206 cmd_groups.setdefault(cmd.INFO['group'], set()).add(cmd)
207 for val in value_classes:
208 value_groups.setdefault(val.INFO['group'], set()).add(val)
209 return cmd_groups, value_groups
210
211 def main():
212 all_cmds_by_group, all_values_by_group = split_into_commands_and_values()
213 all_values = get_all_values(all_values_by_group)
214 print("""
215 All commands
216 ------------
217
218 <table>
219
220 <tr><th>Command</th><th>Arguments</th><th>Description</th></tr>
221 """)
222 gen_cmd_groups(all_cmds_by_group)
223 print("""
224 </table>
225 """)
226
227 print("""
228 All values
229 ----------
230
231 Values represent a setting or piece of information that can be read and/or changed. They are accessed with
232 the [`show`](#show) and [`set`](#set) commands. The "Access" column of the table below shows whether the
233 command can be read, written, or both.
234
235 <table>
236
237 <tr><th>Value</th><th>Access</th><th>Description</th></tr>
238 """)
239 gen_value_groups(all_values)
240 print("""
241 </table>
242 """)
243
244 print("""
245 Command details
246 ---------------""")
247 gen_command_docs(all_cmds_by_group)
248
249 print("""
250
251 Value details
252 -------------""")
253 gen_value_docs(all_values)
254
255 if __name__ == '__main__':
256 main()
257
258