-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmake_command_docs.py
executable file
·187 lines (146 loc) · 6.39 KB
/
make_command_docs.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
#!/usr/bin/env python3
####################################################################################################
# Title: make_command_docs.py; Author Greg Propf; Date 2022-02-01
####################################################################################################
# Usage: './make_command_docs' This script runs the 'vast.py' command
# with the --help option to generate the list of commands and then again
# for each command shown by help. It does some minimal formatting on
# the results and produces a Markdown version of the commands and their
# options.
####################################################################################################
import re
import subprocess
import sys
import os
import pty
import subprocess
import shutil
import json
def snip_lines_from_text(text: str, start_line: int, end_line: int = None, invert: bool = False):
text_lines = text.split("\n")
# for x in text_lines:
# print(x)
if invert:
return text_lines[:start_line] + text_lines[end_line:]
if end_line is not None:
return text_lines[start_line:end_line]
else:
return text_lines[start_line:]
def make_lines_into_links(text_lines: list):
return "\n".join([re.sub(r"\s*(.+)\s*", r"[\1](#\1) ", line) for line in text_lines])
def run_cmd_and_capture_output(verb: str, obj: str = None, direct_obj: str = None) -> str:
# Get terminal size
columns, rows = shutil.get_terminal_size()
# Create a pseudo-terminal with the same size
master, slave = pty.openpty()
# Set the terminal size of the slave
os.system(f'stty cols {columns} rows {rows} < /dev/pts/{os.minor(os.fstat(slave).st_rdev)}')
# Execute the command
#proc = subprocess.Popen(["./vast.py", "--help"], stdout=slave, stderr=slave)
if verb:
if direct_obj:
cmd_output = subprocess.run(["./vast.py", verb, obj, direct_obj, "--help"], stdout=subprocess.PIPE)
#proc = subprocess.Popen(["./vast.py", verb, obj, direct_obj, "--help"], stdout=slave, stderr=slave)
elif obj:
cmd_output = subprocess.run(["./vast.py", verb, obj, "--help"], stdout=subprocess.PIPE)
#proc = subprocess.Popen(["./vast.py", verb, obj, "--help"], stdout=slave, stderr=slave)
else:
cmd_output = subprocess.run(["./vast.py", verb, "--help"], stdout=subprocess.PIPE)
#proc = subprocess.Popen(["./vast.py", verb, "--help"], stdout=slave, stderr=slave)
else:
cmd_output = subprocess.run(["./vast.py", "--help"], stdout=subprocess.PIPE)
#cmd_output = subprocess.Popen(["./vast.py", "--help"], stdout=subprocess.PIPE)
command = "./vast.py --help"
os.system("resize -s 128 128")
res = os.popen(command).read() # get all content as text
#res = list(os.popen(command)) # get lines as array elements
return res
# Read the output
#cmd_output = os.fdopen(master).read()
#return cmd_output.stdout
cmd_output_text = cmd_output.stdout.decode('utf-8')
# Close the pseudo-terminal
os.close(slave)
os.close(master)
return cmd_output_text
def run_help_for_commands(lines):
help_text = ""
i = 1
for line in lines:
cmd_output = ""
line_parts = re.split(r"\s{2,}", line.strip())
command_parts = re.split(r"\s+", line_parts[0])
if len(line_parts) < 2:
line_parts.append("")
print(f"{i}: {command_parts}")
i += 1
num_command_parts = len(command_parts)
if num_command_parts == 0:
continue
if num_command_parts == 3:
cmd_output = run_cmd_and_capture_output(command_parts[0], command_parts[1], command_parts[2])
if num_command_parts == 2:
cmd_output = run_cmd_and_capture_output(command_parts[0], command_parts[1])
elif num_command_parts == 1:
cmd_output = run_cmd_and_capture_output(command_parts[0])
help_text += f"## {line_parts[0]} \n {line_parts[1]}\n\n"
#help_text += f"\n{cmd_output}\n\n---\n"
help_text += f"```\n{cmd_output}\n```\n---\n"
return help_text
def parse_commands(lines):
commands = []
help_text = ""
i = 1
for line in lines:
cmd_output = ""
line_parts = re.split(r"\s{2,}", line.strip())
command_parts = re.split(r"\s+", line_parts[0])
if len(line_parts) < 2:
line_parts.append("")
print(f"{i}: {command_parts}")
i += 1
num_command_parts = len(command_parts)
if num_command_parts == 0:
continue
if num_command_parts == 3:
cmd_output = run_cmd_and_capture_output(command_parts[0], command_parts[1], command_parts[2])
if num_command_parts == 2:
cmd_output = run_cmd_and_capture_output(command_parts[0], command_parts[1])
elif num_command_parts == 1:
cmd_output = run_cmd_and_capture_output(command_parts[0])
help_text += f"## {line_parts[0]} \n {line_parts[1]}\n\n"
#help_text += f"\n{cmd_output}\n\n---\n"
help_text += f"```\n{cmd_output}\n```\n---\n"
command = {"name": line_parts[0], "summary": line_parts[1], "details": cmd_output}
commands.append(command)
return commands
main_help_text = run_cmd_and_capture_output("", "")
snipped_help_text = snip_lines_from_text(main_help_text, start_line=7, end_line=None)
snipped_help_text = sorted(list(map(str.strip, snipped_help_text)))
commands = parse_commands(snipped_help_text)
# with open("commands.json", "w") as f:
# json.dump(commands, f)
command_help_text = ""
command_help_text += f"\n# Client Commands \n\n"
for c in commands:
if "[Host]" not in c["summary"]:
name = c['name']
summary = c['summary']
details = c['details']
if name == "" or name[:1] == "-" or details == "":
continue
command_help_text += f"\n## {name} \n\n {summary}\n\n"
command_help_text += f"```\n{details}\n```\n\n"
command_help_text += f"\n# Host Commands \n\n"
for c in commands:
if "[Host]" in c["summary"]:
name = c['name']
summary = c['summary']
details = c['details']
command_help_text += f"\n## {name} \n\n {summary}\n\n"
command_help_text += f"```\n{details}\n```\n\n"
out_text = "# CLI Commands \n\n"
out_text += f"```\n{main_help_text}\n```\n"
with open("commands.md", "w") as fh:
fh.write(out_text)
fh.write(command_help_text)