source: sasview/docs/sphinx-docs/build_sphinx.py @ 7bcbeb7

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 7bcbeb7 was 354524d, checked in by smk78, 9 years ago

Now copying sasmodels rst files to sasview and building

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