1 | """ |
---|
2 | Functions for building sphinx docs. |
---|
3 | |
---|
4 | For more information on the invocation of sphinx see: |
---|
5 | http://sphinx-doc.org/invocation.html |
---|
6 | """ |
---|
7 | import subprocess |
---|
8 | import os |
---|
9 | import sys |
---|
10 | import fnmatch |
---|
11 | import shutil |
---|
12 | import imp |
---|
13 | |
---|
14 | from distutils.dir_util import copy_tree |
---|
15 | from distutils.util import get_platform |
---|
16 | |
---|
17 | platform = '.%s-%s'%(get_platform(),sys.version[:3]) |
---|
18 | |
---|
19 | CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
---|
20 | |
---|
21 | run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py')) |
---|
22 | run.prepare() |
---|
23 | |
---|
24 | SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src") |
---|
25 | SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform)) |
---|
26 | SASVIEW_DOCS = os.path.join(SASVIEW_BUILD, "doc") |
---|
27 | |
---|
28 | SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build") |
---|
29 | SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source") |
---|
30 | SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api") |
---|
31 | SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "guiframe") |
---|
32 | SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models") |
---|
33 | SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "perspectives") |
---|
34 | |
---|
35 | def _remove_dir(dir_path): |
---|
36 | """Removes the given directory.""" |
---|
37 | if os.path.isdir(dir_path): |
---|
38 | print "Removing \"%s\"... " % dir_path |
---|
39 | shutil.rmtree(dir_path) |
---|
40 | |
---|
41 | def clean(): |
---|
42 | """ |
---|
43 | Clean the sphinx build directory. |
---|
44 | """ |
---|
45 | print "=== Cleaning Sphinx Build ===" |
---|
46 | _remove_dir(SASVIEW_DOCS) |
---|
47 | _remove_dir(SPHINX_BUILD) |
---|
48 | _remove_dir(SPHINX_SOURCE_GUIFRAME) |
---|
49 | _remove_dir(SPHINX_SOURCE_MODELS) |
---|
50 | _remove_dir(SPHINX_SOURCE_PERSPECTIVES) |
---|
51 | |
---|
52 | def retrieve_user_docs(): |
---|
53 | """ |
---|
54 | Copies across the contents of any media/ directories in src/, and puts them |
---|
55 | in an appropriately named directory of docs/sphinx-docs/source/. For |
---|
56 | example: |
---|
57 | |
---|
58 | sas/../[MODULE]/media/dir/A.rst |
---|
59 | sas/../[MODULE]/media/B.rst |
---|
60 | |
---|
61 | gets copied to a new location: |
---|
62 | |
---|
63 | docs/sphinx-docs/source/user/[MODULE]/dir/A.rst |
---|
64 | docs/sphinx-docs/source/user/[MODULE]/B.rst |
---|
65 | |
---|
66 | so that Sphinx may pick it up when generating the documentation. |
---|
67 | """ |
---|
68 | print "=== Retrieve User Docs ===" |
---|
69 | |
---|
70 | # Copy documentation files from their "source" to their "destination". |
---|
71 | for root, dirnames, _ in os.walk(SASVIEW_SRC): |
---|
72 | for dirname in fnmatch.filter(dirnames, 'media'): |
---|
73 | |
---|
74 | docs = os.path.abspath(os.path.join(root, dirname)) |
---|
75 | print "Found docs folder at \"%s\"." % docs |
---|
76 | |
---|
77 | dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC)) |
---|
78 | if os.sep in dest_dir_part: |
---|
79 | dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:] |
---|
80 | dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part) |
---|
81 | |
---|
82 | copy_tree(docs, dest_dir) |
---|
83 | |
---|
84 | def apidoc(): |
---|
85 | """ |
---|
86 | Runs sphinx-apidoc to generate .rst files from the docstrings in .py files |
---|
87 | in the SasView build directory. |
---|
88 | """ |
---|
89 | print "=== Generate API Rest Files ===" |
---|
90 | |
---|
91 | # Clean directory before generating a new version. |
---|
92 | _remove_dir(SPHINX_SOURCE_API) |
---|
93 | |
---|
94 | subprocess.call(["sphinx-apidoc", |
---|
95 | "-o", SPHINX_SOURCE_API, # Output dir. |
---|
96 | "-d", "8", # Max depth of TOC. |
---|
97 | SASVIEW_BUILD]) |
---|
98 | |
---|
99 | def build(): |
---|
100 | """ |
---|
101 | Runs sphinx-build. Reads in all .rst files and spits out the final html. |
---|
102 | """ |
---|
103 | print "=== Build HTML Docs from Rest Files ===" |
---|
104 | subprocess.call(["sphinx-build", |
---|
105 | "-b", "html", # Builder name. TODO: accept as arg to setup.py. |
---|
106 | "-d", os.path.join(SPHINX_BUILD, "doctrees"), |
---|
107 | SPHINX_SOURCE, |
---|
108 | os.path.join(SPHINX_BUILD, "html")]) |
---|
109 | |
---|
110 | print "=== Copy HTML Docs to Build Directory ===" |
---|
111 | html = os.path.join(SPHINX_BUILD, "html") |
---|
112 | copy_tree(html, SASVIEW_DOCS) |
---|
113 | |
---|
114 | if __name__ == "__main__": |
---|
115 | clean() |
---|
116 | retrieve_user_docs() |
---|
117 | apidoc() |
---|
118 | build() |
---|
119 | |
---|
120 | print "=== Done ===" |
---|