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

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 a40a875 was a40a875, checked in by Peter Parker, 10 years ago

Refs #202 - Do sphinx via python rather than make.

  • Property mode set to 100644
File size: 3.0 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
12
13from distutils.dir_util import copy_tree
14from distutils.util import get_platform
15
16platform = '.%s-%s'%(get_platform(),sys.version[:3])
17
18CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
19
20SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src")
21SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform))
22
23SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
24SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source")
25SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev","api")
26
27def _remove_dir(dir_path):
28    """Removes the given directory."""
29    if os.path.isdir(dir_path):
30        print "Removing \"%s\"... " % dir_path
31        shutil.rmtree(dir_path)
32
33def clean():
34    """
35    Clean the sphinx build directory.
36    """
37    print "=== Cleaning Sphinx Build ==="
38    _remove_dir(SPHINX_BUILD)
39
40def retrieve_user_docs():
41    """
42    Copies across the contents of any docs/ directories in src/, and puts them
43    in an appropriately named directory of docs/sphinx-docs/source/. For
44    example:
45
46    sans/../[MODULE]/docs/A.rst
47    sans/../[MODULE]/docs/B.rst
48
49    gets copied to a new location:
50
51    docs/sphinx-docs/source/user/[MODULE]/A.rst
52    docs/sphinx-docs/source/user/[MODULE]/B.rst
53
54    so that Sphinx may pick it up when generating the documentation.
55    """
56    print "=== Retrieve User Docs ==="
57
58    # Copy documentation files from their "source" to their "destination".
59    for root, dirnames, _ in os.walk(SASVIEW_SRC):
60        for dirname in fnmatch.filter(dirnames, 'docs'):
61
62            docs = os.path.abspath(os.path.join(root, dirname))
63            print "Found docs folder at \"%s\"." % docs
64
65            dest_dir_name = os.path.basename(os.path.dirname(docs))
66            dest_dir = os.path.join(SPHINX_SOURCE, "user", dest_dir_name)
67
68            copy_tree(docs, dest_dir, update=1)
69
70def apidoc():
71    """
72    Runs sphinx-apidoc to generate .rst files from the docstrings in .py files
73    in the SasView build directory.
74    """
75    print "=== Generate API Rest Files ==="
76
77    # Clean directory before generating a new version.
78    _remove_dir(SPHINX_SOURCE_API)
79
80    subprocess.call(["sphinx-apidoc",
81                     "-o", SPHINX_SOURCE_API, # Output dir.
82                     "-d", "8", # Max depth of TOC.
83                     SASVIEW_BUILD])
84
85def build():
86    """
87    Runs sphinx-build.  Reads in all .rst files and spits out the final html.
88    """
89    print "=== Build HTML from Rest Files ==="
90    subprocess.call(["sphinx-build",
91                     "-b", "html", # Builder name.
92                     "-d", os.path.join(SPHINX_BUILD, "doctrees"), # Cache directory (doctrees).
93                     SPHINX_SOURCE,
94                     os.path.join(SPHINX_BUILD, "html")])
95
96if __name__ == "__main__":
97    call()
Note: See TracBrowser for help on using the repository browser.