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 | |
---|
13 | from distutils.dir_util import copy_tree |
---|
14 | from distutils.util import get_platform |
---|
15 | |
---|
16 | platform = '.%s-%s'%(get_platform(),sys.version[:3]) |
---|
17 | |
---|
18 | CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
---|
19 | |
---|
20 | SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src") |
---|
21 | SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform)) |
---|
22 | |
---|
23 | SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build") |
---|
24 | SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source") |
---|
25 | SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev","api") |
---|
26 | |
---|
27 | def _remove_dir(dir_path): |
---|
28 | """Removes the given directory.""" |
---|
29 | if os.path.isdir(dir_path): |
---|
30 | print "Removing \"%s\"... " % dir_path |
---|
31 | shutil.rmtree(dir_path) |
---|
32 | |
---|
33 | def clean(): |
---|
34 | """ |
---|
35 | Clean the sphinx build directory. |
---|
36 | """ |
---|
37 | print "=== Cleaning Sphinx Build ===" |
---|
38 | _remove_dir(SPHINX_BUILD) |
---|
39 | |
---|
40 | def retrieve_user_docs(): |
---|
41 | """ |
---|
42 | Copies across the contents of any docs/ directories in src/, and puts them |
---|
43 | in an appropriately named directory of docs/sphinx-docs/source/. For |
---|
44 | example: |
---|
45 | |
---|
46 | sans/../[MODULE]/docs/A.rst |
---|
47 | sans/../[MODULE]/docs/B.rst |
---|
48 | |
---|
49 | gets copied to a new location: |
---|
50 | |
---|
51 | docs/sphinx-docs/source/user/[MODULE]/A.rst |
---|
52 | docs/sphinx-docs/source/user/[MODULE]/B.rst |
---|
53 | |
---|
54 | so that Sphinx may pick it up when generating the documentation. |
---|
55 | """ |
---|
56 | print "=== Retrieve User Docs ===" |
---|
57 | |
---|
58 | # Copy documentation files from their "source" to their "destination". |
---|
59 | for root, dirnames, _ in os.walk(SASVIEW_SRC): |
---|
60 | for dirname in fnmatch.filter(dirnames, 'docs'): |
---|
61 | |
---|
62 | docs = os.path.abspath(os.path.join(root, dirname)) |
---|
63 | print "Found docs folder at \"%s\"." % docs |
---|
64 | |
---|
65 | dest_dir_name = os.path.basename(os.path.dirname(docs)) |
---|
66 | dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_name) |
---|
67 | |
---|
68 | copy_tree(docs, dest_dir, update=1) |
---|
69 | |
---|
70 | def apidoc(): |
---|
71 | """ |
---|
72 | Runs sphinx-apidoc to generate .rst files from the docstrings in .py files |
---|
73 | in the SasView build directory. |
---|
74 | """ |
---|
75 | print "=== Generate API Rest Files ===" |
---|
76 | |
---|
77 | # Clean directory before generating a new version. |
---|
78 | _remove_dir(SPHINX_SOURCE_API) |
---|
79 | |
---|
80 | subprocess.call(["sphinx-apidoc", |
---|
81 | "-o", SPHINX_SOURCE_API, # Output dir. |
---|
82 | "-d", "8", # Max depth of TOC. |
---|
83 | SASVIEW_BUILD]) |
---|
84 | |
---|
85 | def build(): |
---|
86 | """ |
---|
87 | Runs sphinx-build. Reads in all .rst files and spits out the final html. |
---|
88 | """ |
---|
89 | print "=== Build HTML from Rest Files ===" |
---|
90 | subprocess.call(["sphinx-build", |
---|
91 | "-b", "html", # Builder name. |
---|
92 | "-d", os.path.join(SPHINX_BUILD, "doctrees"), # Cache directory (doctrees). |
---|
93 | SPHINX_SOURCE, |
---|
94 | os.path.join(SPHINX_BUILD, "html")]) |
---|
95 | |
---|
96 | if __name__ == "__main__": |
---|
97 | call() |
---|