michael@0: import os michael@0: import sys michael@0: import warnings michael@0: import imp michael@0: import opcode # opcode is not a virtualenv module, so we can use it to find the stdlib michael@0: # Important! To work on pypy, this must be a module that resides in the michael@0: # lib-python/modified-x.y.z directory michael@0: michael@0: dirname = os.path.dirname michael@0: michael@0: distutils_path = os.path.join(os.path.dirname(opcode.__file__), 'distutils') michael@0: if os.path.normpath(distutils_path) == os.path.dirname(os.path.normpath(__file__)): michael@0: warnings.warn( michael@0: "The virtualenv distutils package at %s appears to be in the same location as the system distutils?") michael@0: else: michael@0: __path__.insert(0, distutils_path) michael@0: real_distutils = imp.load_module("_virtualenv_distutils", None, distutils_path, ('', '', imp.PKG_DIRECTORY)) michael@0: # Copy the relevant attributes michael@0: try: michael@0: __revision__ = real_distutils.__revision__ michael@0: except AttributeError: michael@0: pass michael@0: __version__ = real_distutils.__version__ michael@0: michael@0: from distutils import dist, sysconfig michael@0: michael@0: try: michael@0: basestring michael@0: except NameError: michael@0: basestring = str michael@0: michael@0: ## patch build_ext (distutils doesn't know how to get the libs directory michael@0: ## path on windows - it hardcodes the paths around the patched sys.prefix) michael@0: michael@0: if sys.platform == 'win32': michael@0: from distutils.command.build_ext import build_ext as old_build_ext michael@0: class build_ext(old_build_ext): michael@0: def finalize_options (self): michael@0: if self.library_dirs is None: michael@0: self.library_dirs = [] michael@0: elif isinstance(self.library_dirs, basestring): michael@0: self.library_dirs = self.library_dirs.split(os.pathsep) michael@0: michael@0: self.library_dirs.insert(0, os.path.join(sys.real_prefix, "Libs")) michael@0: old_build_ext.finalize_options(self) michael@0: michael@0: from distutils.command import build_ext as build_ext_module michael@0: build_ext_module.build_ext = build_ext michael@0: michael@0: ## distutils.dist patches: michael@0: michael@0: old_find_config_files = dist.Distribution.find_config_files michael@0: def find_config_files(self): michael@0: found = old_find_config_files(self) michael@0: system_distutils = os.path.join(distutils_path, 'distutils.cfg') michael@0: #if os.path.exists(system_distutils): michael@0: # found.insert(0, system_distutils) michael@0: # What to call the per-user config file michael@0: if os.name == 'posix': michael@0: user_filename = ".pydistutils.cfg" michael@0: else: michael@0: user_filename = "pydistutils.cfg" michael@0: user_filename = os.path.join(sys.prefix, user_filename) michael@0: if os.path.isfile(user_filename): michael@0: for item in list(found): michael@0: if item.endswith('pydistutils.cfg'): michael@0: found.remove(item) michael@0: found.append(user_filename) michael@0: return found michael@0: dist.Distribution.find_config_files = find_config_files michael@0: michael@0: ## distutils.sysconfig patches: michael@0: michael@0: old_get_python_inc = sysconfig.get_python_inc michael@0: def sysconfig_get_python_inc(plat_specific=0, prefix=None): michael@0: if prefix is None: michael@0: prefix = sys.real_prefix michael@0: return old_get_python_inc(plat_specific, prefix) michael@0: sysconfig_get_python_inc.__doc__ = old_get_python_inc.__doc__ michael@0: sysconfig.get_python_inc = sysconfig_get_python_inc michael@0: michael@0: old_get_python_lib = sysconfig.get_python_lib michael@0: def sysconfig_get_python_lib(plat_specific=0, standard_lib=0, prefix=None): michael@0: if standard_lib and prefix is None: michael@0: prefix = sys.real_prefix michael@0: return old_get_python_lib(plat_specific, standard_lib, prefix) michael@0: sysconfig_get_python_lib.__doc__ = old_get_python_lib.__doc__ michael@0: sysconfig.get_python_lib = sysconfig_get_python_lib michael@0: michael@0: old_get_config_vars = sysconfig.get_config_vars michael@0: def sysconfig_get_config_vars(*args): michael@0: real_vars = old_get_config_vars(*args) michael@0: if sys.platform == 'win32': michael@0: lib_dir = os.path.join(sys.real_prefix, "libs") michael@0: if isinstance(real_vars, dict) and 'LIBDIR' not in real_vars: michael@0: real_vars['LIBDIR'] = lib_dir # asked for all michael@0: elif isinstance(real_vars, list) and 'LIBDIR' in args: michael@0: real_vars = real_vars + [lib_dir] # asked for list michael@0: return real_vars michael@0: sysconfig_get_config_vars.__doc__ = old_get_config_vars.__doc__ michael@0: sysconfig.get_config_vars = sysconfig_get_config_vars