/
sphinx_pdf.py
58 lines (48 loc) · 2.14 KB
/
sphinx_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from glob import glob
import re
import os
from django.conf import settings
from doc_builder.base import BaseBuilder, restoring_chdir
from projects.utils import run
from core.utils import copy_file_to_app_servers
latex_re = re.compile('the LaTeX files are in (.*)\.')
pdf_re = re.compile('Output written on (.+) \(')
class Builder(BaseBuilder):
@restoring_chdir
def build(self):
project = self.version.project
os.chdir(project.conf_dir(self.version.slug))
if project.use_virtualenv:
latex_results = run('%s -b latex -d _build/doctrees . _build/latex' %
project.venv_bin(version=self.version.slug, bin='sphinx-build'))
else:
latex_results = run('sphinx-build -b latex '
'-d _build/doctrees . _build/latex')
if latex_results[0] == 0:
os.chdir('_build/latex')
tex_globs = glob('*.tex')
if tex_globs:
tex_file = tex_globs[0]
pdf_results = run('pdflatex -interaction=nonstopmode %s' % tex_file)
pdf_match = pdf_re.search(pdf_results[1])
if pdf_match:
to_path = os.path.join(settings.MEDIA_ROOT,
'pdf',
project.slug,
self.version.slug)
from_file = os.path.join(os.getcwd(), "*.pdf")
to_file = os.path.join(to_path, '%s.pdf' % project.slug)
if getattr(settings, "MULTIPLE_APP_SERVERS", None):
copy_file_to_app_servers(from_file, to_file)
else:
if not os.path.exists(to_path):
os.makedirs(to_path)
run('mv -f %s %s' % (from_file, to_file))
else:
pdf_results = (1, '', '')
print "PDF Building failed. Moving on."
return (latex_results, pdf_results)
def move(self):
#This needs to be thought about more because of all the state above.
#We could just shove the filename on the instance or something.
return True