diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2017-05-30 19:39:58 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2017-05-30 19:39:58 -0400 |
commit | fcdf12ee7fdf43c9dded5b68232a0fb3376d4858 (patch) | |
tree | 29baaad1490dcce705ccc5f32c6d9d9240e1d10d /setuptools/sandbox.py | |
parent | 3d0cc355fb5e8012cb8c72f0e25042a5a44f31d6 (diff) | |
parent | 4dc2c76b62a5071dfacf434555dfa8ec2be0b433 (diff) | |
download | external_python_setuptools-fcdf12ee7fdf43c9dded5b68232a0fb3376d4858.tar.gz external_python_setuptools-fcdf12ee7fdf43c9dded5b68232a0fb3376d4858.tar.bz2 external_python_setuptools-fcdf12ee7fdf43c9dded5b68232a0fb3376d4858.zip |
Merge branch 'master' into feature/re-vendor-sadface
Diffstat (limited to 'setuptools/sandbox.py')
-rwxr-xr-x | setuptools/sandbox.py | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 817a3afa..f99c13c4 100755 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -7,6 +7,7 @@ import itertools import re import contextlib import pickle +import textwrap from setuptools.extern import six from setuptools.extern.six.moves import builtins, map @@ -215,7 +216,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)) @@ -248,11 +249,9 @@ def run_setup(setup_script, args): setup_script.encode(sys.getfilesystemencoding()) ) - def runner(): + with DirectorySandbox(setup_dir): ns = dict(__file__=dunder_file, __name__='__main__') _execfile(setup_script, ns) - - DirectorySandbox(setup_dir).run(runner) except SystemExit as v: if v.args and v.args[0]: raise @@ -274,21 +273,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) @@ -391,7 +393,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" @@ -476,16 +478,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()) |