Temporary prefix for installing build dependencies
@@ -14,6 +14,7 @@ import re
import shutil
import stat
import sys
+import sysconfig
import tempfile
import warnings
@@ -631,6 +632,55 @@ class Wheel(object):
return bool(set(tags).intersection(self.file_tags))
+class BuildEnvironment(object):
+ """Context manager to install build deps in a simple temporary environment
+ """
+ def __init__(self):
+ self.prefix = tempfile.mkdtemp('pip-build-env-')
+
+ def __enter__(self):
+ self.save_path = os.environ.get('PATH', None)
+ self.save_pythonpath = os.environ.get('PYTHONPATH', None)
+
+ install_scheme = 'nt' if (os.name == 'nt') else 'posix_prefix'
+ install_dirs = sysconfig.get_paths(install_scheme, vars={
+ 'base': self.prefix,
+ 'platbase': self.prefix,
+ })
+
+ scripts = install_dirs['scripts']
+ if self.save_path:
+ os.environ['PATH'] = scripts + os.pathsep + self.save_path
+ else:
+ os.environ['PATH'] = scripts + os.pathsep + os.defpath
+
+ if install_dirs['purelib'] == install_dirs['platlib']:
+ lib_dirs = install_dirs['purelib']
+ else:
+ lib_dirs = install_dirs['purelib'] + os.pathsep + install_dirs['platlib']
+ if self.save_pythonpath:
+ os.environ['PYTHONPATH'] = lib_dirs + os.pathsep + self.save_pythonpath
+ else:
+ os.environ['PYTHONPATH'] = lib_dirs
+
+ return self.prefix
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ if self.save_path is None:
+ os.environ.pop('PATH', None)
+ else:
+ os.environ['PATH'] = self.save_path
+
+ if self.save_pythonpath is None:
+ os.environ.pop('PYTHONPATH', None)
+ else:
+ os.environ['PYTHONPATH'] = self.save_pythonpath
+
+ rmtree(self.prefix)
+
+ return False # Do not suppress exceptions
+
+
class WheelBuilder(object):
"""Build wheels from a RequirementSet."""
