| 1 |
"""helper for quick cross-platform makefile for sphinx |
| 2 |
|
| 3 |
TODO: this was hacked up really quickly, could use lots of work. |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import print_function |
| 10 |
import logging; log = logging.getLogger(__name__) |
| 11 |
import os,sys |
| 12 |
from string import Template |
| 13 |
import subprocess |
| 14 |
|
| 15 |
|
| 16 |
__all__ = [ |
| 17 |
"SphinxMaker", |
| 18 |
] |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
def sub(fmt, **kwds): |
| 24 |
if not kwds: |
| 25 |
kwds = globals() |
| 26 |
return Template(fmt).substitute(**kwds) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
joinpath = os.path.join |
| 32 |
|
| 33 |
def abspath(*args): |
| 34 |
return os.path.abspath(joinpath(*args)) |
| 35 |
|
| 36 |
if hasattr(os.path, "realpath"): |
| 37 |
def realpath(*args): |
| 38 |
return os.path.realpath(joinpath(*args)) |
| 39 |
else: |
| 40 |
|
| 41 |
def realpath(*args): |
| 42 |
return os.path.normcase(os.path.abspath(joinpath(*args))) |
| 43 |
|
| 44 |
def pathdir(path): |
| 45 |
return os.path.split(path)[0] |
| 46 |
|
| 47 |
def clearpath(path): |
| 48 |
"recursively remove all contents of dir, but leave dir" |
| 49 |
for root, dirs, files in os.walk(path, topdown=False): |
| 50 |
for name in files: |
| 51 |
os.remove(joinpath(root, name)) |
| 52 |
for name in dirs: |
| 53 |
os.rmdir(joinpath(root, name)) |
| 54 |
|
| 55 |
def rmpath(path): |
| 56 |
"drecursively delete path" |
| 57 |
if os.path.exists(path): |
| 58 |
if os.path.isdir(path): |
| 59 |
clearpath(path) |
| 60 |
os.rmdir(path) |
| 61 |
else: |
| 62 |
os.remove(path) |
| 63 |
|
| 64 |
def ensuredirs(path): |
| 65 |
"ensure specified directory & all parents exist" |
| 66 |
if not os.path.isdir(path): |
| 67 |
os.makedirs(path) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
class SphinxMaker(object): |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
SPHINXOPTS = [] |
| 79 |
SPHINXBUILD = "sphinx-build" |
| 80 |
PAPER = "letter" |
| 81 |
SERVEHTML_PORT = 8000 |
| 82 |
|
| 83 |
|
| 84 |
BUILD = "_build" |
| 85 |
SOURCE = "." |
| 86 |
|
| 87 |
|
| 88 |
PAPEROPT_a4 = ["-D","latex_paper_size=a4"] |
| 89 |
PAPEROPT_letter = ["-D","latex_paper_size=letter"] |
| 90 |
|
| 91 |
|
| 92 |
env_vars = [ "SPHINXOPTS", "SPHINXBUILD", "PAPER", "SERVEHTML_PORT", "BUILD", "SOURCE" ] |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
root_dir = None |
| 98 |
conf_file = None |
| 99 |
conf = None |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
def __init__(self, root_dir=None, **kwds): |
| 105 |
|
| 106 |
if root_dir is None: |
| 107 |
root_dir = joinpath(sys.modules["__main__"].__file__, os.pardir) |
| 108 |
self.root_dir = abspath(root_dir) |
| 109 |
self.conf_file = joinpath(self.root_dir, "conf.py") |
| 110 |
if not os.path.exists(self.conf_file): |
| 111 |
raise RuntimeError("conf file not found in root: %r" % |
| 112 |
(self.root_dir)) |
| 113 |
|
| 114 |
|
| 115 |
for key in self.env_vars: |
| 116 |
value = kwds.pop(key, None) |
| 117 |
value = os.environ.get(key, value) |
| 118 |
if value is not None: |
| 119 |
t = type(getattr(self,key)) |
| 120 |
|
| 121 |
if isinstance(t, str): |
| 122 |
if isinstance(t, int): |
| 123 |
value = int(t) |
| 124 |
elif isinstance(t, list): |
| 125 |
|
| 126 |
value = " ".split(value) |
| 127 |
setattr(self, key, value) |
| 128 |
|
| 129 |
|
| 130 |
for name in ("BUILD", "SOURCE"): |
| 131 |
value = getattr(self, name) |
| 132 |
if not os.path.isabs(value): |
| 133 |
value = abspath(self.root_dir, value) |
| 134 |
setattr(self, name, value) |
| 135 |
|
| 136 |
if kwds: |
| 137 |
raise TypeError("unknown keywords: %r" % (kwds,)) |
| 138 |
|
| 139 |
@classmethod |
| 140 |
def execute(cls, args=None, **kwds): |
| 141 |
return cls(**kwds).run(args) |
| 142 |
|
| 143 |
def run(self, args=None): |
| 144 |
if args is None: |
| 145 |
args = sys.argv[1:] |
| 146 |
os.chdir(self.root_dir) |
| 147 |
for arg in args: |
| 148 |
getattr(self,"target_"+arg)() |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
def target_help(self): |
| •154 |
print("Please use 'make <target>' where <target> is one of") |
| 155 |
print(" clean remove all compiled files") |
| 156 |
print(" html to make standalone HTML files") |
| 157 |
print(" servehtml to serve standalone HTML files on port 8000") |
| 158 |
|
| 159 |
|
| 160 |
print(" htmlhelp to make HTML files and a HTML help project") |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
def target_clean(self): |
| 166 |
rmpath(self.BUILD) |
| 167 |
|
| 168 |
def target_html(self): |
| 169 |
self.build("html") |
| 170 |
|
| 171 |
def target_htmlhelp(self): |
| 172 |
self.build("htmlhelp") |
| 173 |
|
| 174 |
def target_servehtml(self): |
| 175 |
path = realpath(self.BUILD, "html") |
| 176 |
port = self.SERVEHTML_PORT |
| 177 |
|
| 178 |
|
| 179 |
try: |
| 180 |
from paste.httpserver import serve |
| 181 |
from paste.urlparser import StaticURLParser |
| 182 |
except ImportError: |
| 183 |
|
| 184 |
if sys.version_info[0] >= 3: |
| 185 |
import http.server as s |
| 186 |
HTTPServer = s.HTTPServer |
| 187 |
else: |
| 188 |
import SimpleHTTPServer as s |
| 189 |
HTTPServer = s.BaseHTTPServer.HTTPServer |
| 190 |
|
| 191 |
os.chdir(path) |
| 192 |
print("Serving files from %r on port %r" % (path, port)) |
| 193 |
|
| 194 |
HTTPServer(('',port), s.SimpleHTTPRequestHandler).serve_forever() |
| 195 |
else: |
| 196 |
serve(StaticURLParser(path), host="0.0.0.0", port=port) |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
def build(self, name): |
| 217 |
BUILD = self.BUILD |
| 218 |
ALLSPHINXOPTS = self.get_sphinx_opts() |
| 219 |
|
| 220 |
dt = joinpath(BUILD, "doctrees") |
| 221 |
ensuredirs(dt) |
| 222 |
|
| 223 |
target = joinpath(BUILD, name) |
| 224 |
ensuredirs(target) |
| 225 |
|
| 226 |
rc = subprocess.call([self.SPHINXBUILD, "-b", name] + ALLSPHINXOPTS + [ target ]) |
| 227 |
if rc: |
| 228 |
print("Sphinx-Build returned error, exiting.") |
| 229 |
sys.exit(rc) |
| 230 |
print("Build finished. The %s pages are in %r." % (name, target,)) |
| 231 |
return target |
| 232 |
|
| 233 |
def get_paper_opts(self): |
| 234 |
return getattr(self,"PAPER_" + self.PAPER, []) |
| 235 |
|
| 236 |
def get_sphinx_opts(self): |
| 237 |
return ["-d", joinpath(self.BUILD, "doctrees")] + self.get_paper_opts() + self.SPHINXOPTS + [ self.SOURCE ] |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
class ProjectSphinxMaker(SphinxMaker): |
| 244 |
"SphinxMaker variant which more usefully integrates into setup.py of a python project" |
| 245 |
|
| 246 |
BUILD = os.path.join(os.pardir, "build", "sphinx") |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|