forked from conan-io/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands_help_update.py
134 lines (106 loc) · 3.46 KB
/
commands_help_update.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
import os
import platform
import subprocess
import sys
folder = {
"config": "consumer",
"get": "consumer",
"info": "consumer",
"install": "consumer",
"search": "consumer",
"create": "creator",
"export-pkg": "creator",
"export": "creator",
"new": "creator",
"test": "creator",
"upload": "creator",
"build": "development",
"package": "development",
"source": "development",
"editable": "development",
"workspace": "development",
"alias": "misc",
"copy": "misc",
"download": "misc",
"help": "misc",
"imports": "misc",
"inspect": "misc",
"profile": "misc",
"remote": "misc",
"remove": "misc",
"user": "misc",
"graph": "misc",
}
experimental = ["inspect"]
commands = folder.keys()
conan_name = ""
try:
conan_name = sys.argv[1]
except IndexError:
conan_name = "conan"
template = """.. _conan_{0}:
conan {0}
======{1}
{2}
.. code-block:: bash
$ {3}
{4}
.. code-block:: text
{5}"""
for command in commands:
execute = [conan_name, command, "-h"]
print(execute)
output = str(subprocess.check_output(execute))
output = output.rstrip()
search_string = "conan %s [-h]" % command
output = search_string + output.split(search_string)[1]
output = output.split("\\r\\n\\r\\n" if platform.system() == "Windows" else "\\n\\n", 2)
underline = ""
for char in command:
underline += "="
small_help = ""
for line in output[0].replace("\\r", "").replace("\\n", "\n").splitlines():
if not line.startswith("conan"):
line = line[1:]
small_help += "%s\n" % line.rstrip()
text_help = output[1].replace("\\r", "").replace("\\n", "\n").rstrip()
if output[2].startswith("positional arguments"):
args_text = output[2]
else:
tmp = output[2].split("positional arguments")
text_help += "\n\n" + tmp[0].replace("\\r", "").replace("\\n", "\n").rstrip()
args_text = "positional arguments" + tmp[1]
arguments_help = ""
for line in args_text.replace("\\r", "").replace("\\n", "\n").splitlines():
if line == "'" or line == "\"":
continue
arguments_help += (" %s\n" % line) if line else "\n"
arguments_help = arguments_help.rstrip()
print(arguments_help)
text_experimental = """
.. warning::
This is an **experimental** feature subject to breaking changes in future releases.
""" if command in experimental else ""
text = template.format(command, underline, text_experimental, small_help, text_help,
arguments_help)
text = text.replace("\\'", "\'")
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "reference", "commands",
folder[command], command)
print("filepath:", filepath)
the_file = open("%s.rst" % filepath, "r")
content = the_file.read()
the_file.close()
the_file = open("%s.rst" % filepath, "w")
separator = "\n\n\n"
begin = content.find(".. _conan_%s" % command) # To avoid deleting ..spelling:: and other stuff
prev_content = content[0:begin]
rest_content = content[begin + 1:]
if rest_content:
rest_content = rest_content.split(separator, 1)
if len(rest_content) > 1:
the_file.write(prev_content + text + separator + rest_content[1])
else:
raise Exception("Separator (two consecutive newlines) not found")
else:
the_file.write(text)
the_file.close()