source: sasview/docs/sphinx-docs/build_sphinx.py @ 333575b

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 333575b was 354524d, checked in by smk78, 9 years ago

Now copying sasmodels rst files to sasview and building

  • Property mode set to 100755
File size: 7.6 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", "doc", "model")
38SASMODELS_SOURCE_IMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model", "img")
39SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "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
81
82def retrieve_user_docs():
83    """
84    Copies across the contents of any media/ directories in src/, and puts them
85    in an appropriately named directory of docs/sphinx-docs/source/. For
86    example:
87
88        sas/../[MODULE]/media/dir/A.rst
89        sas/../[MODULE]/media/B.rst
90
91    gets copied to a new location:
92
93        docs/sphinx-docs/source/user/[MODULE]/dir/A.rst
94        docs/sphinx-docs/source/user/[MODULE]/B.rst
95
96    so that Sphinx may pick it up when generating the documentation.
97    """
98    print "=== Retrieve User Docs ==="
99
100    # Copy documentation files from their "source" to their "destination".
101    for root, dirnames, _ in os.walk(SASVIEW_SRC):
102        for dirname in fnmatch.filter(dirnames, 'media'):
103
104            docs = os.path.abspath(os.path.join(root, dirname))
105            print "Found docs folder at \"%s\"." % docs
106
107            dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC))
108            if os.sep in dest_dir_part:
109                dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:]
110            dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part)
111
112            copy_tree(docs, dest_dir)
113           
114    # Now pickup testdata_help.rst
115#    print os.path.abspath(SASVIEW_TEST)
116#    print os.path.abspath(SPHINX_SOURCE_TEST)
117    print "=== Including Test Data Docs ==="
118    if os.path.exists(SASVIEW_TEST):
119       print "Found docs folder at ", SASVIEW_TEST
120       shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST)       
121
122    print "=== And the Sasmodels Docs ===" 
123    # Make sure we have the relevant images for the new sasmodels documentation
124    if os.path.exists(SASMODELS_SOURCE_IMG):
125        print "Found img  folder SASMODELS_SOURCE_IMG    at ", SASMODELS_SOURCE_IMG
126        if os.path.exists(SASMODELS_DEST_IMG):
127            print "Found img  folder SASMODELS_DEST_IMG      at ", SASMODELS_DEST_IMG
128            for files in os.listdir(SASMODELS_SOURCE_IMG):
129                fromhere=os.path.join(SASMODELS_SOURCE_IMG,files)
130                tohere=os.path.join(SASMODELS_DEST_IMG,files)
131                shutil.copy(fromhere,tohere)
132
133    # And the relevant rst descriptions for the new sasmodels documentation
134    if os.path.exists(SASMODELS_SOURCE_MODELS):
135        print "Found docs folder SASMODELS_SOURCE_MODELS at ", SASMODELS_SOURCE_MODELS
136        if os.path.exists(SASMODELS_DEST_MODELS):
137            print "Found docs folder SASMODELS_DEST_MODELS   at ", SASMODELS_DEST_MODELS
138            for files in os.listdir(SASMODELS_SOURCE_MODELS):
139                if files.endswith(".rst"):
140                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
141                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
142                    shutil.copy(fromhere,tohere)
143
144
145def retrieve_bumps_docs():
146    """
147    Copies select files from the bumps documentation into fitting perspective
148    """
149    if os.path.exists(BUMPS_DOCS):
150        print "=== Retrieve BUMPS Docs ==="
151        filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")]
152        filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png"))
153        filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png"))
154        for f in filenames:
155            print "Copying file", f
156            shutil.copy(f, BUMPS_TARGET)
157    else:
158        print """
159*** Error *** missing directory %s
160The documentation will not include the optimizer selection section.
161Checkout the bumps source tree and rebuild the docs.
162
163
164""" % BUMPS_DOCS
165
166def apidoc():
167    """
168    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
169    in the SasView build directory.
170    """
171    print "=== Generate API Rest Files ==="
172
173    # Clean directory before generating a new version.
174    _remove_dir(SPHINX_SOURCE_API)
175
176    subprocess.call(["sphinx-apidoc",
177                     "-o", SPHINX_SOURCE_API, # Output dir.
178                     "-d", "8", # Max depth of TOC.
179                     SASVIEW_BUILD])
180
181def build():
182    """
183    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
184    """
185    print "=== Build HTML Docs from Rest Files ==="
186    subprocess.call(["sphinx-build",
187                     "-b", "html", # Builder name. TODO: accept as arg to setup.py.
188                     "-d", os.path.join(SPHINX_BUILD, "doctrees"),
189                     SPHINX_SOURCE,
190                     os.path.join(SPHINX_BUILD, "html")])
191
192    print "=== Copy HTML Docs to Build Directory ==="
193    html = os.path.join(SPHINX_BUILD, "html")
194    copy_tree(html, SASVIEW_DOCS)
195
196def rebuild():
197    clean()
198    retrieve_user_docs()
199    retrieve_bumps_docs()
200    apidoc()
201    build()
202
203    print "=== Done ==="
204
205if __name__ == "__main__":
206    rebuild()
Note: See TracBrowser for help on using the repository browser.