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