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 | from __future__ import print_function |
---|
9 | |
---|
10 | import subprocess |
---|
11 | import os |
---|
12 | from os.path import join as joinpath, abspath, dirname, isdir, exists, relpath |
---|
13 | import sys |
---|
14 | import fnmatch |
---|
15 | import shutil |
---|
16 | import imp |
---|
17 | |
---|
18 | from glob import glob |
---|
19 | from distutils.dir_util import copy_tree |
---|
20 | from distutils.util import get_platform |
---|
21 | from distutils.spawn import find_executable |
---|
22 | |
---|
23 | from shutil import copy |
---|
24 | from os import listdir |
---|
25 | |
---|
26 | platform = '.%s-%s'%(get_platform(),sys.version[:3]) |
---|
27 | |
---|
28 | # sphinx paths |
---|
29 | SPHINX_ROOT = dirname(abspath(__file__)) |
---|
30 | SPHINX_BUILD = joinpath(SPHINX_ROOT, "build") |
---|
31 | SPHINX_SOURCE = joinpath(SPHINX_ROOT, "source-temp") |
---|
32 | SPHINX_PERSPECTIVES = joinpath(SPHINX_SOURCE, "user", "qtgui", "perspectives") |
---|
33 | |
---|
34 | # sasview paths |
---|
35 | SASVIEW_ROOT = joinpath(SPHINX_ROOT, '..', '..') |
---|
36 | SASVIEW_DOCS = joinpath(SPHINX_ROOT, "source") |
---|
37 | SASVIEW_BUILD = abspath(joinpath(SASVIEW_ROOT, "build", "lib"+platform)) |
---|
38 | SASVIEW_MEDIA_SOURCE = joinpath(SASVIEW_ROOT, "src", "sas") |
---|
39 | SASVIEW_DOC_TARGET = joinpath(SASVIEW_BUILD, "doc") |
---|
40 | SASVIEW_API_TARGET = joinpath(SPHINX_SOURCE, "dev", "sasview-api") |
---|
41 | |
---|
42 | # sasmodels paths |
---|
43 | SASMODELS_ROOT = joinpath(SASVIEW_ROOT, "..", "sasmodels") |
---|
44 | SASMODELS_DOCS = joinpath(SASMODELS_ROOT, "doc") |
---|
45 | SASMODELS_BUILD = joinpath(SASMODELS_ROOT, "build", "lib") |
---|
46 | SASMODELS_MODEL_SOURCE = joinpath(SASMODELS_DOCS, "model") |
---|
47 | SASMODELS_MODEL_TARGET = joinpath(SPHINX_SOURCE, "user", "models") |
---|
48 | #SASMODELS_API_SOURCE = joinpath(SASMODELS_DOCS, "api") |
---|
49 | SASMODELS_API_TARGET = joinpath(SPHINX_SOURCE, "dev", "sasmodels-api") |
---|
50 | SASMODELS_DEV_SOURCE = joinpath(SASMODELS_DOCS, "developer") |
---|
51 | SASMODELS_DEV_TARGET = joinpath(SPHINX_SOURCE, "dev", "sasmodels-dev") |
---|
52 | SASMODELS_GUIDE_SOURCE = joinpath(SASMODELS_DOCS, "guide") |
---|
53 | SASMODELS_GUIDE_TARGET = joinpath(SPHINX_PERSPECTIVES, "fitting") |
---|
54 | SASMODELS_GUIDE_EXCLUDE = [ |
---|
55 | "index.rst", "install.rst", "intro.rst", |
---|
56 | ] |
---|
57 | |
---|
58 | # bumps paths |
---|
59 | BUMPS_DOCS = joinpath(SASVIEW_ROOT, "..", "bumps", "doc") |
---|
60 | BUMPS_SOURCE = joinpath(BUMPS_DOCS, "guide") |
---|
61 | BUMPS_TARGET = joinpath(SPHINX_PERSPECTIVES, "fitting") |
---|
62 | |
---|
63 | run = imp.load_source('run', joinpath(SASVIEW_ROOT, 'run.py')) |
---|
64 | run.prepare() |
---|
65 | |
---|
66 | def inplace_change(filename, old_string, new_string): |
---|
67 | # Thanks to http://stackoverflow.com/questions/4128144/replace-string-within-file-contents |
---|
68 | s=open(filename).read() |
---|
69 | if old_string in s: |
---|
70 | print('Changing "{old_string}" to "{new_string}"'.format(**locals())) |
---|
71 | s=s.replace(old_string, new_string) |
---|
72 | f=open(filename, 'w') |
---|
73 | f.write(s) |
---|
74 | f.flush() |
---|
75 | f.close() |
---|
76 | else: |
---|
77 | print('No occurences of "{old_string}" found.'.format(**locals())) |
---|
78 | |
---|
79 | def _remove_dir(dir_path): |
---|
80 | """Removes the given directory.""" |
---|
81 | if isdir(dir_path): |
---|
82 | print("Removing \"%s\"... " % dir_path) |
---|
83 | shutil.rmtree(dir_path) |
---|
84 | |
---|
85 | def clean(): |
---|
86 | """ |
---|
87 | Clean the sphinx build directory. |
---|
88 | """ |
---|
89 | print("=== Cleaning Sphinx Build ===") |
---|
90 | _remove_dir(SASVIEW_DOC_TARGET) |
---|
91 | _remove_dir(SPHINX_BUILD) |
---|
92 | _remove_dir(SPHINX_SOURCE) |
---|
93 | |
---|
94 | def setup_source_temp(): |
---|
95 | """ |
---|
96 | Copy the source toctrees to new folder for assembling the sphinx-docs |
---|
97 | """ |
---|
98 | print("=== Copying Source toctrees ===") |
---|
99 | shutil.copytree(SASVIEW_DOCS, SPHINX_SOURCE) |
---|
100 | |
---|
101 | def retrieve_user_docs(): |
---|
102 | """ |
---|
103 | Copies across the contents of any media/ directories in src/, and puts them |
---|
104 | in an appropriately named directory of docs/sphinx-docs/source/. For |
---|
105 | example: |
---|
106 | |
---|
107 | sas/../[MODULE]/media/dir/A.rst |
---|
108 | sas/../[MODULE]/media/B.rst |
---|
109 | |
---|
110 | gets copied to a new location: |
---|
111 | |
---|
112 | docs/sphinx-docs/source/user/[MODULE]/dir/A.rst |
---|
113 | docs/sphinx-docs/source/user/[MODULE]/B.rst |
---|
114 | |
---|
115 | so that Sphinx may pick it up when generating the documentation. |
---|
116 | """ |
---|
117 | print("=== Retrieve User Docs ===") |
---|
118 | |
---|
119 | # Copy documentation files from sas/.../media to the sphinx directory |
---|
120 | for root, dirs, _ in os.walk(SASVIEW_MEDIA_SOURCE): |
---|
121 | if 'media' in dirs: |
---|
122 | source_dir = abspath(joinpath(root, "media")) |
---|
123 | relative = dirname(relpath(source_dir, SASVIEW_MEDIA_SOURCE)) |
---|
124 | dest_dir = joinpath(SPHINX_SOURCE, "user", relative) |
---|
125 | |
---|
126 | print("Found sasview docs folder at \"%s\"." % relative) |
---|
127 | copy_tree(source_dir, dest_dir) |
---|
128 | |
---|
129 | print("=== Sasmodels Docs ===") |
---|
130 | shutil.copy(joinpath(SASMODELS_DOCS, "rst_prolog"), SPHINX_SOURCE) |
---|
131 | copy_tree(SASMODELS_MODEL_SOURCE, SASMODELS_MODEL_TARGET) |
---|
132 | #copy_tree(SASMODELS_API_SOURCE, SASMODELS_API_TARGET) |
---|
133 | copy_tree(SASMODELS_DEV_SOURCE, SASMODELS_DEV_TARGET) |
---|
134 | copy_tree(SASMODELS_GUIDE_SOURCE, SASMODELS_GUIDE_TARGET) |
---|
135 | for filename in SASMODELS_GUIDE_EXCLUDE: |
---|
136 | os.unlink(joinpath(SASMODELS_GUIDE_TARGET, filename)) |
---|
137 | |
---|
138 | # Model category files reference the model as ../../model/name.rst. Since |
---|
139 | # we are rearranging the tree, we need to update each of these links. |
---|
140 | catdir = joinpath(SASMODELS_GUIDE_TARGET, "models") |
---|
141 | for filename in os.listdir(catdir): |
---|
142 | inplace_change(joinpath(catdir, filename), "../../model/", "/user/models/") |
---|
143 | |
---|
144 | |
---|
145 | def retrieve_bumps_docs(): |
---|
146 | """ |
---|
147 | Copies select files from the bumps documentation into fitting perspective |
---|
148 | """ |
---|
149 | if exists(BUMPS_SOURCE): |
---|
150 | print("=== Retrieve BUMPS Docs ===") |
---|
151 | filenames = glob(joinpath(BUMPS_SOURCE, "dream-*.png")) |
---|
152 | #filenames = [joinpath(BUMPS_SOURCE, "optimizer.rst")] |
---|
153 | #filenames += glob(joinpath(BUMPS_SOURCE, "dream-*.png")) |
---|
154 | #filenames += glob(joinpath(BUMPS_SOURCE, "fit-*.png")) |
---|
155 | for f in filenames: |
---|
156 | print("Copying file", f) |
---|
157 | shutil.copy(f, BUMPS_TARGET) |
---|
158 | else: |
---|
159 | print(""" |
---|
160 | ======= Error ======= |
---|
161 | missing directory %s |
---|
162 | The documentation will not include the optimizer selection section. |
---|
163 | Checkout the bumps source tree and rebuild the docs. |
---|
164 | """ % BUMPS_DOCS) |
---|
165 | |
---|
166 | def apidoc(): |
---|
167 | """ |
---|
168 | Runs sphinx-apidoc to generate .rst files from the docstrings in .py files |
---|
169 | in the SasView build directory. |
---|
170 | """ |
---|
171 | print("=== Generate API Rest Files ===") |
---|
172 | |
---|
173 | # Clean directory before generating a new version. |
---|
174 | #_remove_dir(SASVIEW_API_TARGET) |
---|
175 | |
---|
176 | subprocess.call(["sphinx-apidoc", |
---|
177 | "-o", SASVIEW_API_TARGET, # Output dir. |
---|
178 | "-d", "8", # Max depth of TOC. |
---|
179 | "-H", "SasView", # Package header |
---|
180 | SASVIEW_BUILD]) |
---|
181 | |
---|
182 | subprocess.call(["sphinx-apidoc", |
---|
183 | "-o", SASMODELS_API_TARGET, # Output dir. |
---|
184 | "-d", "8", # Max depth of TOC. |
---|
185 | "-H", "sasmodels", # Package header |
---|
186 | SASMODELS_BUILD, |
---|
187 | joinpath(SASMODELS_BUILD, "sasmodels", "models"), # exclude |
---|
188 | ]) |
---|
189 | |
---|
190 | def build_pdf(): |
---|
191 | """ |
---|
192 | Runs sphinx-build for pdf. Reads in all .rst files and spits out the final html. |
---|
193 | """ |
---|
194 | print("=== Build PDF Docs from ReST Files ===") |
---|
195 | subprocess.call(["sphinx-build", |
---|
196 | "-b", "latex", # Builder name. TODO: accept as arg to setup.py. |
---|
197 | "-d", joinpath(SPHINX_BUILD, "doctrees"), |
---|
198 | SPHINX_SOURCE, |
---|
199 | joinpath(SPHINX_BUILD, "latex")]) |
---|
200 | |
---|
201 | LATEXDIR = joinpath(SPHINX_BUILD, "latex") |
---|
202 | #TODO: Does it need to be done so many time? |
---|
203 | def pdflatex(): |
---|
204 | subprocess.call(["pdflatex", "SasView.tex"], cwd=LATEXDIR) |
---|
205 | pdflatex() |
---|
206 | pdflatex() |
---|
207 | pdflatex() |
---|
208 | subprocess.call(["makeindex", "-s", "python.ist", "SasView.idx"], cwd=LATEXDIR) |
---|
209 | pdflatex() |
---|
210 | pdflatex() |
---|
211 | |
---|
212 | print("=== Copy PDF to HTML Directory ===") |
---|
213 | source = joinpath(LATEXDIR, "SasView.pdf") |
---|
214 | target = joinpath(SASVIEW_DOC_TARGET, "SasView.pdf") |
---|
215 | shutil.copyfile(source, target) |
---|
216 | |
---|
217 | def build(): |
---|
218 | """ |
---|
219 | Runs sphinx-build. Reads in all .rst files and spits out the final html. |
---|
220 | """ |
---|
221 | print("=== Build HTML Docs from ReST Files ===") |
---|
222 | subprocess.call(["sphinx-build", |
---|
223 | "-b", "html", # Builder name. TODO: accept as arg to setup.py. |
---|
224 | "-d", joinpath(SPHINX_BUILD, "doctrees"), |
---|
225 | SPHINX_SOURCE, |
---|
226 | joinpath(SPHINX_BUILD, "html")]) |
---|
227 | |
---|
228 | print("=== Copy HTML Docs to Build Directory ===") |
---|
229 | html = joinpath(SPHINX_BUILD, "html") |
---|
230 | copy_tree(html, SASVIEW_DOC_TARGET) |
---|
231 | |
---|
232 | def fetch_katex(version, destination="_static"): |
---|
233 | from zipfile import ZipFile |
---|
234 | import urllib2 |
---|
235 | url = "https://github.com/Khan/KaTeX/releases/download/%s/katex.zip" % version |
---|
236 | cache_path = "katex_%s.zip" % version |
---|
237 | if not exists(cache_path): |
---|
238 | try: |
---|
239 | fd_in = urllib2.urlopen(url) |
---|
240 | with open(cache_path, "wb") as fd_out: |
---|
241 | fd_out.write(fd_in.read()) |
---|
242 | finally: |
---|
243 | fd_in.close() |
---|
244 | with ZipFile(cache_path) as zip: |
---|
245 | zip.extractall(destination) |
---|
246 | |
---|
247 | def convert_katex(): |
---|
248 | print("=== Preprocess HTML, converting latex to html ===") |
---|
249 | subprocess.call(["node", "convertKaTex.js", SASVIEW_DOC_TARGET]) |
---|
250 | |
---|
251 | def convert_mathjax(): |
---|
252 | print("=== Preprocess HTML, converting latex to html ===") |
---|
253 | subprocess.call(["node", "convertMathJax.js", SASVIEW_DOC_TARGET]) |
---|
254 | |
---|
255 | def fetch_mathjax(): |
---|
256 | subprocess.call(["npm", "install", "mathjax-node-page"]) |
---|
257 | # TODO: copy fonts from node_modules/mathjax/fonts/HTML-CSS/Tex into static |
---|
258 | |
---|
259 | def rebuild(): |
---|
260 | clean() |
---|
261 | setup_source_temp() |
---|
262 | retrieve_user_docs() |
---|
263 | retrieve_bumps_docs() |
---|
264 | #fetch_katex(version=KATEX_VERSION, destination=KATEX_PARENT) |
---|
265 | #fetch_mathjax() |
---|
266 | apidoc() |
---|
267 | build() |
---|
268 | if find_executable('latex'): |
---|
269 | build_pdf() |
---|
270 | #convert_katex() |
---|
271 | #convert_mathjax() |
---|
272 | |
---|
273 | print("=== Done ===") |
---|
274 | |
---|
275 | if __name__ == "__main__": |
---|
276 | rebuild() |
---|