summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshimizukawa <shimizukawa@gmail.com>2014-09-26 01:46:29 +0900
committershimizukawa <shimizukawa@gmail.com>2014-09-26 01:46:29 +0900
commit8caef60e757f066c343a0d01a00540239b58a015 (patch)
treef3bd4e5572a33c036dffa83e431592922b93474d
parentac52217d9ca87a1b28ad583a0993d50bc8401f99 (diff)
downloadsphinx-8caef60e757f066c343a0d01a00540239b58a015.tar.gz
refactoring by using `with cd`
-rw-r--r--sphinx/config.py10
-rw-r--r--sphinx/ext/pngmath.py12
-rw-r--r--sphinx/make_mode.py21
-rw-r--r--sphinx/util/osutil.py12
4 files changed, 24 insertions, 31 deletions
diff --git a/sphinx/config.py b/sphinx/config.py
index a4fc234a..69ba7cfe 100644
--- a/sphinx/config.py
+++ b/sphinx/config.py
@@ -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
diff --git a/sphinx/ext/pngmath.py b/sphinx/ext/pngmath.py
index b6546301..abac15cd 100644
--- a/sphinx/ext/pngmath.py
+++ b/sphinx/ext/pngmath.py
@@ -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:
diff --git a/sphinx/make_mode.py b/sphinx/make_mode.py
index bf6764aa..45f62b9b 100644
--- a/sphinx/make_mode.py
+++ b/sphinx/make_mode.py
@@ -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')
diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py
index d7b292b3..e98c49b2 100644
--- a/sphinx/util/osutil.py
+++ b/sphinx/util/osutil.py
@@ -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)