From 0a25e61effbc3cbd7ce8f9648a70ca9e53f8aa8c Mon Sep 17 00:00:00 2001 From: Takayuki Shimizukawa Date: Fri, 26 Sep 2014 01:46:29 +0900 Subject: [PATCH] refactoring by using `with cd` --- sphinx/config.py | 10 +++------- sphinx/ext/pngmath.py | 12 ++++-------- sphinx/make_mode.py | 21 +++++---------------- sphinx/util/osutil.py | 12 ++++++++++++ 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/sphinx/config.py b/sphinx/config.py index a4fc234ac..69ba7cfe1 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 b6546301c..abac15cd3 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 bf6764aa3..45f62b9b1 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', '') @@ -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 d7b292b3e..e98c49b20 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)