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

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

With proto-copy of sasmodels docs for integration

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