source: sasview/docs/sphinx-docs/build_sphinx.py @ 115eb7e

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 115eb7e was 115eb7e, checked in by ajj, 7 years ago

Added warnings to build_sphix about missing directories.

Initial work to run sasmodels build from sasview setup.py

  • Property mode set to 100755
File size: 18.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")
32SASVIEW_TOC_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source")
33
34# Need to slurp in the new sasmodels model definitions to replace the old model_functions.rst
35# We are currently here:
36#/sasview-local-trunk/docs/sphinx-docs/build_sphinx.py
37SASMODELS_SOURCE_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc")
38SASMODELS_SOURCE_GPU = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "gpu")
39SASMODELS_SOURCE_SESANS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "sesans")
40SASMODELS_SOURCE_SESANSIMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "sesans", "sesans_img")
41SASMODELS_SOURCE_MAGNETISM = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "magnetism")
42SASMODELS_SOURCE_MAGIMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "magnetism", "mag_img")
43SASMODELS_SOURCE_REF_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "ref", "models")
44SASMODELS_SOURCE_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model")
45SASMODELS_SOURCE_IMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model", "img")
46SASMODELS_SOURCE_AUTOIMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "_build", "html","_images")
47## Don't do assemble-in-place
48## Assemble the docs in a temporary folder
49SASMODELS_DEST_PROLOG = os.path.join(CURRENT_SCRIPT_DIR, "source-temp")
50SASMODELS_DEST_REF_MODELS = os.path.join(SASMODELS_DEST_PROLOG, "user")
51SASMODELS_DEST_MODELS = os.path.join(SASMODELS_DEST_PROLOG, "user", "models")
52SASMODELS_DEST_IMG = os.path.join(SASMODELS_DEST_PROLOG, "user", "model-imgs", "new-models")
53SASMODELS_DEST_MAGIMG = os.path.join(SASMODELS_DEST_PROLOG, "user", "mag_img")
54SASMODELS_DEST_SESANSIMG = os.path.join(SASMODELS_DEST_PROLOG, "user", "sesans_img")
55SASMODELS_DEST_BUILDIMG = os.path.join(SASMODELS_DEST_PROLOG, "user", "models", "img")
56
57#if os.path.exists(SASMODELS_SOURCE_PROLOG):
58#    print "Found models prolog folder at ", SASMODELS_SOURCE_PROLOG
59#if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
60#    print "Found models ref folder at ", SASMODELS_SOURCE_REF_MODELS
61#if os.path.exists(SASMODELS_SOURCE_MODELS):
62#    print "Found models folder at ", SASMODELS_SOURCE_MODELS
63#if os.path.exists(SASMODELS_SOURCE_IMG):
64#    print "Found img folder at ", SASMODELS_SOURCE_IMG
65#if os.path.exists(SASMODELS_DEST_REF_MODELS):
66#    print "Found models ref folder at ", SASMODELS_DEST_REF_MODELS
67#if os.path.exists(SASMODELS_DEST_MODELS):
68#    print "Found models folder at ", SASMODELS_DEST_MODELS
69#if os.path.exists(SASMODELS_DEST_IMG):
70#    print "Found img folder at ", SASMODELS_DEST_IMG
71#sys.exit()
72
73SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
74SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source-temp")
75SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api")
76SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe")
77SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models")
78SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives")
79SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test")
80SPHINX_SOURCE_USER = os.path.join(SPHINX_SOURCE, "user")
81
82BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..",
83                          "bumps", "doc", "guide")
84BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting")
85
86def inplace_change(filename, old_string, new_string):
87# Thanks to http://stackoverflow.com/questions/4128144/replace-string-within-file-contents
88        s=open(filename).read()
89        if old_string in s:
90                print 'Changing "{old_string}" to "{new_string}"'.format(**locals())
91                s=s.replace(old_string, new_string)
92                f=open(filename, 'w')
93                f.write(s)
94                f.flush()
95                f.close()
96        else:
97                print 'No occurences of "{old_string}" found.'.format(**locals())
98
99def _remove_dir(dir_path):
100    """Removes the given directory."""
101    if os.path.isdir(dir_path):
102        print "Removing \"%s\"... " % dir_path
103        shutil.rmtree(dir_path)
104
105def clean():
106    """
107    Clean the sphinx build directory.
108    """
109    print "=== Cleaning Sphinx Build ==="
110    _remove_dir(SASVIEW_DOCS)
111    _remove_dir(SPHINX_BUILD)
112    _remove_dir(SPHINX_SOURCE)
113    #_remove_dir(SPHINX_SOURCE_GUIFRAME)
114    #_remove_dir(SPHINX_SOURCE_MODELS)
115    #_remove_dir(SPHINX_SOURCE_PERSPECTIVES)
116    #_remove_dir(SPHINX_SOURCE_TEST)
117
118def setup_source_temp():
119    """
120    Copy the source toctrees to new folder for assembling the sphinx-docs
121    """
122    print "=== Copying Source toctrees ==="
123    if os.path.exists(SASVIEW_TOC_SOURCE):
124       print "Found docs folder at ", SASVIEW_TOC_SOURCE
125       shutil.copytree(SASVIEW_TOC_SOURCE, SPHINX_SOURCE)
126
127def retrieve_user_docs():
128    """
129    Copies across the contents of any media/ directories in src/, and puts them
130    in an appropriately named directory of docs/sphinx-docs/source/. For
131    example:
132
133        sas/../[MODULE]/media/dir/A.rst
134        sas/../[MODULE]/media/B.rst
135
136    gets copied to a new location:
137
138        docs/sphinx-docs/source/user/[MODULE]/dir/A.rst
139        docs/sphinx-docs/source/user/[MODULE]/B.rst
140
141    so that Sphinx may pick it up when generating the documentation.
142    """
143    print "=== Retrieve User Docs ==="
144
145    # Copy documentation files from their "source" to their "destination".
146    for root, dirnames, _ in os.walk(SASVIEW_SRC):
147        for dirname in fnmatch.filter(dirnames, 'media'):
148
149            docs = os.path.abspath(os.path.join(root, dirname))
150            print "Found docs folder at \"%s\"." % docs
151
152            dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC))
153            if os.sep in dest_dir_part:
154                dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:]
155            dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part)
156
157            copy_tree(docs, dest_dir)
158
159    # Now pickup testdata_help.rst
160#    print os.path.abspath(SASVIEW_TEST)
161#    print os.path.abspath(SPHINX_SOURCE_TEST)
162    print "=== Including Test Data Docs ==="
163    if os.path.exists(SASVIEW_TEST):
164       print "Found docs folder at ", SASVIEW_TEST
165       shutil.copytree(SASVIEW_TEST, SPHINX_SOURCE_TEST)
166
167    print "=== And the Sasmodels Docs ==="
168    # Make sure we have the relevant images for the new sasmodels documentation
169    # First(!) we'll make a local reference copy for SasView (/new-models will be cleaned each build)
170    if os.path.exists(SASMODELS_SOURCE_IMG):
171        print "Found img  folder SASMODELS_SOURCE_IMG    at ", SASMODELS_SOURCE_IMG
172        if not os.path.exists(SASMODELS_DEST_IMG):
173            print "Missing docs folder SASMODELS_DEST_IMG at ", SASMODELS_DEST_IMG
174            os.makedirs(SASMODELS_DEST_IMG)
175            print "created SASMODELS_DEST_BUILDIMG at ", SASMODELS_DEST_BUILDIMG
176        else: print "Found img  folder SASMODELS_DEST_IMG      at ", SASMODELS_DEST_IMG
177        print "Copying sasmodels model image files..."
178        for files in os.listdir(SASMODELS_SOURCE_IMG):
179            fromhere=os.path.join(SASMODELS_SOURCE_IMG,files)
180            tohere=os.path.join(SASMODELS_DEST_IMG,files)
181            shutil.copy(fromhere,tohere)
182    else: print "cannot find SASMODELS_SOURCE_IMG", SASMODELS_SOURCE_IMG
183
184    if os.path.exists(SASMODELS_SOURCE_AUTOIMG):
185        print "Found img  folder SASMODELS_SOURCE_AUTOIMG    at ", SASMODELS_SOURCE_AUTOIMG
186        if not os.path.exists(SASMODELS_DEST_IMG):
187            print "Missing docs folder SASMODELS_DEST_IMG at ", SASMODELS_DEST_IMG
188            os.makedirs(SASMODELS_DEST_BUILDIMG)
189            print "created SASMODELS_DEST_BUILDIMG at ", SASMODELS_DEST_BUILDIMG
190        print "Copying sasmodels model auto-generated image files..."
191        for files in os.listdir(SASMODELS_SOURCE_AUTOIMG):
192            fromhere=os.path.join(SASMODELS_SOURCE_AUTOIMG,files)
193            tohere=os.path.join(SASMODELS_DEST_IMG,files)
194            shutil.copy(fromhere,tohere)
195    else: print "no source directory",SASMODELS_SOURCE_AUTOIMG ,"was found"
196
197    # And the rst prolog with the unit substitutions
198    if os.path.exists(SASMODELS_SOURCE_PROLOG):
199        print "Found prolog folder SASMODELS_SOURCE_PROLOG at ", SASMODELS_SOURCE_PROLOG
200        if os.path.exists(SASMODELS_DEST_PROLOG):
201            print "Found docs folder SASMODELS_DEST_PROLOG   at ", SASMODELS_DEST_PROLOG
202            print "Copying sasmodels rst_prolog file..."
203            for files in os.listdir(SASMODELS_SOURCE_PROLOG):
204                if files.startswith("rst"):
205                    fromhere=os.path.join(SASMODELS_SOURCE_PROLOG,files)
206                    tohere=os.path.join(SASMODELS_DEST_PROLOG,files)
207                    shutil.copy(fromhere,tohere)
208    else: print "no source directory",SASMODELS_SOURCE_PROLOG, "was found"
209
210    if os.path.exists(SASMODELS_SOURCE_GPU):
211        print "Found docs folder SASMODELS_SOURCE_GPU at ", SASMODELS_SOURCE_GPU
212        if os.path.exists(SPHINX_SOURCE_USER):
213            print "Found docs folder SPHINX_SOURCE_USER      at ", SPHINX_SOURCE_USER
214            print "Copying sasmodels gpu files..."
215            for files in os.listdir(SASMODELS_SOURCE_GPU):
216                if files.endswith(".rst"):
217                    fromhere=os.path.join(SASMODELS_SOURCE_GPU,files)
218                    tohere=os.path.join(SPHINX_SOURCE_USER,files)
219                    shutil.copy(fromhere,tohere)
220    else: print "no source directory",SASMODELS_SOURCE_GPU," was found"
221
222    if os.path.exists(SASMODELS_SOURCE_SESANS):
223        print "Found docs folder SASMODELS_SOURCE_SESANS at ", SASMODELS_SOURCE_SESANS
224        if os.path.exists(SPHINX_SOURCE_USER):
225            print "Found docs folder SPHINX_SOURCE_USER      at ", SPHINX_SOURCE_USER
226            print "Copying sasmodels sesans files..."
227            for files in os.listdir(SASMODELS_SOURCE_SESANS):
228                if files.endswith(".rst"):
229                    fromhere=os.path.join(SASMODELS_SOURCE_SESANS,files)
230                    tohere=os.path.join(SPHINX_SOURCE_USER,files)
231                    shutil.copy(fromhere,tohere)
232    else: print "no source directory",SASMODELS_SOURCE_SESANS," was found"
233
234    if os.path.exists(SASMODELS_SOURCE_MAGNETISM):
235        print "Found docs folder SASMODELS_SOURCE_MAGNETISM at ", SASMODELS_SOURCE_MAGNETISM
236        if os.path.exists(SASMODELS_DEST_REF_MODELS):
237            print "Found docs folder SASMODELS_DEST_REF_MODELS   at ", SASMODELS_DEST_REF_MODELS
238            print "Copying sasmodels model toctree files..."
239            for files in os.listdir(SASMODELS_SOURCE_MAGNETISM):
240                if files.endswith(".rst"):
241                    fromhere=os.path.join(SASMODELS_SOURCE_MAGNETISM,files)
242                    tohere=os.path.join(SASMODELS_DEST_REF_MODELS,files)
243                    shutil.copy(fromhere,tohere)
244    else: print "no source directory",SASMODELS_SOURCE_MAGNETISM," was found"
245
246    if os.path.exists(SASMODELS_SOURCE_MAGIMG):
247        print "Found img folder SASMODELS_SOURCE_MAGIMG   at ", SASMODELS_SOURCE_MAGIMG
248        if not os.path.exists(SASMODELS_DEST_MAGIMG):
249            print "Missing img folder SASMODELS_DEST_MAGIMG at ", SASMODELS_DEST_MAGIMG
250            os.makedirs(SASMODELS_DEST_MAGIMG)
251            print "created SASMODELS_DEST_MAGIMG at ", SASMODELS_DEST_MAGIMG
252        print "Copying sasmodels mag image files..."
253        for files in os.listdir(SASMODELS_SOURCE_MAGIMG):
254            fromhere=os.path.join(SASMODELS_SOURCE_MAGIMG,files)
255            tohere=os.path.join(SASMODELS_DEST_MAGIMG,files)
256            shutil.copy(fromhere,tohere)
257    else: print "no source directory",SASMODELS_SOURCE_MAGIMG ,"was found"
258
259    if os.path.exists(SASMODELS_SOURCE_SESANSIMG):
260        print "Found img folder SASMODELS_SOURCE_SESANSIMG   at ", SASMODELS_SOURCE_SESANSIMG
261        if not os.path.exists(SASMODELS_DEST_SESANSIMG):
262            print "Missing img folder SASMODELS_DEST_SESANSIMG at ", SASMODELS_DEST_SESANSIMG
263            os.makedirs(SASMODELS_DEST_SESANSIMG)
264            print "created SASMODELS_DEST_SESANSIMG at ", SASMODELS_DEST_SESANSIMG
265        print "Copying sasmodels sesans image files..."
266        for files in os.listdir(SASMODELS_SOURCE_SESANSIMG):
267            fromhere=os.path.join(SASMODELS_SOURCE_SESANSIMG,files)
268            tohere=os.path.join(SASMODELS_DEST_SESANSIMG,files)
269            shutil.copy(fromhere,tohere)
270    else: print "no source directory",SASMODELS_SOURCE_SESANSIMG ,"was found"
271
272    if os.path.exists(SASMODELS_SOURCE_REF_MODELS):
273        print "Found docs folder SASMODELS_SOURCE_REF_MODELS at ", SASMODELS_SOURCE_REF_MODELS
274        if os.path.exists(SASMODELS_DEST_REF_MODELS):
275            print "Found docs folder SASMODELS_DEST_REF_MODELS   at ", SASMODELS_DEST_REF_MODELS
276            print "Copying sasmodels model toctree files..."
277            for files in os.listdir(SASMODELS_SOURCE_REF_MODELS):
278                if files.endswith(".rst"):
279                    fromhere=os.path.join(SASMODELS_SOURCE_REF_MODELS,files)
280                    tohere=os.path.join(SASMODELS_DEST_REF_MODELS,files)
281                    shutil.copy(fromhere,tohere)
282            # But need to change the path to the model docs in the tocs
283            for files in os.listdir(SASMODELS_DEST_REF_MODELS):
284        #        print files
285                if files.startswith("shape"):
286                    print "Changing toc paths in", files
287                    inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
288                if files.startswith("sphere"):
289                    print "Changing toc paths in", files
290                    inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
291                if files.startswith("custom"):
292                    print "Changing toc paths in", files
293                    inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
294                if files.startswith("structure"):
295                    print "Changing toc paths in", files
296                    inplace_change(os.path.join(SASMODELS_DEST_REF_MODELS,files), "../../model/", "models/")
297    else: print "no source directory",SASMODELS_SOURCE_REF_MODELS," was found"
298
299    if os.path.exists(SASMODELS_SOURCE_MODELS):
300        print "Found docs folder SASMODELS_SOURCE_MODELS at ", SASMODELS_SOURCE_MODELS
301        if os.path.exists(SASMODELS_DEST_MODELS):
302            print "Found docs folder SASMODELS_DEST_MODELS   at ", SASMODELS_DEST_MODELS
303            print "Copying sasmodels model files..."
304            for files in os.listdir(SASMODELS_SOURCE_MODELS):
305                if files.endswith(".rst"):
306                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
307                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
308                    shutil.copy(fromhere,tohere)
309        else:
310            print "Missing docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS
311            os.makedirs(SASMODELS_DEST_MODELS)
312            if not os.path.exists(SASMODELS_DEST_BUILDIMG):
313                os.makedirs(SASMODELS_DEST_BUILDIMG)
314            print "Created docs folder SASMODELS_DEST_MODELS at ", SASMODELS_DEST_MODELS
315            print "Copying model files for build..."
316            for files in os.listdir(SASMODELS_SOURCE_MODELS):
317                if files.endswith(".rst"):
318                    fromhere=os.path.join(SASMODELS_SOURCE_MODELS,files)
319                    tohere=os.path.join(SASMODELS_DEST_MODELS,files)
320                    shutil.copy(fromhere,tohere)
321            # No choice but to do this because model files are all coded for images in /models/img
322            print "Copying image files for build..."
323            for files in os.listdir(SASMODELS_DEST_IMG):
324                fromhere=os.path.join(SASMODELS_DEST_IMG,files)
325                tohere=os.path.join(SASMODELS_DEST_BUILDIMG,files)
326                shutil.copy(fromhere,tohere)
327    else: print "no source directory",SASMODELS_SOURCE_MODELS," was found.\n !!!!NO MODEL DOCS WILL BE BUILT!!!!"
328
329
330def retrieve_bumps_docs():
331    """
332    Copies select files from the bumps documentation into fitting perspective
333    """
334    if os.path.exists(BUMPS_DOCS):
335        print "=== Retrieve BUMPS Docs ==="
336        filenames = [os.path.join(BUMPS_DOCS, "optimizer.rst")]
337        filenames += glob(os.path.join(BUMPS_DOCS, "dream-*.png"))
338        filenames += glob(os.path.join(BUMPS_DOCS, "fit-*.png"))
339        for f in filenames:
340            print "Copying file", f
341            shutil.copy(f, BUMPS_TARGET)
342    else:
343        print """
344*** Error *** missing directory %s
345The documentation will not include the optimizer selection section.
346Checkout the bumps source tree and rebuild the docs.
347
348
349""" % BUMPS_DOCS
350
351def apidoc():
352    """
353    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
354    in the SasView build directory.
355    """
356    print "=== Generate API Rest Files ==="
357
358    # Clean directory before generating a new version.
359    _remove_dir(SPHINX_SOURCE_API)
360
361    subprocess.call(["sphinx-apidoc",
362                     "-o", SPHINX_SOURCE_API, # Output dir.
363                     "-d", "8", # Max depth of TOC.
364                     SASVIEW_BUILD])
365
366def build():
367    """
368    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
369    """
370    print "=== Build HTML Docs from Rest Files ==="
371    subprocess.call(["sphinx-build",
372                     "-b", "html", # Builder name. TODO: accept as arg to setup.py.
373                     "-d", os.path.join(SPHINX_BUILD, "doctrees"),
374                     SPHINX_SOURCE,
375                     os.path.join(SPHINX_BUILD, "html")])
376
377    print "=== Copy HTML Docs to Build Directory ==="
378    html = os.path.join(SPHINX_BUILD, "html")
379    copy_tree(html, SASVIEW_DOCS)
380
381def rebuild():
382    clean()
383    setup_source_temp()
384    retrieve_user_docs()
385    retrieve_bumps_docs()
386    apidoc()
387    build()
388
389    print "=== Done ==="
390
391if __name__ == "__main__":
392    rebuild()
Note: See TracBrowser for help on using the repository browser.