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