source: sasview/docs/sphinx-docs/build_sphinx.py @ 5911718

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 5911718 was 9bf64f6, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

all build_sphinx.py to run as command again

  • Property mode set to 100755
File size: 4.8 KB
Line 
1#!/usr/bin/env python
2"""
3Functions for building sphinx docs.
4
5For more information on the invocation of sphinx see:
6http://sphinx-doc.org/invocation.html
7"""
8import subprocess
9import os
10import sys
11import fnmatch
12import shutil
13import imp
14from glob import glob
15
16from distutils.dir_util import copy_tree
17from distutils.util import get_platform
18
19platform = '.%s-%s'%(get_platform(),sys.version[:3])
20
21CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
22
23run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py'))
24run.prepare()
25
26SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src")
27SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform))
28SASVIEW_DOCS = os.path.join(SASVIEW_BUILD, "doc")
29
30SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
31SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source")
32SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api")
33SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "guiframe")
34SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models")
35SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "perspectives")
36
37BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..",
38                          "bumps", "doc", "guide")
39BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting")
40
41def _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
47def 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
58def 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
90def 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
105The documentation will not include the optimizer selection section.
106Checkout the bumps source tree and rebuild the docs.
107
108
109""" % BUMPS_DOCS
110
111def 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
126def 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
141def rebuild():
142    clean()
143    retrieve_user_docs()
144    retrieve_bumps_docs()
145    apidoc()
146    build()
147
148    print "=== Done ==="
149
150if __name__ == "__main__":
151    rebuild()
Note: See TracBrowser for help on using the repository browser.