1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Functions for building sphinx docs. |
---|
4 | |
---|
5 | For more information on the invocation of sphinx see: |
---|
6 | http://sphinx-doc.org/invocation.html |
---|
7 | """ |
---|
8 | import subprocess |
---|
9 | import os |
---|
10 | import sys |
---|
11 | import fnmatch |
---|
12 | import shutil |
---|
13 | import imp |
---|
14 | from glob import glob |
---|
15 | |
---|
16 | from distutils.dir_util import copy_tree |
---|
17 | from distutils.util import get_platform |
---|
18 | from shutil import copy |
---|
19 | from os import listdir |
---|
20 | |
---|
21 | platform = '.%s-%s'%(get_platform(),sys.version[:3]) |
---|
22 | |
---|
23 | CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
---|
24 | |
---|
25 | run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py')) |
---|
26 | run.prepare() |
---|
27 | |
---|
28 | SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src") |
---|
29 | SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform)) |
---|
30 | SASVIEW_DOCS = os.path.join(SASVIEW_BUILD, "doc") |
---|
31 | SASVIEW_TEST = os.path.join(SASVIEW_SRC, "..", "sasview", "test", "media") |
---|
32 | |
---|
33 | # Need to slurp in the new sasmodels model definitions to replace the old model_functions.rst |
---|
34 | # We are currently here: |
---|
35 | #/sasview-local-trunk/docs/sphinx-docs/build_sphinx.py |
---|
36 | SASMODELS_SOURCE_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc") |
---|
37 | SASMODELS_SOURCE_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "models") |
---|
38 | SASMODELS_SOURCE_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model") |
---|
39 | SASMODELS_SOURCE_IMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model", "img") |
---|
40 | SASMODELS_DEST_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "source") |
---|
41 | SASMODELS_DEST_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user") |
---|
42 | SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models") |
---|
43 | SASMODELS_DEST_IMG = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models", "img") |
---|
44 | |
---|
45 | #if os.path.exists(SASMODELS_SOURCE_PROLOG): |
---|
46 | # print "Found models prolog folder at ", SASMODELS_SOURCE_PROLOG |
---|
47 | #if os.path.exists(SASMODELS_SOURCE_REF_MODELS): |
---|
48 | # print "Found models ref folder at ", SASMODELS_SOURCE_REF_MODELS |
---|
49 | #if os.path.exists(SASMODELS_SOURCE_MODELS): |
---|
50 | # print "Found models folder at ", SASMODELS_SOURCE_MODELS |
---|
51 | #if os.path.exists(SASMODELS_SOURCE_IMG): |
---|
52 | # print "Found img folder at ", SASMODELS_SOURCE_IMG |
---|
53 | #if os.path.exists(SASMODELS_DEST_REF_MODELS): |
---|
54 | # print "Found models ref folder at ", SASMODELS_DEST_REF_MODELS |
---|
55 | #if os.path.exists(SASMODELS_DEST_MODELS): |
---|
56 | # print "Found models folder at ", SASMODELS_DEST_MODELS |
---|
57 | #if os.path.exists(SASMODELS_DEST_IMG): |
---|
58 | # print "Found img folder at ", SASMODELS_DEST_IMG |
---|
59 | #sys.exit() |
---|
60 | |
---|
61 | SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build") |
---|
62 | SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source") |
---|
63 | SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api") |
---|
64 | SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe") |
---|
65 | SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models") |
---|
66 | SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives") |
---|
67 | SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test") |
---|
68 | |
---|
69 | BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", |
---|
70 | "bumps", "doc", "guide") |
---|
71 | BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting") |
---|
72 | |
---|
73 | def inplace_change(filename, old_string, new_string): |
---|
74 | # Thanks to http://stackoverflow.com/questions/4128144/replace-string-within-file-contents |
---|
75 | s=open(filename).read() |
---|
76 | if old_string in s: |
---|
77 | print 'Changing "{old_string}" to "{new_string}"'.format(**locals()) |
---|
78 | s=s.replace(old_string, new_string) |
---|
79 | f=open(filename, 'w') |
---|
80 | f.write(s) |
---|
81 | f.flush() |
---|
82 | f.close() |
---|
83 | else: |
---|
84 | print 'No occurences of "{old_string}" found.'.format(**locals()) |
---|
85 | |
---|
86 | def _remove_dir(dir_path): |
---|
87 | """Removes the given directory.""" |
---|
88 | if os.path.isdir(dir_path): |
---|
89 | print "Removing \"%s\"... " % dir_path |
---|
90 | shutil.rmtree(dir_path) |
---|
91 | |
---|
92 | def clean(): |
---|
93 | """ |
---|
94 | Clean the sphinx build directory. |
---|
95 | """ |
---|
96 | print "=== Cleaning Sphinx Build ===" |
---|
97 | _remove_dir(SASVIEW_DOCS) |
---|
98 | _remove_dir(SPHINX_BUILD) |
---|
99 | _remove_dir(SPHINX_SOURCE_GUIFRAME) |
---|
100 | _remove_dir(SPHINX_SOURCE_MODELS) |
---|
101 | _remove_dir(SPHINX_SOURCE_PERSPECTIVES) |
---|
102 | _remove_dir(SPHINX_SOURCE_TEST) |
---|
103 | |
---|
104 | |
---|
105 | def retrieve_user_docs(): |
---|
106 | """ |
---|
107 | Copies across the contents of any media/ directories in src/, and puts them |
---|
108 | in an appropriately named directory of docs/sphinx-docs/source/. For |
---|
109 | example: |
---|
110 | |
---|
111 | sas/../[MODULE]/media/dir/A.rst |
---|
112 | sas/../[MODULE]/media/B.rst |
---|
113 | |
---|
114 | gets copied to a new location: |
---|
115 | |
---|
116 | docs/sphinx-docs/source/user/[MODULE]/dir/A.rst |
---|
117 | docs/sphinx-docs/source/user/[MODULE]/B.rst |
---|
118 | |
---|
119 | so that Sphinx may pick it up when generating the documentation. |
---|
120 | """ |
---|
121 | print "=== Retrieve User Docs ===" |
---|
122 | |
---|
123 | # Copy documentation files from their "source" to their "destination". |
---|
124 | for root, dirnames, _ in os.walk(SASVIEW_SRC): |
---|
125 | for dirname in fnmatch.filter(dirnames, 'media'): |
---|
126 | |
---|
127 | docs = os.path.abspath(os.path.join(root, dirname)) |
---|
128 | print "Found docs folder at \"%s\"." % docs |
---|
129 | |
---|
130 | dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC)) |
---|
131 | if os.sep in dest_dir_part: |
---|
132 | dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:] |
---|
133 | dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part) |
---|
134 | |
---|
135 | copy_tree(docs, dest_dir) |
---|
136 | |
---|
137 | # Now pickup testdata_help.rst |
---|
138 | # print os.path.abspath(SASVIEW_TEST) |
---|
139 | # print os.path.abspath(SPHINX_SOURCE_TEST) |
---|
140 | print "=== Including Test Data Docs ===" |
---|
141 | if os.path.exists(SASVIEW_TEST): |
---|
142 | print "Found docs folder at ", SASVIEW_TEST |
---|
143 | shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST) |
---|
144 | |
---|
145 | print "=== And the Sasmodels Docs ===" |
---|
146 | # Make sure we have the relevant images for the new sasmodels documentation |
---|
147 | if os.path.exists(SASMODELS_SOURCE_IMG): |
---|
148 | print "Found img folder SASMODELS_SOURCE_IMG at ", SASMODELS_SOURCE_IMG |
---|
149 | if os.path.exists(SASMODELS_DEST_IMG): |
---|
150 | print "Found img folder SASMODELS_DEST_IMG at ", SASMODELS_DEST_IMG |
---|
151 | print "Copying sasmodels model image files..." |
---|
152 | for files in os.listdir(SASMODELS_SOURCE_IMG): |
---|
153 | fromhere=os.path.join(SASMODELS_SOURCE_IMG,files) |
---|
154 | tohere=os.path.join(SASMODELS_DEST_IMG,files) |
---|
155 | shutil.copy(fromhere,tohere) |
---|
156 | |
---|
157 | # And the rst prolog with the unit substitutions |
---|
158 | if os.path.exists(SASMODELS_SOURCE_PROLOG): |
---|
159 | print "Found prolog folder SASMODELS_SOURCE_PROLOG at ", SASMODELS_SOURCE_PROLOG |
---|
160 | if os.path.exists(SASMODELS_DEST_PROLOG): |
---|
161 | print "Found docs folder SASMODELS_DEST_PROLOG at ", SASMODELS_DEST_PROLOG |
---|
162 | print "Copying sasmodels rst_prolog file..." |
---|
163 | for files in os.listdir(SASMODELS_SOURCE_PROLOG): |
---|
164 | if files.startswith("rst"): |
---|
165 | fromhere=os.path.join(SASMODELS_SOURCE_PROLOG,files) |
---|
166 | tohere=os.path.join(SASMODELS_DEST_PROLOG,files) |
---|
167 | shutil.copy(fromhere,tohere) |
---|
168 | |
---|
169 | if os.path.exists(SASMODELS_SOURCE_REF_MODELS): |
---|
170 | print "Found docs folder SASMODELS_SOURCE_REF_MODELS at ", SASMODELS_SOURCE_REF_MODELS |
---|
171 | if os.path.exists(SASMODELS_DEST_REF_MODELS): |
---|
172 | print "Found docs folder SASMODELS_DEST_REF_MODELS at ", SASMODELS_DEST_REF_MODELS |
---|
173 | print "Copying sasmodels model toctree files..." |
---|
174 | for files in os.listdir(SASMODELS_SOURCE_REF_MODELS): |
---|
175 | if files.endswith(".rst"): |
---|
176 | fromhere=os.path.join(SASMODELS_SOURCE_REF_MODELS,files) |
---|
177 | tohere=os.path.join(SASMODELS_DEST_REF_MODELS,files) |
---|
178 | shutil.copy(fromhere,tohere) |
---|
179 | # But need to change the path to the model docs in the tocs |
---|
180 | for files in os.listdir(SASMODELS_DEST_REF_MODELS): |
---|
181 | # print files |
---|
182 | if files.startswith("shape"): |
---|
183 | print "Changing toc paths in", files |
---|
184 | inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/") |
---|
185 | if files.startswith("sphere"): |
---|
186 | print "Changing toc paths in", files |
---|
187 | inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/") |
---|
188 | if files.startswith("custom"): |
---|
189 | print "Changing toc paths in", files |
---|
190 | inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/") |
---|
191 | if files.startswith("structure"): |
---|
192 | print "Changing toc paths in", files |
---|
193 | inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/") |
---|
194 | |
---|
195 | if os.path.exists(SASMODELS_SOURCE_MODELS): |
---|
196 | print "Found docs folder SASMODELS_SOURCE_MODELS at ", SASMODELS_SOURCE_MODELS |
---|
197 | if os.path.exists(SASMODELS_DEST_MODELS): |
---|
198 | print "Found docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS |
---|
199 | print "Copying sasmodels model files..." |
---|
200 | for files in os.listdir(SASMODELS_SOURCE_MODELS): |
---|
201 | if files.endswith(".rst"): |
---|
202 | fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files) |
---|
203 | tohere=os.path.join(SASMODELS_DEST_MODELS,files) |
---|
204 | shutil.copy(fromhere,tohere) |
---|
205 | |
---|
206 | |
---|
207 | def retrieve_bumps_docs(): |
---|
208 | """ |
---|
209 | Copies select files from the bumps documentation into fitting perspective |
---|
210 | """ |
---|
211 | if os.path.exists(BUMPS_DOCS): |
---|
212 | print "=== Retrieve BUMPS Docs ===" |
---|
213 | filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")] |
---|
214 | filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png")) |
---|
215 | filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png")) |
---|
216 | for f in filenames: |
---|
217 | print "Copying file", f |
---|
218 | shutil.copy(f, BUMPS_TARGET) |
---|
219 | else: |
---|
220 | print """ |
---|
221 | *** Error *** missing directory %s |
---|
222 | The documentation will not include the optimizer selection section. |
---|
223 | Checkout the bumps source tree and rebuild the docs. |
---|
224 | |
---|
225 | |
---|
226 | """ % BUMPS_DOCS |
---|
227 | |
---|
228 | def apidoc(): |
---|
229 | """ |
---|
230 | Runs sphinx-apidoc to generate .rst files from the docstrings in .py files |
---|
231 | in the SasView build directory. |
---|
232 | """ |
---|
233 | print "=== Generate API Rest Files ===" |
---|
234 | |
---|
235 | # Clean directory before generating a new version. |
---|
236 | _remove_dir(SPHINX_SOURCE_API) |
---|
237 | |
---|
238 | subprocess.call(["sphinx-apidoc", |
---|
239 | "-o", SPHINX_SOURCE_API, # Output dir. |
---|
240 | "-d", "8", # Max depth of TOC. |
---|
241 | SASVIEW_BUILD]) |
---|
242 | |
---|
243 | def build(): |
---|
244 | """ |
---|
245 | Runs sphinx-build. Reads in all .rst files and spits out the final html. |
---|
246 | """ |
---|
247 | print "=== Build HTML Docs from Rest Files ===" |
---|
248 | subprocess.call(["sphinx-build", |
---|
249 | "-b", "html", # Builder name. TODO: accept as arg to setup.py. |
---|
250 | "-d", os.path.join(SPHINX_BUILD, "doctrees"), |
---|
251 | SPHINX_SOURCE, |
---|
252 | os.path.join(SPHINX_BUILD, "html")]) |
---|
253 | |
---|
254 | print "=== Copy HTML Docs to Build Directory ===" |
---|
255 | html = os.path.join(SPHINX_BUILD, "html") |
---|
256 | copy_tree(html, SASVIEW_DOCS) |
---|
257 | |
---|
258 | def rebuild(): |
---|
259 | clean() |
---|
260 | retrieve_user_docs() |
---|
261 | retrieve_bumps_docs() |
---|
262 | apidoc() |
---|
263 | build() |
---|
264 | |
---|
265 | print "=== Done ===" |
---|
266 | |
---|
267 | if __name__ == "__main__": |
---|
268 | rebuild() |
---|