| 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 |
#imports |
| 7 |
#=============================================================== |
| 8 |
#core |
| 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 |
#pkg |
| 15 |
#local |
| 16 |
__all__ = [ |
| 17 |
"SphinxMaker", |
| 18 |
] |
| 19 |
|
| 20 |
#=============================================================== |
| 21 |
#misc helpers |
| 22 |
#=============================================================== |
| 23 |
def sub(fmt, **kwds): |
| 24 |
if not kwds: |
| 25 |
kwds = globals() |
| 26 |
return Template(fmt).substitute(**kwds) |
| 27 |
|
| 28 |
#=============================================================== |
| 29 |
#fs helpers |
| 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 |
#probably windows - fake it best we can |
| 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 |
#main class |
| 71 |
#=============================================================== |
| 72 |
class SphinxMaker(object): |
| 73 |
#=============================================================== |
| 74 |
#class attrs |
| 75 |
#=============================================================== |
| 76 |
|
| 77 |
# You can subclass these variables |
| 78 |
SPHINXOPTS = [] |
| 79 |
SPHINXBUILD = "sphinx-build" |
| 80 |
PAPER = "letter" |
| 81 |
SERVEHTML_PORT = 8000 |
| 82 |
|
| 83 |
# Paths |
| 84 |
BUILD = "_build" |
| 85 |
SOURCE = "." |
| 86 |
|
| 87 |
#internal opts |
| 88 |
PAPEROPT_a4 = ["-D","latex_paper_size=a4"] |
| 89 |
PAPEROPT_letter = ["-D","latex_paper_size=letter"] |
| 90 |
|
| 91 |
#: list of attrs to check os.environ for overriddes. |
| 92 |
env_vars = [ "SPHINXOPTS", "SPHINXBUILD", "PAPER", "SERVEHTML_PORT", "BUILD", "SOURCE" ] |
| 93 |
|
| 94 |
#=============================================================== |
| 95 |
#instance attrs |
| 96 |
#=============================================================== |
| 97 |
root_dir = None |
| 98 |
conf_file = None |
| 99 |
conf = None |
| 100 |
|
| 101 |
#=============================================================== |
| 102 |
#frontend |
| 103 |
#=============================================================== |
| 104 |
def __init__(self, root_dir=None, **kwds): |
| 105 |
#FIXME: this may not be properly flexible. |
| 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 |
#check environment for overrides, as well as constructor |
| 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 |
#FIXME: this is *real* hacked way to do type conversion |
| 121 |
if isinstance(t, str): |
| 122 |
if isinstance(t, int): #for ints, eg SERVEHTML_PORT |
| 123 |
value = int(t) |
| 124 |
elif isinstance(t, list): #for list of arguments, eg SPHINXOPTS |
| 125 |
#FIXME: should use proper quote escaping logic when we split :( |
| 126 |
value = " ".split(value) |
| 127 |
setattr(self, key, value) |
| 128 |
|
| 129 |
# make all relative paths relative to root dir |
| 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) #due to relative paths like self.BUILD |
| 147 |
for arg in args: |
| 148 |
getattr(self,"target_"+arg)() |
| 149 |
|
| 150 |
#=============================================================== |
| 151 |
#targets |
| 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 |
# print(" pickle to make pickle files") |
| 159 |
# print(" json to make JSON files") |
| 160 |
print(" htmlhelp to make HTML files and a HTML help project") |
| 161 |
# print(" latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter") |
| 162 |
# print(" changes to make an overview over all changed/added/deprecated items") |
| 163 |
# print(" linkcheck to check all external links for integrity") |
| 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 |
# try to use paste |
| 179 |
try: |
| 180 |
from paste.httpserver import serve |
| 181 |
from paste.urlparser import StaticURLParser |
| 182 |
except ImportError: |
| 183 |
# fall back to stdlib server |
| 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 |
#TODO: support latex, pdf, etc... |
| 199 |
|
| 200 |
##def target_latex(self): |
| 201 |
## build("latex") |
| 202 |
## print("Run \`make all-pdf' or \`make all-ps' in that directory to" \ |
| 203 |
## "run these through (pdf)latex.") |
| 204 |
## |
| 205 |
##def target_pdf(): |
| 206 |
## assert os.name == "posix", "pdf build support not automated for your os" |
| 207 |
## build("latex") |
| 208 |
## target = BUILD / "latex" |
| 209 |
## target.chdir() |
| 210 |
## subprocess.call(['make', 'all-pdf']) |
| 211 |
## print("pdf built") |
| 212 |
|
| 213 |
#=============================================================== |
| 214 |
#helpers |
| 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 |
#eoc |
| 241 |
#=============================================================== |
| 242 |
|
| 243 |
class ProjectSphinxMaker(SphinxMaker): |
| 244 |
"SphinxMaker variant which more usefully integrates into setup.py of a python project" |
| 245 |
#TODO: make this read setup.cfg etc to see where build_sphinx *actually* puts things |
| 246 |
BUILD = os.path.join(os.pardir, "build", "sphinx") |
| 247 |
|
| 248 |
#=============================================================== |
| 249 |
#eof |
| 250 |
#=============================================================== |