mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactoring by using with cd
This commit is contained in:
parent
636c14ef4a
commit
0a25e61eff
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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')
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user