Skip to content

Commit

Permalink
add decorator to restore cwd after running build commands, use safe_w…
Browse files Browse the repository at this point in the history
…rite, remove unused try/except, some pep8 changes.
  • Loading branch information
dcolish committed Jun 19, 2011
1 parent ac95921 commit a251351
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
44 changes: 20 additions & 24 deletions readthedocs/doc_builder/backends/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings

from doc_builder.base import BaseBuilder
from doc_builder.base import BaseBuilder, restoring_chdir
from projects.utils import safe_write, run


Expand Down Expand Up @@ -59,38 +59,32 @@
TEMPLATE_DIR = '%s/readthedocs/templates/sphinx' % settings.SITE_ROOT
STATIC_DIR = '%s/_static' % TEMPLATE_DIR


class Builder(BaseBuilder):

def _whitelisted(self, version):
"""Modify the given ``conf.py`` file from a whitelisted user's project.
"""
project = version.project
#Open file for appending.
try:
outfile = open(project.conf_file(version.slug), 'a')
except IndexError:
print "Nothing to white list, no conf.py"
return
outfile.write("\n")
rtd_ctx = Context({
'verisons': project.active_versions(),
'current_version': version,
'project': project,
'settings': settings,
'static_path': STATIC_DIR,
'template_path': TEMPLATE_DIR,
})
'verisons': project.active_versions(),
'current_version': version,
'project': project,
'settings': settings,
'static_path': STATIC_DIR,
'template_path': TEMPLATE_DIR,
})
rtd_string = Template(RTD_CONF_ADDITIONS).render(rtd_ctx)
outfile.write(rtd_string)
outfile.close()
safe_write(project.conf_file(version.slug), rtd_string)

def _sanitize(self, version):
project = version.project
conf_template = render_to_string('sphinx/conf.py.conf',
{'project': project,
'template_dir': TEMPLATE_DIR,
'badge': project.sponsored
})
conf_template = render_to_string('sphinx/conf.py.conf',
{'project': project,
'template_dir': TEMPLATE_DIR,
'badge': project.sponsored
})
rtd_ctx = Context({
'verisons': project.active_versions(),
'current_version': version,
Expand All @@ -117,13 +111,15 @@ def clean(self, version):
self._sanitize(version)
except (OSError, IOError):
print "Conf file not found. Error writing to disk."
return ('','Conf file not found. Error writing to disk.',-1)
return ('', 'Conf file not found. Error writing to disk.', -1)

@restoring_chdir
def build(self, version):
project = version.project
os.chdir(version.project.conf_dir(version.slug))
os.chdir(project.conf_dir(version.slug))
if project.use_virtualenv and project.whitelisted:
build_command = '%s -b html . _build/html' % project.venv_bin(version=version.slug, bin='sphinx-build')
build_command = '%s -b html . _build/html' % project.venv_bin(
version=version.slug, bin='sphinx-build')
else:
build_command = "sphinx-build -b html . _build/html"
build_results = run(build_command)
Expand Down
6 changes: 5 additions & 1 deletion readthedocs/doc_builder/backends/sphinx_htmldir.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import os
from doc_builder.base import restoring_chdir
from doc_builder.backends.sphinx import Builder as HtmlBuilder
from projects.utils import run


class Builder(HtmlBuilder):

@restoring_chdir
def build(self, version):
project = version.project
os.chdir(version.project.conf_dir(version.slug))
if project.use_virtualenv and project.whitelisted:
build_command = '%s -b dirhtml . _build/html' % project.venv_bin(version=version.slug, bin='sphinx-build')
build_command = '%s -b dirhtml . _build/html' % project.venv_bin(
version=version.slug, bin='sphinx-build')
else:
build_command = "sphinx-build -b dirhtml . _build/html"
build_results = run(build_command)
Expand Down
3 changes: 3 additions & 0 deletions readthedocs/doc_builder/backends/sphinx_man.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from django.conf import settings

from doc_builder.base import restoring_chdir
from doc_builder.backends.sphinx import Builder as ManpageBuilder
from projects.tasks import copy_to_app_servers
from projects.utils import run


class Builder(ManpageBuilder):

@restoring_chdir
def build(self, version):
project = version.project
os.chdir(version.project.conf_dir(version.slug))
Expand Down Expand Up @@ -38,6 +40,7 @@ def build(self, version):
run('mv -f %s %s' % (from_file, to_file))
else:
print "File doesn't exist, not symlinking."
return False
return True
else:
return False
3 changes: 2 additions & 1 deletion readthedocs/doc_builder/backends/sphinx_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.conf import settings

from doc_builder.base import BaseBuilder
from doc_builder.base import BaseBuilder, restoring_chdir
from projects.utils import run
from projects.tasks import copy_to_app_servers

Expand All @@ -14,6 +14,7 @@

class Builder(BaseBuilder):

@restoring_chdir
def build(self, version):
project = version.project
os.chdir(project.conf_dir(version.slug))
Expand Down
12 changes: 12 additions & 0 deletions readthedocs/doc_builder/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import os


def restoring_chdir(fn):
def decorator(*args, **kw):
try:
path = os.getcwd()
return fn(*args, **kw)
finally:
os.chdir(path)
return decorator


class BaseBuilder(object):

@restoring_chdir
def touch(self, version):
print "Touching files"
os.chdir(version.project.conf_dir(version.slug))
Expand Down

0 comments on commit a251351

Please sign in to comment.