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. :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from __future__ import with_statement
import os
import re import re
import sys import sys
from os import path from os import path
from sphinx.errors import ConfigError from sphinx.errors import ConfigError
from sphinx.locale import l_ 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_ from sphinx.util.pycompat import bytes, b, execfile_
nonascii_re = re.compile(b(r'[\x80-\xff]')) nonascii_re = re.compile(b(r'[\x80-\xff]'))
@ -220,17 +220,13 @@ class Config(object):
config_file = path.join(dirname, filename) config_file = path.join(dirname, filename)
config['__file__'] = config_file config['__file__'] = config_file
config['tags'] = tags config['tags'] = tags
olddir = os.getcwd() with cd(dirname):
try:
# we promise to have the config dir as current dir while the # we promise to have the config dir as current dir while the
# config file is executed # config file is executed
os.chdir(dirname)
try: try:
execfile_(filename, config) execfile_(filename, config)
except SyntaxError, err: except SyntaxError, err:
raise ConfigError(CONFIG_SYNTAX_ERROR % err) raise ConfigError(CONFIG_SYNTAX_ERROR % err)
finally:
os.chdir(olddir)
self._raw_config = config self._raw_config = config
# these two must be preinitialized because extensions can add their # 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. :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
from __future__ import with_statement
import re import re
import codecs import codecs
import shutil import shutil
import tempfile import tempfile
import posixpath import posixpath
from os import path, getcwd, chdir from os import path
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
try: try:
from hashlib import sha1 as sha from hashlib import sha1 as sha
@ -25,7 +26,7 @@ from docutils import nodes
from sphinx.errors import SphinxError from sphinx.errors import SphinxError
from sphinx.util.png import read_png_depth, write_png_depth 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.util.pycompat import b, sys_encoding
from sphinx.ext.mathbase import setup_math as mathbase_setup, wrap_displaymath 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.extend(self.builder.config.pngmath_latex_args)
ltx_args.append('math.tex') ltx_args.append('math.tex')
curdir = getcwd() with cd(tempdir):
chdir(tempdir)
try:
try: try:
p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) p = Popen(ltx_args, stdout=PIPE, stderr=PIPE)
except OSError, err: except OSError, err:
@ -131,8 +129,6 @@ def render_math(self, math):
self.builder.config.pngmath_latex) self.builder.config.pngmath_latex)
self.builder._mathpng_warned_latex = True self.builder._mathpng_warned_latex = True
return None, None return None, None
finally:
chdir(curdir)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
if p.returncode != 0: if p.returncode != 0:

View File

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

View File

@ -18,6 +18,7 @@ import locale
import shutil import shutil
import gettext import gettext
from os import path from os import path
import contextlib
# Errnos that we need. # Errnos that we need.
EEXIST = getattr(errno, 'EEXIST', 0) EEXIST = getattr(errno, 'EEXIST', 0)
@ -196,3 +197,14 @@ def abspath(pathdir):
if isinstance(pathdir, bytes): if isinstance(pathdir, bytes):
pathdir = pathdir.decode(fs_encoding) pathdir = pathdir.decode(fs_encoding)
return pathdir 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)