source: sasview/docs/sphinx-docs/build_sphinx.py @ 296f290

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 296f290 was 296f290, checked in by pkienzle, 9 years ago

use bumps/periodictable source directory to build docs if they are not installed

  • Property mode set to 100644
File size: 3.5 KB
Line 
1"""
2Functions for building sphinx docs.
3
4For more information on the invocation of sphinx see:
5http://sphinx-doc.org/invocation.html
6"""
7import subprocess
8import os
9import sys
10import fnmatch
11import shutil
12import imp
13
14from distutils.dir_util import copy_tree
15from distutils.util import get_platform
16
17platform = '.%s-%s'%(get_platform(),sys.version[:3])
18
19CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
20
21run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py'))
22run.prepare()
23
24SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src")
25SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform))
26SASVIEW_DOCS = os.path.join(SASVIEW_BUILD, "doc")
27
28SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
29SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source")
30SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api")
31
32def _remove_dir(dir_path):
33    """Removes the given directory."""
34    if os.path.isdir(dir_path):
35        print "Removing \"%s\"... " % dir_path
36        shutil.rmtree(dir_path)
37
38def clean():
39    """
40    Clean the sphinx build directory.
41    """
42    print "=== Cleaning Sphinx Build ==="
43    _remove_dir(SASVIEW_DOCS)
44    _remove_dir(SPHINX_BUILD)
45
46def retrieve_user_docs():
47    """
48    Copies across the contents of any media/ directories in src/, and puts them
49    in an appropriately named directory of docs/sphinx-docs/source/. For
50    example:
51
52        sas/../[MODULE]/media/dir/A.rst
53        sas/../[MODULE]/media/B.rst
54
55    gets copied to a new location:
56
57        docs/sphinx-docs/source/user/[MODULE]/dir/A.rst
58        docs/sphinx-docs/source/user/[MODULE]/B.rst
59
60    so that Sphinx may pick it up when generating the documentation.
61    """
62    print "=== Retrieve User Docs ==="
63
64    # Copy documentation files from their "source" to their "destination".
65    for root, dirnames, _ in os.walk(SASVIEW_SRC):
66        for dirname in fnmatch.filter(dirnames, 'media'):
67
68            docs = os.path.abspath(os.path.join(root, dirname))
69            print "Found docs folder at \"%s\"." % docs
70
71            dest_dir_part = os.path.dirname(os.path.relpath(docs, SASVIEW_SRC))
72            if os.sep in dest_dir_part:
73                dest_dir_part = dest_dir_part[dest_dir_part.index(os.sep) + 1:]
74            dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_part)
75
76            copy_tree(docs, dest_dir)
77
78def apidoc():
79    """
80    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
81    in the SasView build directory.
82    """
83    print "=== Generate API Rest Files ==="
84
85    # Clean directory before generating a new version.
86    _remove_dir(SPHINX_SOURCE_API)
87
88    subprocess.call(["sphinx-apidoc",
89                     "-o", SPHINX_SOURCE_API, # Output dir.
90                     "-d", "8", # Max depth of TOC.
91                     SASVIEW_BUILD])
92
93def build():
94    """
95    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
96    """
97    print "=== Build HTML Docs from Rest Files ==="
98    subprocess.call(["sphinx-build",
99                     "-b", "html", # Builder name. TODO: accept as arg to setup.py.
100                     "-d", os.path.join(SPHINX_BUILD, "doctrees"),
101                     SPHINX_SOURCE,
102                     os.path.join(SPHINX_BUILD, "html")])
103
104    print "=== Copy HTML Docs to Build Directory ==="
105    html = os.path.join(SPHINX_BUILD, "html")
106    copy_tree(html, SASVIEW_DOCS)
107
108if __name__ == "__main__":
109    clean()
110    retrieve_user_docs()
111    apidoc()
112    build()
113       
114    print "=== Done ==="
Note: See TracBrowser for help on using the repository browser.