source: sasview/docs/sphinx-docs/build_sphinx.py @ 5802e7b

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 5802e7b was 5802e7b, checked in by smk78, 8 years ago

Start paving the way to build sasview and sasmodels documentation in one
go: first, find the feckin' files…

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