refactoring by using with cd

This commit is contained in:
Takayuki Shimizukawa 2014-09-26 01:46:29 +09:00
parent 636c14ef4a
commit 0a25e61eff
4 changed files with 24 additions and 31 deletions

View File

@ -8,15 +8,15 @@
:copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import with_statement
import os
import re
import sys
from os import path
from sphinx.errors import ConfigError
from sphinx.locale import l_
from sphinx.util.osutil import make_filename
from sphinx.util.osutil import make_filename, cd
from sphinx.util.pycompat import bytes, b, execfile_
nonascii_re = re.compile(b(r'[\x80-\xff]'))
@ -220,17 +220,13 @@ class Config(object):
config_file = path.join(dirname, filename)
config['__file__'] = config_file
config['tags'] = tags
olddir = os.getcwd()
try:
with cd(dirname):
# we promise to have the config dir as current dir while the
# config file is executed
os.chdir(dirname)
try:
execfile_(filename, config)
except SyntaxError, err:
raise ConfigError(CONFIG_SYNTAX_ERROR % err)
finally:
os.chdir(olddir)
self._raw_config = config
# these two must be preinitialized because extensions can add their

View File

@ -8,13 +8,14 @@
:copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import with_statement
import re
import codecs
import shutil
import tempfile
import posixpath
from os import path, getcwd, chdir
from os import path
from subprocess import Popen, PIPE
try:
from hashlib import sha1 as sha
@ -25,7 +26,7 @@ from docutils import nodes
from sphinx.errors import SphinxError
from sphinx.util.png import read_png_depth, write_png_depth
from sphinx.util.osutil import ensuredir, ENOENT
from sphinx.util.osutil import ensuredir, ENOENT, cd
from sphinx.util.pycompat import b, sys_encoding
from sphinx.ext.mathbase import setup_math as mathbase_setup, wrap_displaymath
@ -117,10 +118,7 @@ def render_math(self, math):
ltx_args.extend(self.builder.config.pngmath_latex_args)
ltx_args.append('math.tex')
curdir = getcwd()
chdir(tempdir)
try:
with cd(tempdir):
try:
p = Popen(ltx_args, stdout=PIPE, stderr=PIPE)
except OSError, err:
@ -131,8 +129,6 @@ def render_math(self, math):
self.builder.config.pngmath_latex)
self.builder._mathpng_warned_latex = True
return None, None
finally:
chdir(curdir)
stdout, stderr = p.communicate()
if p.returncode != 0:

View File

@ -14,6 +14,7 @@
:copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import with_statement
import os
import sys
@ -23,7 +24,7 @@ from subprocess import call
import sphinx
from sphinx.util.console import bold, blue
from sphinx.util.pycompat import getcwd
from sphinx.util.osutil import cd
proj_name = os.getenv('SPHINXPROJ', '<project>')
@ -160,22 +161,14 @@ class Make(object):
def build_latexpdf(self):
if self.run_generic_build('latex') > 0:
return 1
cwd = getcwd()
try:
os.chdir(self.builddir_join('latex'))
with cd(self.builddir_join('latex')):
os.system('make all-pdf')
finally:
os.chdir(cwd)
def build_latexpdfja(self):
if self.run_generic_build('latex') > 0:
return 1
cwd = getcwd()
try:
os.chdir(self.builddir_join('latex'))
with cd(self.builddir_join('latex')):
os.system('make all-pdf-ja')
finally:
os.chdir(cwd)
def build_text(self):
if self.run_generic_build('text') > 0:
@ -195,12 +188,8 @@ class Make(object):
def build_info(self):
if self.run_generic_build('texinfo') > 0:
return 1
cwd = getcwd()
try:
os.chdir(self.builddir_join('texinfo'))
with cd(self.builddir_join('texinfo')):
os.system('make info')
finally:
os.chdir(cwd)
def build_gettext(self):
dtdir = self.builddir_join('gettext', '.doctrees')

View File

@ -18,6 +18,7 @@ import locale
import shutil
import gettext
from os import path
import contextlib
# Errnos that we need.
EEXIST = getattr(errno, 'EEXIST', 0)
@ -196,3 +197,14 @@ def abspath(pathdir):
if isinstance(pathdir, bytes):
pathdir = pathdir.decode(fs_encoding)
return pathdir
@contextlib.contextmanager
def cd(target_dir):
from sphinx.util.pycompat import getcwd
cwd = getcwd()
try:
os.chdir(target_dir)
yield
finally:
os.chdir(cwd)