forked from ossec/ossec-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis_sphinx.py
103 lines (90 loc) · 3.16 KB
/
travis_sphinx.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
import os
import sys
import subprocess
import getopt
import sphinx
def run(*args):
ret = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr)
if ret != 0:
exit(ret)
def build_docs(source_dir, target_dir, flags):
"""
Build documentation from ``source_dir``, placing built files in
``target_dir``.
:param str source_dir: location of sphinx documentation files
:param str target_dir: location to build to
"""
print 'building documentation'
args = ['-b html']
if len(flags):
args = args + flags
if sphinx.build_main(args + [source_dir, target_dir]):
return False
open('%s/.nojekyll' % target_dir, 'a').close()
return True
def deploy_docs(target_dir):
"""
Deploy built docs to gh-pages, uses ``GH_TOKEN`` for pushing built
documentation files located in *target/doc* to gh
:param str target_dir: directory that build files were written to
"""
branch = os.environ['TRAVIS_BRANCH']
pr = os.environ['TRAVIS_PULL_REQUEST']
token = os.environ['GH_TOKEN']
repo = os.environ['TRAVIS_REPO_SLUG']
if branch == 'master' and pr == 'false':
print 'uploading docs...'
sys.stdout.flush()
run('git', 'clone', 'https://github.com/davisp/ghp-import')
run('./ghp-import/ghp-import', '-n', 'target/doc/build')
run('git', 'push', '-fq', 'https://%[email protected]/%s.git'
% (token, repo), 'master')
else:
print 'build triggered for non-master branch \'' + branch + \
'\', skipping deploy...'
def usage():
"""
Print usage message when a user does not enter any cline args, or
if they specify --help
"""
print 'Usage: travis-sphinx [options] {actions}\n'
print 'Options:\n -h, --help\t\tSee usage of script\n' + \
' -s, --source\t\tSource directory of sphinx docs, default is docs/source' + \
' -n, --nowarn\t\tDo not error on warnings'
print 'Actions:\n build \t\tBuild sphinx documentation, places docs in target/doc' + \
'\n deploy\t\tDeploy sphinx docs to travis branch by pulling from target/doc'
def main():
source_dir = 'docs/source'
target_dir = 'target/doc/build'
flags = ['-W']
# Print usage if no arguments entered
if len(sys.argv) == 1:
print 'travis-sphinx v0.0.1'
usage()
exit(0)
try:
opts, args = getopt.getopt(sys.argv[1:], 'nhs:', ['nowarn', 'help', 'source='])
except getopt.GetoptError as err:
print str(err) + ', see --help for valid arguments'
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
elif opt in ('-s', '--source'):
if sys.argv[-1] == 'deploy':
print 'source option not allowed for deploy'
sys.exit(2)
elif opt in ('-n', '--nowarn'):
flags.remove('-W')
source_dir = arg
if sys.argv[-1] == 'build':
if not build_docs(source_dir, target_dir, flags):
exit(2)
elif sys.argv[-1] == 'deploy':
deploy_docs(target_dir)
else:
usage()
exit(2)
if __name__ == '__main__':
main()