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

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 fb3f9af was 2fb09c4, checked in by smk78, 9 years ago

Adding documentation for test data to sphinx build

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