source: sasview/docs/sphinx-docs/build_sphinx.py @ 74c5521

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 74c5521 was 5420ef6, checked in by smk78, 9 years ago

Fix to docs build errors resulting from reorganisation of image file
locations

  • Property mode set to 100755
File size: 12.9 KB
RevLine 
[9bf64f6]1#!/usr/bin/env python
[35bf493]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
[296f290]13import imp
[c22c5e3]14from glob import glob
[35bf493]15
16from distutils.dir_util import copy_tree
17from distutils.util import get_platform
[d2143ab]18from shutil import copy
[b7a2ebfd]19from os import listdir
[35bf493]20
21platform = '.%s-%s'%(get_platform(),sys.version[:3])
22
23CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
24
[296f290]25run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py'))
26run.prepare()
27
[35bf493]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")
[2fb09c4]31SASVIEW_TEST = os.path.join(SASVIEW_SRC, "..", "sasview", "test", "media")
[35bf493]32
[5802e7b]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
[a728658]36SASMODELS_SOURCE_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc")
[bb0c836]37SASMODELS_SOURCE_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "models")
[354524d]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")
[5420ef6]40SASMODELS_SOURCE_AUTOIMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "_build", "html","_images")
[a728658]41SASMODELS_DEST_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "source")
42SASMODELS_DEST_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user")
[756e8ce]43SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models")
[f603bec]44SASMODELS_DEST_IMG = os.path.join(CURRENT_SCRIPT_DIR,  "source", "user", "model-imgs", "new-models")
[5420ef6]45SASMODELS_DEST_BUILDIMG = os.path.join(CURRENT_SCRIPT_DIR,  "source", "user", "models", "img")
[5802e7b]46
[a728658]47#if os.path.exists(SASMODELS_SOURCE_PROLOG):
48#    print "Found models prolog folder at ", SASMODELS_SOURCE_PROLOG
49#if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
50#    print "Found models ref folder at ", SASMODELS_SOURCE_REF_MODELS
[b7a2ebfd]51#if os.path.exists(SASMODELS_SOURCE_MODELS):
52#    print "Found models folder at ", SASMODELS_SOURCE_MODELS
53#if os.path.exists(SASMODELS_SOURCE_IMG):
54#    print "Found img folder at ", SASMODELS_SOURCE_IMG
[a728658]55#if os.path.exists(SASMODELS_DEST_REF_MODELS):
56#    print "Found models ref folder at ", SASMODELS_DEST_REF_MODELS
[b7a2ebfd]57#if os.path.exists(SASMODELS_DEST_MODELS):
58#    print "Found models folder at ", SASMODELS_DEST_MODELS
59#if os.path.exists(SASMODELS_DEST_IMG):
60#    print "Found img folder at ", SASMODELS_DEST_IMG
[a728658]61#sys.exit()
[5802e7b]62
[35bf493]63SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
64SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source")
65SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api")
[ba31ed1]66SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe")
[7168b8b]67SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models")
[ba31ed1]68SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives")
[2fb09c4]69SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test")
[35bf493]70
[c22c5e3]71BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..",
72                          "bumps", "doc", "guide")
73BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting")
74
[bb0c836]75def inplace_change(filename, old_string, new_string):
76# Thanks to http://stackoverflow.com/questions/4128144/replace-string-within-file-contents
77        s=open(filename).read()
78        if old_string in s:
79                print 'Changing "{old_string}" to "{new_string}"'.format(**locals())
80                s=s.replace(old_string, new_string)
81                f=open(filename, 'w')
82                f.write(s)
83                f.flush()
84                f.close()
85        else:
86                print 'No occurences of "{old_string}" found.'.format(**locals())
87
[35bf493]88def _remove_dir(dir_path):
89    """Removes the given directory."""
90    if os.path.isdir(dir_path):
91        print "Removing \"%s\"... " % dir_path
92        shutil.rmtree(dir_path)
93
94def clean():
95    """
96    Clean the sphinx build directory.
97    """
98    print "=== Cleaning Sphinx Build ==="
99    _remove_dir(SASVIEW_DOCS)
100    _remove_dir(SPHINX_BUILD)
[7168b8b]101    _remove_dir(SPHINX_SOURCE_GUIFRAME)
102    _remove_dir(SPHINX_SOURCE_MODELS)
103    _remove_dir(SPHINX_SOURCE_PERSPECTIVES)
[2fb09c4]104    _remove_dir(SPHINX_SOURCE_TEST)
[35bf493]105
[354524d]106
[35bf493]107def retrieve_user_docs():
108    """
109    Copies across the contents of any media/ directories in src/, and puts them
110    in an appropriately named directory of docs/sphinx-docs/source/. For
111    example:
112
113        sas/../[MODULE]/media/dir/A.rst
114        sas/../[MODULE]/media/B.rst
115
116    gets copied to a new location:
117
118        docs/sphinx-docs/source/user/[MODULE]/dir/A.rst
119        docs/sphinx-docs/source/user/[MODULE]/B.rst
120
121    so that Sphinx may pick it up when generating the documentation.
122    """
123    print "=== Retrieve User Docs ==="
124
125    # Copy documentation files from their "source" to their "destination".
126    for root, dirnames, _ in os.walk(SASVIEW_SRC):
127        for dirname in fnmatch.filter(dirnames, 'media'):
128
129            docs = os.path.abspath(os.path.join(root, dirname))
130            print "Found docs folder at \"%s\"." % docs
131
[f620870]132            dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC))
133            if os.sep in dest_dir_part:
134                dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:]
135            dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part)
[35bf493]136
137            copy_tree(docs, dest_dir)
[2fb09c4]138           
139    # Now pickup testdata_help.rst
140#    print os.path.abspath(SASVIEW_TEST)
141#    print os.path.abspath(SPHINX_SOURCE_TEST)
[354524d]142    print "=== Including Test Data Docs ==="
[2fb09c4]143    if os.path.exists(SASVIEW_TEST):
144       print "Found docs folder at ", SASVIEW_TEST
145       shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST)       
[354524d]146
147    print "=== And the Sasmodels Docs ===" 
[6a9c0e5a]148    # Make sure we have the relevant images for the new sasmodels documentation
[5420ef6]149    # First(!) we'll make a local reference copy for SasView (/new-models will be cleaned each build)
[b7a2ebfd]150    if os.path.exists(SASMODELS_SOURCE_IMG):
151        print "Found img  folder SASMODELS_SOURCE_IMG    at ", SASMODELS_SOURCE_IMG
152        if os.path.exists(SASMODELS_DEST_IMG):
153            print "Found img  folder SASMODELS_DEST_IMG      at ", SASMODELS_DEST_IMG
[a728658]154            print "Copying sasmodels model image files..."
[b7a2ebfd]155            for files in os.listdir(SASMODELS_SOURCE_IMG):
156                fromhere=os.path.join(SASMODELS_SOURCE_IMG,files)
157                tohere=os.path.join(SASMODELS_DEST_IMG,files)
158                shutil.copy(fromhere,tohere)
159
[5420ef6]160    if os.path.exists(SASMODELS_SOURCE_AUTOIMG):
161        print "Found img  folder SASMODELS_SOURCE_AUTOIMG    at ", SASMODELS_SOURCE_AUTOIMG
162        if os.path.exists(SASMODELS_DEST_IMG):
163            print "Copying sasmodels model auto-generated image files..."
164            for files in os.listdir(SASMODELS_SOURCE_AUTOIMG):
165                fromhere=os.path.join(SASMODELS_SOURCE_AUTOIMG,files)
166                tohere=os.path.join(SASMODELS_DEST_IMG,files)
167                shutil.copy(fromhere,tohere)
168   
[a728658]169    # And the rst prolog with the unit substitutions
170    if os.path.exists(SASMODELS_SOURCE_PROLOG):
171        print "Found prolog folder SASMODELS_SOURCE_PROLOG at ", SASMODELS_SOURCE_PROLOG
172        if os.path.exists(SASMODELS_DEST_PROLOG):
173            print "Found docs folder SASMODELS_DEST_PROLOG   at ", SASMODELS_DEST_PROLOG
174            print "Copying sasmodels rst_prolog file..."
175            for files in os.listdir(SASMODELS_SOURCE_PROLOG):
176                if files.startswith("rst"):
177                    fromhere=os.path.join(SASMODELS_SOURCE_PROLOG,files)
178                    tohere=os.path.join(SASMODELS_DEST_PROLOG,files)
179                    shutil.copy(fromhere,tohere)
180
[bb0c836]181    if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
182        print "Found docs folder SASMODELS_SOURCE_REF_MODELS at ", SASMODELS_SOURCE_REF_MODELS
183        if os.path.exists(SASMODELS_DEST_REF_MODELS):
184            print "Found docs folder SASMODELS_DEST_REF_MODELS   at ", SASMODELS_DEST_REF_MODELS
185            print "Copying sasmodels model toctree files..."
186            for files in os.listdir(SASMODELS_SOURCE_REF_MODELS):
187                if files.endswith(".rst"):
188                    fromhere=os.path.join(SASMODELS_SOURCE_REF_MODELS,files)
189                    tohere=os.path.join(SASMODELS_DEST_REF_MODELS,files)
190                    shutil.copy(fromhere,tohere)
191    # But need to change the path to the model docs in the tocs
192    for files in os.listdir(SASMODELS_DEST_REF_MODELS):
193#        print files
194        if files.startswith("shape"):
195            print "Changing toc paths in", files
196            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
197        if files.startswith("sphere"):
198            print "Changing toc paths in", files
199            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
200        if files.startswith("custom"):
201            print "Changing toc paths in", files
202            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
203        if files.startswith("structure"):
204            print "Changing toc paths in", files
205            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
[a728658]206
[b7a2ebfd]207    if os.path.exists(SASMODELS_SOURCE_MODELS):
208        print "Found docs folder SASMODELS_SOURCE_MODELS at ", SASMODELS_SOURCE_MODELS
209        if os.path.exists(SASMODELS_DEST_MODELS):
210            print "Found docs folder SASMODELS_DEST_MODELS   at ", SASMODELS_DEST_MODELS
[a728658]211            print "Copying sasmodels model files..."
[b7a2ebfd]212            for files in os.listdir(SASMODELS_SOURCE_MODELS):
[354524d]213                if files.endswith(".rst"):
[b7a2ebfd]214                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
215                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
216                    shutil.copy(fromhere,tohere)
[5420ef6]217        else:
218            print "Missing docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS
219            os.makedirs(SASMODELS_DEST_MODELS)
220            if not os.path.exists(SASMODELS_DEST_BUILDIMG):
221                os.makedirs(SASMODELS_DEST_BUILDIMG)
222            print "Created docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS
223            print "Copying model files for build..."
224            for files in os.listdir(SASMODELS_SOURCE_MODELS):
225                if files.endswith(".rst"):
226                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
227                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
228                    shutil.copy(fromhere,tohere)
229            # No choice but to do this because model files are all coded for images in /models/img
230            print "Copying image files for build..."
231            for files in os.listdir(SASMODELS_DEST_IMG):
232                fromhere=os.path.join(SASMODELS_DEST_IMG,files)
233                tohere=os.path.join(SASMODELS_DEST_BUILDIMG,files)
234                shutil.copy(fromhere,tohere)
[6a9c0e5a]235
[35bf493]236
[c22c5e3]237def retrieve_bumps_docs():
238    """
239    Copies select files from the bumps documentation into fitting perspective
240    """
241    if os.path.exists(BUMPS_DOCS):
242        print "=== Retrieve BUMPS Docs ==="
243        filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")]
244        filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png"))
245        filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png"))
246        for f in filenames:
247            print "Copying file", f
248            shutil.copy(f, BUMPS_TARGET)
249    else:
250        print """
251*** Error *** missing directory %s
252The documentation will not include the optimizer selection section.
253Checkout the bumps source tree and rebuild the docs.
254
255
256""" % BUMPS_DOCS
257
[35bf493]258def apidoc():
259    """
260    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
261    in the SasView build directory.
262    """
263    print "=== Generate API Rest Files ==="
264
265    # Clean directory before generating a new version.
266    _remove_dir(SPHINX_SOURCE_API)
267
268    subprocess.call(["sphinx-apidoc",
269                     "-o", SPHINX_SOURCE_API, # Output dir.
270                     "-d", "8", # Max depth of TOC.
271                     SASVIEW_BUILD])
272
273def build():
274    """
275    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
276    """
277    print "=== Build HTML Docs from Rest Files ==="
278    subprocess.call(["sphinx-build",
279                     "-b", "html", # Builder name. TODO: accept as arg to setup.py.
280                     "-d", os.path.join(SPHINX_BUILD, "doctrees"),
281                     SPHINX_SOURCE,
282                     os.path.join(SPHINX_BUILD, "html")])
283
284    print "=== Copy HTML Docs to Build Directory ==="
285    html = os.path.join(SPHINX_BUILD, "html")
286    copy_tree(html, SASVIEW_DOCS)
287
[c2ee2b1]288def rebuild():
[35bf493]289    clean()
290    retrieve_user_docs()
[c22c5e3]291    retrieve_bumps_docs()
[35bf493]292    apidoc()
293    build()
[c2ee2b1]294
295    print "=== Done ==="
296
297if __name__ == "__main__":
[9bf64f6]298    rebuild()
Note: See TracBrowser for help on using the repository browser.