-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.py
334 lines (266 loc) · 9.89 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import re
import subprocess
from typing import Tuple
from library import format_readme_titles
PATTERN = re.compile(r"(.*?) +(.*)")
# ( - Start of capture
# .*? - 0 or more repetitions of any character except a new line (non-greedy)
# ) - End of capture
# + - 2 or more repetitions of space
# ( - Start of capture
# .* - 0 of more repetitions of any character except a new line
# ) - End of group
KEYWORDS = ["Options:", "Commands:"]
TEMPLATE = """---
title: {}
---
{}
{}
{}
{}
"""
def build(output_dir: str = None):
"""
Entry point for docgen_cli.
Builds the entire documentation for `wandb` CLI
by calling wandb --help, parsing the output,
and then doing the same for each subcommand.
Args:
output_dir: (str) The output directory for the generated CLI docs.
"""
if output_dir is None:
output_dir = os.getcwd()
output_dir, output_file = prepare_dirs(output_dir, "cli")
markdown_render("wandb", output_dir, output_file)
def markdown_render(command: str, output_dir: str, output_file: str) -> str:
"""
Renders the markdown and also provides
the commands nested in it.
Args:
command: (str) The command that is executed as `wandb command --help`
output_file: (str) The file in which the markdown is written.
Returns:
str: The output directory
"""
usage, summary, parsed_dict = parse_help(command)
if usage:
# Document usage
usage = usage.split(":")
md_usage = usage[1].strip()
usage = f"**Usage**\n\n`{md_usage}`"
if summary:
# Document summary
summary = f"**Summary**\n{summary}"
if "Options:" in parsed_dict.keys():
options = get_options_markdown(parsed_dict["Options:"])
else:
options = ""
if "Commands:" in parsed_dict.keys():
subcommands, subcommand_list = get_subcommands_markdown(
command, parsed_dict["Commands:"]
)
else:
subcommands, subcommand_list = "", []
# Write to the output file
if usage or summary or options or subcommands:
write_to_file(output_file, command, usage, summary, options, subcommands)
# render markdown for subcommands
if len(subcommand_list) > 0:
for command in subcommand_list:
# For `command --help`
command_dir_name = "-".join(command.split(" "))
output_dir, output_file = prepare_dirs(output_dir, command_dir_name)
output_dir = markdown_render(command, output_dir, output_file)
parent_path = os.path.dirname(output_dir)
return parent_path
def run_help(command: str) -> str:
"""
Runs `command --help` and gathers the output.
Args:
command: (str) The command eg. wandb in `wandb --help`
Returns:
str: The help page of the command.
"""
help_page = subprocess.run(
f"{command} --help", shell=True, capture_output=True, text=True
).stdout
return help_page
def parse_help(command: str) -> Tuple[str, str, str]:
"""
Gathers the help page of the command and then parses it.
It is noted that the help page is structured in the following manner.
```bash
Usage: ....
<Summary>
Options:
...
Commands:
...
```
This help page is parsed in Usage, Summary and a Parsed Dict
that contains Options and Commands.
Args:
command (str): The command eg. wandb in `wandb --help`
Returns:
str, str, str: usage, summary and the parsed document
"""
help_page = run_help(command)
summary = []
keyword = None # initializing keyword with None
parsed_dict = {} # will hold Options and Commands
help_page = pre_process(help_page)
full_line = ""
tmp = None
for line in help_page.split("\n"):
line = line.strip()
if line in KEYWORDS: # Keywords contains [Options, Commands]
parsed_dict[line] = []
keyword = line
continue
if keyword is None:
summary.append(line)
else:
# handle multi line options
if keyword == "Options:" and not line.startswith("-"):
full_line += " " + line
continue
elif keyword == "Options:" and line.startswith("-"):
tmp = line
line = full_line
# PATTERN helps with option and value
# eg. --version Shows the version
# will be captured like
# [("--version","Show the version")]
extract = PATTERN.findall(line)
if extract:
parsed_dict[keyword].append([extract[0][0], extract[0][1]])
if tmp is not None:
full_line = tmp
tmp = None
if len(summary) == 0:
return "", "", parsed_dict
elif len(summary) == 1:
return summary[0], "", parsed_dict
else:
usage = summary[0]
summary = "\n".join(summary[1:])
return usage, summary, parsed_dict
def pre_process(help_page: str) -> str:
"""
Transforms the help page of a command to better suit the needs of the parser.
The wandb CLI is made with the help of [click](https://click.palletsprojects.com/).
We have noticed the problem of line wrapping with this tool.
eg.
```bash
--sync-tensorboard / --no-sync-tensorboard
Stream tfevent files to wandb.
--nvidia / --no-nvidia Use the nvidia runtime, defaults to nvidia if
nvidia-docker is present
```
We find two types of overflow problems:
- new description: Due to the overflow of the option
`--sync-tensorboard / --no-sync-tensorboard`
the description `Stream tfevent files to wandb.`
is sent to the next line.
- wrapped description: Due to the overflow of the description
`Use the nvidia runtime, defaults to nvidia if`
the rest of the description is wrapped to the next line.
This breaks our parser that looks for lines with `option description`.
With this function we transform these lines into the suitable format that our parser
expects.
```bash
--sync-tensorboard / --no-sync-tensorboard Stream tfevent files to wandb.
--nvidia / --no-nvidia Use the nvidia runtime,
defaults to nvidia if nvidia-docker is present.
```
Args:
help_page: The help page of a command gathered by `parse_help()`
Returns:
str: The transformed help page.
"""
# The help page has a lot of components
# We need to sample the `option description` lines, which starts
# with white spaces.
re_space = re.compile(r"(^\s+)") # Regex to capture starting white spaces.
# Split the lines and iterate
page_splits = help_page.split("\n")
num_of_lines = len(page_splits)
for idx, line in enumerate(page_splits):
# Capture the starting white spaces of the line
white_space = re_space.findall(line)
num_white_space = len(white_space[0]) if white_space else 0
if num_white_space > 2:
# Can be either
# - wrapped description
# - new description
if num_of_lines >= idx + 1 and page_splits[idx + 1] == "":
# wrapped description
desc_1 = page_splits[idx - 1]
desc_2 = page_splits.pop(idx).strip()
page_splits[idx - 1] = desc_1 + " " + desc_2
# Remove the empty line
page_splits.pop(idx)
else:
# new description
option = page_splits[idx - 1]
description = page_splits.pop(idx).strip()
page_splits[idx - 1] = option + " " + description
return "\n".join(page_splits)
def get_options_markdown(options):
"""Formats the options of a command as a markdown table."""
options_md = ""
for element in options:
arg = parse_description(element)
desc = element[1]
# concatenate all the options
options_md += f"""| `{arg}` | {desc} |\n"""
options_md = (
"""**Options**\n\n| **Option** | **Description** |\n| :--- | :--- |\n"""
+ options_md
)
return options_md
def get_subcommands_markdown(command, subcommands):
"""Formats the subcommands of a command as a markdown table."""
subcommands_md, subcommand_list = "", []
for element in subcommands:
subcommand_list.append(
f"{command} {element[0]}"
) # Keeping a list of all the nested counts
arg = parse_description(element)
desc = element[1]
# concatenate all the options
subcommands_md += f"""| {arg} | {desc} |\n"""
subcommands_md = (
"""**Commands**\n\n| **Command** | **Description** |\n| :--- | :--- |\n"""
+ subcommands_md
)
return subcommands_md, subcommand_list
def parse_description(element):
"""
There are options in the help page where the defualt datatype
is displayed.
```bash
--shell TEXT The shell to start the container with
```
Here the datatype is TEXT.
With this function we strip the datatype off the line and return
the formatted line.
"""
markdown = " ".join(
list(filter(lambda x: "" if x.isupper() else x, element[0].split(" ")))
)
return markdown
def write_to_file(output_file, command, usage, summary, options, subcommands):
"""Write contents to the output_file based on TEMPLATE"""
contents = TEMPLATE.format(command, usage, summary, options, subcommands)
with open(output_file, "w") as fp:
fp.write(contents)
def prepare_dirs(base_dir, subdir_name):
"""Add a subdirectory with README to a base directory.
Returns the directory and README paths.
"""
subdir = os.path.join(base_dir, subdir_name)
os.mkdir(path=subdir)
markdown_file = os.path.join(subdir, "README.md")
return subdir, markdown_file