forked from akretion/docky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_doc.py
executable file
·141 lines (105 loc) · 3.95 KB
/
build_doc.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
#!/usr/bin/env python3
from subprocess import run, PIPE
import shlex
import os
import logging
logging.basicConfig(format='{levelname}:{message}',
style='{',
level=logging.DEBUG)
DOCUMENT_FOLDER = 'doc'
API_DOCUMENT_FOLDER = '{}/{}'.format(DOCUMENT_FOLDER, 'api')
SOURCE = 'voodoo'
MASTER_BRANCH = 'master'
HTML_DIR = 'html'
def run_cmd(cmd, quiet=False):
if not quiet:
logging.info('command: {}'.format(cmd))
# use shlex to keep quoted substrings
result = run(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
stdout = result.stdout.strip().decode()
stderr = result.stderr.strip().decode()
if stdout and not quiet:
logging.debug(stdout)
if stderr and not quiet:
logging.warning(stderr)
return result.stdout.strip()
REPLACE_DOC = [
("Subcommands:",
"============== ==========\nSubcommands:\n============== =========="),
("VALUE:str", "VALUE"),
("\nvoodoo build", "============== ==========\nvoodoo build")
]
def generate_cmd_doc():
data = run_cmd('voodoo --help-all')
run_cmd('rm -rf doc/auto')
run_cmd('mkdir doc/auto')
cmd = open("doc/auto/cmd.rst", "w")
cmd.write("Command Line\n=========================\n\n")
data = data.decode()
for replace in REPLACE_DOC:
data = data.replace(*replace)
for doc in data.split("\nvoodoo "):
header, doc = doc.split("\n", 1)
name = header.rsplit(' ', 1)[0]
filename = name.replace(' ', '.')
header = "\n".join([name, len(name) * "-"])
cmd.write(header + "\n")
cmd.write(doc + "\n\n")
cmd.close()
def build_docstring_rst():
run_cmd('sphinx-apidoc -o {} {}'.format(API_DOCUMENT_FOLDER, SOURCE))
def build_html():
run_cmd('python setup.py build_sphinx -s {}'.format(DOCUMENT_FOLDER))
def clean_old_build():
run_cmd('rm -rf build/sphinx/html {} {}'.format(API_DOCUMENT_FOLDER,
HTML_DIR))
def duplicate_old_html():
url = get_repo_url().decode()
run_cmd('git clone -b gh-pages {} {}'.format(url, HTML_DIR))
run_cmd('rm -rf {html}/.git {html}/{br}'.format(html=HTML_DIR,
br=MASTER_BRANCH))
def get_git_tags():
return run_cmd('git tag --contains').split()
def update_html():
run_cmd('mv build/sphinx/html {}/{}'.format(HTML_DIR, MASTER_BRANCH))
tags = get_git_tags()
if tags:
destination = tags[0].decode()
run_cmd('rm -rf {}/{}'.format(HTML_DIR, destination))
run_cmd('cp -r {html}/{br} {html}/{dst}'.format(html=HTML_DIR,
br=MASTER_BRANCH,
dst=destination))
def get_commit_message():
return run_cmd('git log -1 --pretty="%s"')
def get_repo_url():
result = run_cmd('git remote get-url origin')
# older version of Git doesn't have 'get-ur' ...
if not result:
result = run_cmd('git remote -v').split()[1]
return result
def commit_to_github():
run_cmd('pip install -U ghp-import')
if os.environ.get('TRAVIS', ''):
run_cmd('git config --global user.name "Travis"')
run_cmd('git config --global user.email [email protected]')
msg = get_commit_message().decode()
cmd = 'ghp-import -n -r origin -b gh-pages -m "{}" {}'.format(msg,
HTML_DIR)
run_cmd(cmd)
url = get_repo_url().decode().lstrip('https://')
gh_token = os.environ['GH_TOKEN']
# Please set your GH_TOKEN as Travis CI
cmd = 'git push -fq \
https://{}@{} gh-pages:gh-pages'.format(gh_token,
url)
run_cmd(cmd, quiet=True)
def main():
clean_old_build()
generate_cmd_doc()
#build_docstring_rst()
build_html()
duplicate_old_html()
update_html()
commit_to_github()
if __name__ == '__main__':
main()