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 | |
---|
32 | def _remove_dir(dir_path): |
---|
33 | """Removes the given directory.""" |
---|
34 | if os.path.isdir(dir_path): |
---|
35 | print "Removing \"%s\"... " % dir_path |
---|
36 | shutil.rmtree(dir_path) |
---|
37 | |
---|
38 | def clean(): |
---|
39 | """ |
---|
40 | Clean the sphinx build directory. |
---|
41 | """ |
---|
42 | print "=== Cleaning Sphinx Build ===" |
---|
43 | _remove_dir(SASVIEW_DOCS) |
---|
44 | _remove_dir(SPHINX_BUILD) |
---|
45 | |
---|
46 | def retrieve_user_docs(): |
---|
47 | """ |
---|
48 | Copies across the contents of any media/ directories in src/, and puts them |
---|
49 | in an appropriately named directory of docs/sphinx-docs/source/. For |
---|
50 | example: |
---|
51 | |
---|
52 | sas/../[MODULE]/media/dir/A.rst |
---|
53 | sas/../[MODULE]/media/B.rst |
---|
54 | |
---|
55 | gets copied to a new location: |
---|
56 | |
---|
57 | docs/sphinx-docs/source/user/[MODULE]/dir/A.rst |
---|
58 | docs/sphinx-docs/source/user/[MODULE]/B.rst |
---|
59 | |
---|
60 | so that Sphinx may pick it up when generating the documentation. |
---|
61 | """ |
---|
62 | print "=== Retrieve User Docs ===" |
---|
63 | |
---|
64 | # Copy documentation files from their "source" to their "destination". |
---|
65 | for root, dirnames, _ in os.walk(SASVIEW_SRC): |
---|
66 | for dirname in fnmatch.filter(dirnames, 'media'): |
---|
67 | |
---|
68 | docs = os.path.abspath(os.path.join(root, dirname)) |
---|
69 | print "Found docs folder at \"%s\"." % docs |
---|
70 | |
---|
71 | dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC)) |
---|
72 | if os.sep in dest_dir_part: |
---|
73 | dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:] |
---|
74 | dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part) |
---|
75 | |
---|
76 | copy_tree(docs, dest_dir) |
---|
77 | |
---|
78 | def apidoc(): |
---|
79 | """ |
---|
80 | Runs sphinx-apidoc to generate .rst files from the docstrings in .py files |
---|
81 | in the SasView build directory. |
---|
82 | """ |
---|
83 | print "=== Generate API Rest Files ===" |
---|
84 | |
---|
85 | # Clean directory before generating a new version. |
---|
86 | _remove_dir(SPHINX_SOURCE_API) |
---|
87 | |
---|
88 | subprocess.call(["sphinx-apidoc", |
---|
89 | "-o", SPHINX_SOURCE_API, # Output dir. |
---|
90 | "-d", "8", # Max depth of TOC. |
---|
91 | SASVIEW_BUILD]) |
---|
92 | |
---|
93 | def build(): |
---|
94 | """ |
---|
95 | Runs sphinx-build. Reads in all .rst files and spits out the final html. |
---|
96 | """ |
---|
97 | print "=== Build HTML Docs from Rest Files ===" |
---|
98 | subprocess.call(["sphinx-build", |
---|
99 | "-b", "html", # Builder name. TODO: accept as arg to setup.py. |
---|
100 | "-d", os.path.join(SPHINX_BUILD, "doctrees"), |
---|
101 | SPHINX_SOURCE, |
---|
102 | os.path.join(SPHINX_BUILD, "html")]) |
---|
103 | |
---|
104 | print "=== Copy HTML Docs to Build Directory ===" |
---|
105 | html = os.path.join(SPHINX_BUILD, "html") |
---|
106 | copy_tree(html, SASVIEW_DOCS) |
---|
107 | |
---|
108 | if __name__ == "__main__": |
---|
109 | clean() |
---|
110 | retrieve_user_docs() |
---|
111 | apidoc() |
---|
112 | build() |
---|
113 | |
---|
114 | print "=== Done ===" |
---|