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

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 f9fbe99 was eff1a8fc, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

fix type in error message

  • Property mode set to 100755
File size: 13.4 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
[6de5e10]152        if not os.path.exists(SASMODELS_DEST_IMG):
153            print "Missing docs folder SASMODELS_DEST_IMG at ", SASMODELS_DEST_IMG
154            os.makedirs(SASMODELS_DEST_IMG)
155            print "created SASMODELS_DEST_BUILDIMG at ", SASMODELS_DEST_BUILDIMG
156        else: print "Found img  folder SASMODELS_DEST_IMG      at ", SASMODELS_DEST_IMG
157        print "Copying sasmodels model image files..."
158        for files in os.listdir(SASMODELS_SOURCE_IMG):
159            fromhere=os.path.join(SASMODELS_SOURCE_IMG,files)
160            tohere=os.path.join(SASMODELS_DEST_IMG,files)
161            shutil.copy(fromhere,tohere)
162    else: print "cannot find SASMODELS_SOURCE_IMG", SASMODELS_SOURCE_IMG
[b7a2ebfd]163
[5420ef6]164    if os.path.exists(SASMODELS_SOURCE_AUTOIMG):
165        print "Found img  folder SASMODELS_SOURCE_AUTOIMG    at ", SASMODELS_SOURCE_AUTOIMG
[6de5e10]166        if not os.path.exists(SASMODELS_DEST_IMG):
[eff1a8fc]167            print "Missing docs folder SASMODELS_DEST_IMG at ", SASMODELS_DEST_IMG
[6de5e10]168            os.makedirs(SASMODELS_DEST_BUILDIMG)
169            print "created SASMODELS_DEST_BUILDIMG at ", SASMODELS_DEST_BUILDIMG
170        print "Copying sasmodels model auto-generated image files..."
171        for files in os.listdir(SASMODELS_SOURCE_AUTOIMG):
172            fromhere=os.path.join(SASMODELS_SOURCE_AUTOIMG,files)
173            tohere=os.path.join(SASMODELS_DEST_IMG,files)
174            shutil.copy(fromhere,tohere)
175        else: print "no source directorty",SASMODELS_SOURCE_AUTOIMG ,"was found"
[5420ef6]176   
[a728658]177    # And the rst prolog with the unit substitutions
178    if os.path.exists(SASMODELS_SOURCE_PROLOG):
179        print "Found prolog folder SASMODELS_SOURCE_PROLOG at ", SASMODELS_SOURCE_PROLOG
180        if os.path.exists(SASMODELS_DEST_PROLOG):
181            print "Found docs folder SASMODELS_DEST_PROLOG   at ", SASMODELS_DEST_PROLOG
182            print "Copying sasmodels rst_prolog file..."
183            for files in os.listdir(SASMODELS_SOURCE_PROLOG):
184                if files.startswith("rst"):
185                    fromhere=os.path.join(SASMODELS_SOURCE_PROLOG,files)
186                    tohere=os.path.join(SASMODELS_DEST_PROLOG,files)
187                    shutil.copy(fromhere,tohere)
188
[bb0c836]189    if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
190        print "Found docs folder SASMODELS_SOURCE_REF_MODELS at ", SASMODELS_SOURCE_REF_MODELS
191        if os.path.exists(SASMODELS_DEST_REF_MODELS):
192            print "Found docs folder SASMODELS_DEST_REF_MODELS   at ", SASMODELS_DEST_REF_MODELS
193            print "Copying sasmodels model toctree files..."
194            for files in os.listdir(SASMODELS_SOURCE_REF_MODELS):
195                if files.endswith(".rst"):
196                    fromhere=os.path.join(SASMODELS_SOURCE_REF_MODELS,files)
197                    tohere=os.path.join(SASMODELS_DEST_REF_MODELS,files)
198                    shutil.copy(fromhere,tohere)
199    # But need to change the path to the model docs in the tocs
200    for files in os.listdir(SASMODELS_DEST_REF_MODELS):
201#        print files
202        if files.startswith("shape"):
203            print "Changing toc paths in", files
204            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
205        if files.startswith("sphere"):
206            print "Changing toc paths in", files
207            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
208        if files.startswith("custom"):
209            print "Changing toc paths in", files
210            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
211        if files.startswith("structure"):
212            print "Changing toc paths in", files
213            inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
[a728658]214
[b7a2ebfd]215    if os.path.exists(SASMODELS_SOURCE_MODELS):
216        print "Found docs folder SASMODELS_SOURCE_MODELS at ", SASMODELS_SOURCE_MODELS
217        if os.path.exists(SASMODELS_DEST_MODELS):
218            print "Found docs folder SASMODELS_DEST_MODELS   at ", SASMODELS_DEST_MODELS
[a728658]219            print "Copying sasmodels model files..."
[b7a2ebfd]220            for files in os.listdir(SASMODELS_SOURCE_MODELS):
[354524d]221                if files.endswith(".rst"):
[b7a2ebfd]222                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
223                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
224                    shutil.copy(fromhere,tohere)
[5420ef6]225        else:
226            print "Missing docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS
227            os.makedirs(SASMODELS_DEST_MODELS)
228            if not os.path.exists(SASMODELS_DEST_BUILDIMG):
229                os.makedirs(SASMODELS_DEST_BUILDIMG)
230            print "Created docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS
231            print "Copying model files for build..."
232            for files in os.listdir(SASMODELS_SOURCE_MODELS):
233                if files.endswith(".rst"):
234                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
235                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
236                    shutil.copy(fromhere,tohere)
237            # No choice but to do this because model files are all coded for images in /models/img
238            print "Copying image files for build..."
239            for files in os.listdir(SASMODELS_DEST_IMG):
240                fromhere=os.path.join(SASMODELS_DEST_IMG,files)
241                tohere=os.path.join(SASMODELS_DEST_BUILDIMG,files)
242                shutil.copy(fromhere,tohere)
[6a9c0e5a]243
[35bf493]244
[c22c5e3]245def retrieve_bumps_docs():
246    """
247    Copies select files from the bumps documentation into fitting perspective
248    """
249    if os.path.exists(BUMPS_DOCS):
250        print "=== Retrieve BUMPS Docs ==="
251        filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")]
252        filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png"))
253        filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png"))
254        for f in filenames:
255            print "Copying file", f
256            shutil.copy(f, BUMPS_TARGET)
257    else:
258        print """
259*** Error *** missing directory %s
260The documentation will not include the optimizer selection section.
261Checkout the bumps source tree and rebuild the docs.
262
263
264""" % BUMPS_DOCS
265
[35bf493]266def apidoc():
267    """
268    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
269    in the SasView build directory.
270    """
271    print "=== Generate API Rest Files ==="
272
273    # Clean directory before generating a new version.
274    _remove_dir(SPHINX_SOURCE_API)
275
276    subprocess.call(["sphinx-apidoc",
277                     "-o", SPHINX_SOURCE_API, # Output dir.
278                     "-d", "8", # Max depth of TOC.
279                     SASVIEW_BUILD])
280
281def build():
282    """
283    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
284    """
285    print "=== Build HTML Docs from Rest Files ==="
286    subprocess.call(["sphinx-build",
287                     "-b", "html", # Builder name. TODO: accept as arg to setup.py.
288                     "-d", os.path.join(SPHINX_BUILD, "doctrees"),
289                     SPHINX_SOURCE,
290                     os.path.join(SPHINX_BUILD, "html")])
291
292    print "=== Copy HTML Docs to Build Directory ==="
293    html = os.path.join(SPHINX_BUILD, "html")
294    copy_tree(html, SASVIEW_DOCS)
295
[c2ee2b1]296def rebuild():
[35bf493]297    clean()
298    retrieve_user_docs()
[c22c5e3]299    retrieve_bumps_docs()
[35bf493]300    apidoc()
301    build()
[c2ee2b1]302
303    print "=== Done ==="
304
305if __name__ == "__main__":
[9bf64f6]306    rebuild()
Note: See TracBrowser for help on using the repository browser.