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