source: sasview/docs/sphinx-docs/build_sphinx.py @ 5f5c596

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 5f5c596 was 756e8ce, checked in by smk78, 8 years ago

Change so sasmodels docs copy no longer going to sasview src folder

  • Property mode set to 100755
File size: 9.8 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
36SASMODELS_SOURCE_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc")
37#SASMODELS_SOURCE_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "models")
38SASMODELS_SOURCE_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model")
39SASMODELS_SOURCE_IMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model", "img")
40SASMODELS_DEST_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "source")
41SASMODELS_DEST_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user")
42SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models")
43SASMODELS_DEST_IMG = os.path.join(CURRENT_SCRIPT_DIR,  "source", "user", "models", "img")
44
45#if os.path.exists(SASMODELS_SOURCE_PROLOG):
46#    print "Found models prolog folder at ", SASMODELS_SOURCE_PROLOG
47#if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
48#    print "Found models ref folder at ", SASMODELS_SOURCE_REF_MODELS
49#if os.path.exists(SASMODELS_SOURCE_MODELS):
50#    print "Found models folder at ", SASMODELS_SOURCE_MODELS
51#if os.path.exists(SASMODELS_SOURCE_IMG):
52#    print "Found img folder at ", SASMODELS_SOURCE_IMG
53#if os.path.exists(SASMODELS_DEST_REF_MODELS):
54#    print "Found models ref folder at ", SASMODELS_DEST_REF_MODELS
55#if os.path.exists(SASMODELS_DEST_MODELS):
56#    print "Found models folder at ", SASMODELS_DEST_MODELS
57#if os.path.exists(SASMODELS_DEST_IMG):
58#    print "Found img folder at ", SASMODELS_DEST_IMG
59#sys.exit()
60
61SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
62SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source")
63SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api")
64SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe")
65SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models")
66SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives")
67SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test")
68
69BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..",
70                          "bumps", "doc", "guide")
71BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting")
72
73def _remove_dir(dir_path):
74    """Removes the given directory."""
75    if os.path.isdir(dir_path):
76        print "Removing \"%s\"... " % dir_path
77        shutil.rmtree(dir_path)
78
79def clean():
80    """
81    Clean the sphinx build directory.
82    """
83    print "=== Cleaning Sphinx Build ==="
84    _remove_dir(SASVIEW_DOCS)
85    _remove_dir(SPHINX_BUILD)
86    _remove_dir(SPHINX_SOURCE_GUIFRAME)
87    _remove_dir(SPHINX_SOURCE_MODELS)
88    _remove_dir(SPHINX_SOURCE_PERSPECTIVES)
89    _remove_dir(SPHINX_SOURCE_TEST)
90
91
92def retrieve_user_docs():
93    """
94    Copies across the contents of any media/ directories in src/, and puts them
95    in an appropriately named directory of docs/sphinx-docs/source/. For
96    example:
97
98        sas/../[MODULE]/media/dir/A.rst
99        sas/../[MODULE]/media/B.rst
100
101    gets copied to a new location:
102
103        docs/sphinx-docs/source/user/[MODULE]/dir/A.rst
104        docs/sphinx-docs/source/user/[MODULE]/B.rst
105
106    so that Sphinx may pick it up when generating the documentation.
107    """
108    print "=== Retrieve User Docs ==="
109
110    # Copy documentation files from their "source" to their "destination".
111    for root, dirnames, _ in os.walk(SASVIEW_SRC):
112        for dirname in fnmatch.filter(dirnames, 'media'):
113
114            docs = os.path.abspath(os.path.join(root, dirname))
115            print "Found docs folder at \"%s\"." % docs
116
117            dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC))
118            if os.sep in dest_dir_part:
119                dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:]
120            dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part)
121
122            copy_tree(docs, dest_dir)
123           
124    # Now pickup testdata_help.rst
125#    print os.path.abspath(SASVIEW_TEST)
126#    print os.path.abspath(SPHINX_SOURCE_TEST)
127    print "=== Including Test Data Docs ==="
128    if os.path.exists(SASVIEW_TEST):
129       print "Found docs folder at ", SASVIEW_TEST
130       shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST)       
131
132    print "=== And the Sasmodels Docs ===" 
133    # Make sure we have the relevant images for the new sasmodels documentation
134    if os.path.exists(SASMODELS_SOURCE_IMG):
135        print "Found img  folder SASMODELS_SOURCE_IMG    at ", SASMODELS_SOURCE_IMG
136        if os.path.exists(SASMODELS_DEST_IMG):
137            print "Found img  folder SASMODELS_DEST_IMG      at ", SASMODELS_DEST_IMG
138            print "Copying sasmodels model image files..."
139            for files in os.listdir(SASMODELS_SOURCE_IMG):
140                fromhere=os.path.join(SASMODELS_SOURCE_IMG,files)
141                tohere=os.path.join(SASMODELS_DEST_IMG,files)
142                shutil.copy(fromhere,tohere)
143
144    # And the rst prolog with the unit substitutions
145    if os.path.exists(SASMODELS_SOURCE_PROLOG):
146        print "Found prolog folder SASMODELS_SOURCE_PROLOG at ", SASMODELS_SOURCE_PROLOG
147        if os.path.exists(SASMODELS_DEST_PROLOG):
148            print "Found docs folder SASMODELS_DEST_PROLOG   at ", SASMODELS_DEST_PROLOG
149            print "Copying sasmodels rst_prolog file..."
150            for files in os.listdir(SASMODELS_SOURCE_PROLOG):
151                if files.startswith("rst"):
152                    fromhere=os.path.join(SASMODELS_SOURCE_PROLOG,files)
153                    tohere=os.path.join(SASMODELS_DEST_PROLOG,files)
154                    shutil.copy(fromhere,tohere)
155
156    # ACTUALLY IT TURNED OUT WE NEED TO EDIT THE TOCTREES SO THIS COPY WASN'T USEFUL IN THE END!
157    # And the relevant rst descriptions for the new sasmodels documentation
158#    if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
159#        print "Found docs folder SASMODELS_SOURCE_REF_MODELS at ", SASMODELS_SOURCE_REF_MODELS
160#        if os.path.exists(SASMODELS_DEST_REF_MODELS):
161#            print "Found docs folder SASMODELS_DEST_REF_MODELS   at ", SASMODELS_DEST_REF_MODELS
162#            print "Copying sasmodels model toctree files..."
163#            for files in os.listdir(SASMODELS_SOURCE_REF_MODELS):
164#                if files.endswith(".rst"):
165#                    fromhere=os.path.join(SASMODELS_SOURCE_REF_MODELS,files)
166#                    tohere=os.path.join(SASMODELS_DEST_REF_MODELS,files)
167#                    shutil.copy(fromhere,tohere)
168
169    if os.path.exists(SASMODELS_SOURCE_MODELS):
170        print "Found docs folder SASMODELS_SOURCE_MODELS at ", SASMODELS_SOURCE_MODELS
171        if os.path.exists(SASMODELS_DEST_MODELS):
172            print "Found docs folder SASMODELS_DEST_MODELS   at ", SASMODELS_DEST_MODELS
173            print "Copying sasmodels model files..."
174            for files in os.listdir(SASMODELS_SOURCE_MODELS):
175                if files.endswith(".rst"):
176                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
177                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
178                    shutil.copy(fromhere,tohere)
179
180
181def retrieve_bumps_docs():
182    """
183    Copies select files from the bumps documentation into fitting perspective
184    """
185    if os.path.exists(BUMPS_DOCS):
186        print "=== Retrieve BUMPS Docs ==="
187        filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")]
188        filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png"))
189        filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png"))
190        for f in filenames:
191            print "Copying file", f
192            shutil.copy(f, BUMPS_TARGET)
193    else:
194        print """
195*** Error *** missing directory %s
196The documentation will not include the optimizer selection section.
197Checkout the bumps source tree and rebuild the docs.
198
199
200""" % BUMPS_DOCS
201
202def apidoc():
203    """
204    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
205    in the SasView build directory.
206    """
207    print "=== Generate API Rest Files ==="
208
209    # Clean directory before generating a new version.
210    _remove_dir(SPHINX_SOURCE_API)
211
212    subprocess.call(["sphinx-apidoc",
213                     "-o", SPHINX_SOURCE_API, # Output dir.
214                     "-d", "8", # Max depth of TOC.
215                     SASVIEW_BUILD])
216
217def build():
218    """
219    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
220    """
221    print "=== Build HTML Docs from Rest Files ==="
222    subprocess.call(["sphinx-build",
223                     "-b", "html", # Builder name. TODO: accept as arg to setup.py.
224                     "-d", os.path.join(SPHINX_BUILD, "doctrees"),
225                     SPHINX_SOURCE,
226                     os.path.join(SPHINX_BUILD, "html")])
227
228    print "=== Copy HTML Docs to Build Directory ==="
229    html = os.path.join(SPHINX_BUILD, "html")
230    copy_tree(html, SASVIEW_DOCS)
231
232def rebuild():
233    clean()
234    retrieve_user_docs()
235    retrieve_bumps_docs()
236    apidoc()
237    build()
238
239    print "=== Done ==="
240
241if __name__ == "__main__":
242    rebuild()
Note: See TracBrowser for help on using the repository browser.