source: sasview/docs/sphinx-docs/build_sphinx.py @ 372bb7c

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

Minor change

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