[9bf64f6] | 1 | #!/usr/bin/env python |
---|
[35bf493] | 2 | """ |
---|
| 3 | Functions for building sphinx docs. |
---|
| 4 | |
---|
| 5 | For more information on the invocation of sphinx see: |
---|
| 6 | http://sphinx-doc.org/invocation.html |
---|
| 7 | """ |
---|
| 8 | import subprocess |
---|
| 9 | import os |
---|
| 10 | import sys |
---|
| 11 | import fnmatch |
---|
| 12 | import shutil |
---|
[296f290] | 13 | import imp |
---|
[c22c5e3] | 14 | from glob import glob |
---|
[35bf493] | 15 | |
---|
| 16 | from distutils.dir_util import copy_tree |
---|
| 17 | from distutils.util import get_platform |
---|
[d2143ab] | 18 | from shutil import copy |
---|
[b7a2ebfd] | 19 | from os import listdir |
---|
[35bf493] | 20 | |
---|
| 21 | platform = '.%s-%s'%(get_platform(),sys.version[:3]) |
---|
| 22 | |
---|
| 23 | CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
---|
| 24 | |
---|
[296f290] | 25 | run = imp.load_source('run', os.path.join(CURRENT_SCRIPT_DIR, '..', '..', 'run.py')) |
---|
| 26 | run.prepare() |
---|
| 27 | |
---|
[35bf493] | 28 | SASVIEW_SRC = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src") |
---|
| 29 | SASVIEW_BUILD = os.path.abspath(os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "build", "lib"+platform)) |
---|
| 30 | SASVIEW_DOCS = os.path.join(SASVIEW_BUILD, "doc") |
---|
[2fb09c4] | 31 | SASVIEW_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] | 37 | SASMODELS_SOURCE_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model") |
---|
| 38 | SASMODELS_SOURCE_IMG = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", "sasmodels", "doc", "model", "img") |
---|
| 39 | SASMODELS_DEST_MODELS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "src", "sas", "models", "media") |
---|
[b7a2ebfd] | 40 | SASMODELS_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] | 51 | SPHINX_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build") |
---|
| 52 | SPHINX_SOURCE = os.path.join(CURRENT_SCRIPT_DIR, "source") |
---|
| 53 | SPHINX_SOURCE_API = os.path.join(SPHINX_SOURCE, "dev", "api") |
---|
[ba31ed1] | 54 | SPHINX_SOURCE_GUIFRAME = os.path.join(SPHINX_SOURCE, "user", "sasgui", "guiframe") |
---|
[7168b8b] | 55 | SPHINX_SOURCE_MODELS = os.path.join(SPHINX_SOURCE, "user", "models") |
---|
[ba31ed1] | 56 | SPHINX_SOURCE_PERSPECTIVES = os.path.join(SPHINX_SOURCE, "user", "sasgui", "perspectives") |
---|
[2fb09c4] | 57 | SPHINX_SOURCE_TEST = os.path.join(SPHINX_SOURCE, "test") |
---|
[35bf493] | 58 | |
---|
[c22c5e3] | 59 | BUMPS_DOCS = os.path.join(CURRENT_SCRIPT_DIR, "..", "..", "..", |
---|
| 60 | "bumps", "doc", "guide") |
---|
| 61 | BUMPS_TARGET = os.path.join(SPHINX_SOURCE_PERSPECTIVES, "fitting") |
---|
| 62 | |
---|
[35bf493] | 63 | def _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 | |
---|
| 69 | def 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] | 82 | def 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] | 145 | def 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 |
---|
| 160 | The documentation will not include the optimizer selection section. |
---|
| 161 | Checkout the bumps source tree and rebuild the docs. |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | """ % BUMPS_DOCS |
---|
| 165 | |
---|
[35bf493] | 166 | def 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 | |
---|
| 181 | def 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] | 196 | def rebuild(): |
---|
[35bf493] | 197 | clean() |
---|
| 198 | retrieve_user_docs() |
---|
[c22c5e3] | 199 | retrieve_bumps_docs() |
---|
[35bf493] | 200 | apidoc() |
---|
| 201 | build() |
---|
[c2ee2b1] | 202 | |
---|
| 203 | print "=== Done ===" |
---|
| 204 | |
---|
| 205 | if __name__ == "__main__": |
---|
[9bf64f6] | 206 | rebuild() |
---|