-
Notifications
You must be signed in to change notification settings - Fork 5
/
build_doc.py
executable file
·191 lines (162 loc) · 6.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
#
# Copyright (c) 2013-2014, Scott J Maddox
#
# This file is part of openbandparams.
#
# openbandparams is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# openbandparams is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with openbandparams. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
import os
import sys
import subprocess
import shutil
def clear_pycache(path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
root, ext = os.path.splitext(filename)
if ext == '.pyc':
os.remove(os.path.join(dirpath, filename))
def clear_rst_cache(path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
root, ext = os.path.splitext(filename)
if filename.startswith('_') and ext == '.rst':
os.remove(os.path.join(dirpath, filename))
CLEAN = len(sys.argv) > 1 and sys.argv[1].lower() == 'clean'
CWD = os.getcwd()
# sphinx-apidoc -f -o doc -d 4 src/openbandparams/
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, 'src'))
EXAMPLES_DIR = os.path.join(SCRIPT_DIR, 'src/openbandparams/examples')
OBP_FILE = os.path.join(SCRIPT_DIR, 'src/openbandparams/__init__.py')
DOC_DIR = os.path.join(SCRIPT_DIR, 'doc')
DOC_EXAMPLES_DIR = os.path.join(SCRIPT_DIR, 'doc/examples')
BUILD_DIR = os.path.join(SCRIPT_DIR, 'doc/_build_examples')
BUILD_EXAMPLES_DIR = os.path.join(SCRIPT_DIR, 'doc/_build_examples')
clear_rst_cache(DOC_EXAMPLES_DIR)
if CLEAN:
clear_pycache(SRC_DIR)
if os.path.exists(BUILD_DIR):
shutil.rmtree(BUILD_DIR)
if os.path.exists(DOC_EXAMPLES_DIR):
shutil.rmtree(DOC_EXAMPLES_DIR)
if os.path.exists(BUILD_EXAMPLES_DIR):
shutil.rmtree(BUILD_EXAMPLES_DIR)
if not os.path.exists(DOC_EXAMPLES_DIR):
os.mkdir(DOC_EXAMPLES_DIR)
if not os.path.exists(BUILD_EXAMPLES_DIR):
os.mkdir(BUILD_EXAMPLES_DIR)
# make sure that python imports the local openbandparams version
os.chdir(SRC_DIR)
sys.path.insert(0, SRC_DIR)
import openbandparams
if openbandparams.__file__ != OBP_FILE and openbandparams.__file__ != OBP_FILE+'c':
raise RuntimeError('Wrong openbandparams location:\n'
'{}\n'
'Expected:\n'
'{}'.format(openbandparams.__file__,OBP_FILE))
print ''
print 'Building examples...'
examples = []
for root, dirs, files in os.walk(EXAMPLES_DIR):
for f in files:
if f.endswith('.py') and not f.startswith('_'):
examples.append(os.path.relpath(os.path.join(root, f),
EXAMPLES_DIR))
# save a list of the examples
with open(os.path.join(BUILD_EXAMPLES_DIR, 'examples.txt'), 'w') as f:
for example in examples:
f.write(example + '\n')
for example in examples:
# build the result filename
dir, filename = os.path.split(example)
root, ext = os.path.splitext(filename)
if filename.lower().startswith('plot'):
result_type = 'image'
result = os.path.join(dir, root + '.png')
else:
result_type = 'literalinclude'
result = os.path.join(dir, root + '.txt')
# output an rst file for each example
rst_path = os.path.join(DOC_EXAMPLES_DIR, dir, '_' + root + '.rst')
# ../../src/openbandparams/examples/
rst_abs_dir = os.path.dirname(rst_path)
example_rel = os.path.relpath(os.path.join(EXAMPLES_DIR, example),
rst_abs_dir)
result_rel = os.path.relpath(os.path.join(BUILD_EXAMPLES_DIR, result),
rst_abs_dir)
if not os.path.exists(rst_abs_dir):
os.makedirs(rst_abs_dir)
with open(rst_path, 'w') as f:
title = root.replace('_', ' ')
underline = '=' * len(root)
f.write('''{title}
{underline}
Source:
.. literalinclude:: {example_rel}
Result:
.. {result_type}:: {result_rel}
'''.format(title=title,
underline=underline,
result_type=result_type,
example_rel=example_rel,
result_rel=result_rel))
# get the absolute paths
example_path = os.path.join(EXAMPLES_DIR, example)
result_path = os.path.join(BUILD_EXAMPLES_DIR, result)
# check if changes have been made to the example script
if (not CLEAN and
os.path.exists(result_path) and
os.path.getmtime(example_path) < os.path.getmtime(result_path)):
# no changes -- skip running it
continue
# get the relative paths (for printing)
example_relpath = os.path.relpath(example_path, CWD)
result_relpath = os.path.relpath(result_path, CWD)
# run the script and save the result
print ' Running "{}"\n Saving result to "{}"'.format(
example_relpath, result_relpath)
if not os.path.exists(os.path.dirname(result_path)):
os.makedirs(os.path.dirname(result_path))
if result_type == 'image':
try:
subprocess.check_call(['python', example_path, result_path],
env=os.environ)
except Exception as e:
if os.path.exists(result_path):
os.remove(result_path)
raise e
elif result_type == 'literalinclude':
try:
with open(result_path, 'w') as f:
subprocess.check_call(['python', example_path], stdout=f,
env=os.environ)
except Exception as e:
if os.path.exists(result_path):
os.remove(result_path)
raise e
else:
raise RuntimeError('Unknown result_type: {}'.format(result_type))
print 'Done building examples.'
print ''
os.chdir('../doc')
# Run sphinx-apidoc
subprocess.check_call(['sphinx-apidoc', '-o','apidoc', '../src/openbandparams',
# exclude paths:
'../src/openbandparams/tests',
'../src/openbandparams/examples',
])
# Build html
subprocess.check_call(['make', 'html'])