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 | |
---|
19 | platform = '.%s-%s'%(get_platform(),sys.version[:3]) |
---|
20 | |
---|
21 | CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
---|
22 | |
---|
23 | run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py')) |
---|
24 | run.prepare() |
---|
25 | |
---|
26 | SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src") |
---|
27 | SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform)) |
---|
28 | SASVIEW_DOCS = os.path.join(SASVIEW_BUILD, "doc") |
---|
29 | SASVIEW_TEST = os.path.join(SASVIEW_SRC, "..", "sasview", "test", "media") |
---|
30 | |
---|
31 | # Need to slurp in the new sasmodels model definitions to replace the old model_functions.rst |
---|
32 | # We are currently here: |
---|
33 | #/sasview-local-trunk/docs/sphinx-docs/build_sphinx.py |
---|
34 | SASMODELS_SOURCE_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "sasmodels", "models") |
---|
35 | SASMODELS_SOURCE_IMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "sasmodels", "models", "img") |
---|
36 | SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models") |
---|
37 | SASMODELS_DEST_IMG = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models", "img") |
---|
38 | |
---|
39 | #print SASMODELS_SOURCE_MODELS |
---|
40 | #print SASMODELS_SOURCE_IMG |
---|
41 | #print SASMODELS_DEST_MODELS |
---|
42 | #print SASMODELS_DEST_IMG |
---|
43 | |
---|
44 | SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build") |
---|
45 | SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source") |
---|
46 | SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api") |
---|
47 | SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe") |
---|
48 | SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models") |
---|
49 | SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives") |
---|
50 | SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test") |
---|
51 | |
---|
52 | BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", |
---|
53 | "bumps", "doc", "guide") |
---|
54 | BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting") |
---|
55 | |
---|
56 | def _remove_dir(dir_path): |
---|
57 | """Removes the given directory.""" |
---|
58 | if os.path.isdir(dir_path): |
---|
59 | print "Removing \"%s\"... " % dir_path |
---|
60 | shutil.rmtree(dir_path) |
---|
61 | |
---|
62 | def clean(): |
---|
63 | """ |
---|
64 | Clean the sphinx build directory. |
---|
65 | """ |
---|
66 | print "=== Cleaning Sphinx Build ===" |
---|
67 | _remove_dir(SASVIEW_DOCS) |
---|
68 | _remove_dir(SPHINX_BUILD) |
---|
69 | _remove_dir(SPHINX_SOURCE_GUIFRAME) |
---|
70 | _remove_dir(SPHINX_SOURCE_MODELS) |
---|
71 | _remove_dir(SPHINX_SOURCE_PERSPECTIVES) |
---|
72 | _remove_dir(SPHINX_SOURCE_TEST) |
---|
73 | |
---|
74 | def retrieve_user_docs(): |
---|
75 | """ |
---|
76 | Copies across the contents of any media/ directories in src/, and puts them |
---|
77 | in an appropriately named directory of docs/sphinx-docs/source/. For |
---|
78 | example: |
---|
79 | |
---|
80 | sas/../[MODULE]/media/dir/A.rst |
---|
81 | sas/../[MODULE]/media/B.rst |
---|
82 | |
---|
83 | gets copied to a new location: |
---|
84 | |
---|
85 | docs/sphinx-docs/source/user/[MODULE]/dir/A.rst |
---|
86 | docs/sphinx-docs/source/user/[MODULE]/B.rst |
---|
87 | |
---|
88 | so that Sphinx may pick it up when generating the documentation. |
---|
89 | """ |
---|
90 | print "=== Retrieve User Docs ===" |
---|
91 | |
---|
92 | # Copy documentation files from their "source" to their "destination". |
---|
93 | for root, dirnames, _ in os.walk(SASVIEW_SRC): |
---|
94 | for dirname in fnmatch.filter(dirnames, 'media'): |
---|
95 | |
---|
96 | docs = os.path.abspath(os.path.join(root, dirname)) |
---|
97 | print "Found docs folder at \"%s\"." % docs |
---|
98 | |
---|
99 | dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC)) |
---|
100 | if os.sep in dest_dir_part: |
---|
101 | dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:] |
---|
102 | dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part) |
---|
103 | |
---|
104 | copy_tree(docs, dest_dir) |
---|
105 | |
---|
106 | # Now pickup testdata_help.rst |
---|
107 | # print os.path.abspath(SASVIEW_TEST) |
---|
108 | # print os.path.abspath(SPHINX_SOURCE_TEST) |
---|
109 | if os.path.exists(SASVIEW_TEST): |
---|
110 | print "Found docs folder at ", SASVIEW_TEST |
---|
111 | shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST) |
---|
112 | |
---|
113 | def retrieve_bumps_docs(): |
---|
114 | """ |
---|
115 | Copies select files from the bumps documentation into fitting perspective |
---|
116 | """ |
---|
117 | if os.path.exists(BUMPS_DOCS): |
---|
118 | print "=== Retrieve BUMPS Docs ===" |
---|
119 | filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")] |
---|
120 | filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png")) |
---|
121 | filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png")) |
---|
122 | for f in filenames: |
---|
123 | print "Copying file", f |
---|
124 | shutil.copy(f, BUMPS_TARGET) |
---|
125 | else: |
---|
126 | print """ |
---|
127 | *** Error *** missing directory %s |
---|
128 | The documentation will not include the optimizer selection section. |
---|
129 | Checkout the bumps source tree and rebuild the docs. |
---|
130 | |
---|
131 | |
---|
132 | """ % BUMPS_DOCS |
---|
133 | |
---|
134 | def apidoc(): |
---|
135 | """ |
---|
136 | Runs sphinx-apidoc to generate .rst files from the docstrings in .py files |
---|
137 | in the SasView build directory. |
---|
138 | """ |
---|
139 | print "=== Generate API Rest Files ===" |
---|
140 | |
---|
141 | # Clean directory before generating a new version. |
---|
142 | _remove_dir(SPHINX_SOURCE_API) |
---|
143 | |
---|
144 | subprocess.call(["sphinx-apidoc", |
---|
145 | "-o", SPHINX_SOURCE_API, # Output dir. |
---|
146 | "-d", "8", # Max depth of TOC. |
---|
147 | SASVIEW_BUILD]) |
---|
148 | |
---|
149 | def build(): |
---|
150 | """ |
---|
151 | Runs sphinx-build. Reads in all .rst files and spits out the final html. |
---|
152 | """ |
---|
153 | print "=== Build HTML Docs from Rest Files ===" |
---|
154 | subprocess.call(["sphinx-build", |
---|
155 | "-b", "html", # Builder name. TODO: accept as arg to setup.py. |
---|
156 | "-d", os.path.join(SPHINX_BUILD, "doctrees"), |
---|
157 | SPHINX_SOURCE, |
---|
158 | os.path.join(SPHINX_BUILD, "html")]) |
---|
159 | |
---|
160 | print "=== Copy HTML Docs to Build Directory ===" |
---|
161 | html = os.path.join(SPHINX_BUILD, "html") |
---|
162 | copy_tree(html, SASVIEW_DOCS) |
---|
163 | |
---|
164 | def rebuild(): |
---|
165 | clean() |
---|
166 | retrieve_user_docs() |
---|
167 | retrieve_bumps_docs() |
---|
168 | apidoc() |
---|
169 | build() |
---|
170 | |
---|
171 | print "=== Done ===" |
---|
172 | |
---|
173 | if __name__ == "__main__": |
---|
174 | rebuild() |
---|