-
Notifications
You must be signed in to change notification settings - Fork 24
/
update_usage.py
executable file
·39 lines (33 loc) · 1.52 KB
/
update_usage.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
#!/usr/bin/env python3
import re, sys, os
import subprocess
from subprocess import Popen, PIPE
if __name__ == "__main__":
with open('docs/usage.md') as f:
content = f.read()
command = subprocess.run(["cargo", "build"])
if command.returncode != 0:
print('Cargo build exited with code ' + str(command.returncode))
exit(command.returncode)
build_dir = os.path.dirname(os.path.realpath(__file__))
target_dir = os.path.join(build_dir, "target", "debug")
for p in os.listdir(target_dir):
if p.startswith("casr-") \
and os.path.isfile(target := os.path.join(target_dir, p)) \
and os.access(target, os.X_OK):
command = Popen([target, "-h"], stdout=PIPE, stderr=PIPE)
out, _ = command.communicate()
output = str(out, 'utf-8', errors='ignore')
splitted = output.split('\n\n')
number_of_sections = len(splitted)
if number_of_sections != 4 and number_of_sections != 3:
print('Bad format in help message: ' + p)
continue
for i in range(1, number_of_sections):
splitted[i] = '\n'.join([' ' + line for line in splitted[i].split('\n') if line])
new_message = '\n\n'.join(splitted) + '\n\n'
content = re.sub(f'## {p}\n\n' + '(.|\n)*?\n\n(\s+.*\n\n?)+', \
f'## {p}\n\n' + new_message, \
content)
with open('docs/usage.md', 'w') as f:
f.write(content)