This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_doc.py
executable file
·115 lines (81 loc) · 3.12 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
#!/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 = 'everywhere'
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()
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()
build_docstring_rst()
build_html()
duplicate_old_html()
update_html()
commit_to_github()
if __name__ == '__main__':
main()