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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since a6026f52 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
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")
37SASMODELS_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_SOURCE_AUTOIMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "_build", "html","_images")
41SASMODELS_DEST_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "source")
42SASMODELS_DEST_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user")
43SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "source", "user", "models")
44SASMODELS_DEST_IMG = os.path.join(CURRENT_SCRIPT_DIR,  "source", "user", "model-imgs", "new-models")
45SASMODELS_DEST_BUILDIMG = os.path.join(CURRENT_SCRIPT_DIR,  "source", "user", "models", "img")
46
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
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
55#if os.path.exists(SASMODELS_DEST_REF_MODELS):
56#    print "Found models ref folder at ", SASMODELS_DEST_REF_MODELS
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
61#sys.exit()
62
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")
66SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe")
67SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models")
68SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives")
69SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test")
70
71BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..",
72                          "bumps", "doc", "guide")
73BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting")
74
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
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)
101    _remove_dir(SPHINX_SOURCE_GUIFRAME)
102    _remove_dir(SPHINX_SOURCE_MODELS)
103    _remove_dir(SPHINX_SOURCE_PERSPECTIVES)
104    _remove_dir(SPHINX_SOURCE_TEST)
105
106
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
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)
136
137            copy_tree(docs, dest_dir)
138           
139    # Now pickup testdata_help.rst
140#    print os.path.abspath(SASVIEW_TEST)
141#    print os.path.abspath(SPHINX_SOURCE_TEST)
142    print "=== Including Test Data Docs ==="
143    if os.path.exists(SASVIEW_TEST):
144       print "Found docs folder at ", SASVIEW_TEST
145       shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST)       
146
147    print "=== And the Sasmodels Docs ===" 
148    # Make sure we have the relevant images for the new sasmodels documentation
149    # First(!) we'll make a local reference copy for SasView (/new-models will be cleaned each build)
150    if os.path.exists(SASMODELS_SOURCE_IMG):
151        print "Found img  folder SASMODELS_SOURCE_IMG    at ", SASMODELS_SOURCE_IMG
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
163
164    if os.path.exists(SASMODELS_SOURCE_AUTOIMG):
165        print "Found img  folder SASMODELS_SOURCE_AUTOIMG    at ", SASMODELS_SOURCE_AUTOIMG
166        if not os.path.exists(SASMODELS_DEST_IMG):
167            print "Missing docs folder SASMODELS_DEST_IMG at ", SASMODELS_DEST_IMG
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"
176   
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
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/")
214
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
219            print "Copying sasmodels model files..."
220            for files in os.listdir(SASMODELS_SOURCE_MODELS):
221                if files.endswith(".rst"):
222                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
223                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
224                    shutil.copy(fromhere,tohere)
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)
243
244
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
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
296def rebuild():
297    clean()
298    retrieve_user_docs()
299    retrieve_bumps_docs()
300    apidoc()
301    build()
302
303    print "=== Done ==="
304
305if __name__ == "__main__":
306    rebuild()
Note: See TracBrowser for help on using the repository browser.