aboutsummaryrefslogtreecommitdiffstats
path: root/setuptools/sandbox.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2017-09-03 19:57:54 -0400
committerJason R. Coombs <jaraco@jaraco.com>2017-09-03 20:01:45 -0400
commitdcb24ad15465c266a3f258471766fbbe8fc8a42e (patch)
tree13123440610d78e398476a8ce1e8cc3d9f9ec72e /setuptools/sandbox.py
parentf14930e66601b462699c44384c482cd966f53b8f (diff)
parent1b192005562d5cf0de30c02154c58fd1dca577c8 (diff)
downloadexternal_python_setuptools-dcb24ad15465c266a3f258471766fbbe8fc8a42e.tar.gz
external_python_setuptools-dcb24ad15465c266a3f258471766fbbe8fc8a42e.tar.bz2
external_python_setuptools-dcb24ad15465c266a3f258471766fbbe8fc8a42e.zip
Merge branch 'master' into drop-py26
Diffstat (limited to 'setuptools/sandbox.py')
-rwxr-xr-xsetuptools/sandbox.py73
1 files changed, 42 insertions, 31 deletions
diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py
index 640691d8..685f3f72 100755
--- a/setuptools/sandbox.py
+++ b/setuptools/sandbox.py
@@ -7,11 +7,12 @@ import itertools
import re
import contextlib
import pickle
+import textwrap
from setuptools.extern import six
from setuptools.extern.six.moves import builtins, map
-import pkg_resources
+import pkg_resources.py31compat
if sys.platform.startswith('java'):
import org.python.modules.posix.PosixModule as _os
@@ -25,6 +26,7 @@ _open = open
from distutils.errors import DistutilsError
from pkg_resources import working_set
+
__all__ = [
"AbstractSandbox", "DirectorySandbox", "SandboxViolation", "run_setup",
]
@@ -68,8 +70,7 @@ def override_temp(replacement):
"""
Monkey-patch tempfile.tempdir with replacement, ensuring it exists
"""
- if not os.path.isdir(replacement):
- os.makedirs(replacement)
+ pkg_resources.py31compat.makedirs(replacement, exist_ok=True)
saved = tempfile.tempdir
@@ -211,7 +212,7 @@ def _needs_hiding(mod_name):
>>> _needs_hiding('Cython')
True
"""
- pattern = re.compile('(setuptools|pkg_resources|distutils|Cython)(\.|$)')
+ pattern = re.compile(r'(setuptools|pkg_resources|distutils|Cython)(\.|$)')
return bool(pattern.match(mod_name))
@@ -237,11 +238,16 @@ def run_setup(setup_script, args):
working_set.__init__()
working_set.callbacks.append(lambda dist: dist.activate())
- def runner():
- ns = dict(__file__=setup_script, __name__='__main__')
- _execfile(setup_script, ns)
+ # __file__ should be a byte string on Python 2 (#712)
+ dunder_file = (
+ setup_script
+ if isinstance(setup_script, str) else
+ setup_script.encode(sys.getfilesystemencoding())
+ )
- DirectorySandbox(setup_dir).run(runner)
+ with DirectorySandbox(setup_dir):
+ ns = dict(__file__=dunder_file, __name__='__main__')
+ _execfile(setup_script, ns)
except SystemExit as v:
if v.args and v.args[0]:
raise
@@ -263,21 +269,24 @@ class AbstractSandbox:
for name in self._attrs:
setattr(os, name, getattr(source, name))
+ def __enter__(self):
+ self._copy(self)
+ if _file:
+ builtins.file = self._file
+ builtins.open = self._open
+ self._active = True
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self._active = False
+ if _file:
+ builtins.file = _file
+ builtins.open = _open
+ self._copy(_os)
+
def run(self, func):
"""Run 'func' under os sandboxing"""
- try:
- self._copy(self)
- if _file:
- builtins.file = self._file
- builtins.open = self._open
- self._active = True
+ with self:
return func()
- finally:
- self._active = False
- if _file:
- builtins.file = _file
- builtins.open = _open
- self._copy(_os)
def _mk_dual_path_wrapper(name):
original = getattr(_os, name)
@@ -380,7 +389,7 @@ class DirectorySandbox(AbstractSandbox):
_exception_patterns = [
# Allow lib2to3 to attempt to save a pickled grammar object (#121)
- '.*lib2to3.*\.pickle$',
+ r'.*lib2to3.*\.pickle$',
]
"exempt writing to paths that match the pattern"
@@ -465,16 +474,18 @@ WRITE_FLAGS = functools.reduce(
class SandboxViolation(DistutilsError):
"""A setup script attempted to modify the filesystem outside the sandbox"""
- def __str__(self):
- return """SandboxViolation: %s%r %s
-
-The package setup script has attempted to modify files on your system
-that are not within the EasyInstall build area, and has been aborted.
+ tmpl = textwrap.dedent("""
+ SandboxViolation: {cmd}{args!r} {kwargs}
-This package cannot be safely installed by EasyInstall, and may not
-support alternate installation locations even if you run its setup
-script by hand. Please inform the package's author and the EasyInstall
-maintainers to find out if a fix or workaround is available.""" % self.args
+ The package setup script has attempted to modify files on your system
+ that are not within the EasyInstall build area, and has been aborted.
+ This package cannot be safely installed by EasyInstall, and may not
+ support alternate installation locations even if you run its setup
+ script by hand. Please inform the package's author and the EasyInstall
+ maintainers to find out if a fix or workaround is available.
+ """).lstrip()
-#
+ def __str__(self):
+ cmd, args, kwargs = self.args
+ return self.tmpl.format(**locals())