Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | import os |
michael@0 | 2 | import sys |
michael@0 | 3 | import warnings |
michael@0 | 4 | import imp |
michael@0 | 5 | import opcode # opcode is not a virtualenv module, so we can use it to find the stdlib |
michael@0 | 6 | # Important! To work on pypy, this must be a module that resides in the |
michael@0 | 7 | # lib-python/modified-x.y.z directory |
michael@0 | 8 | |
michael@0 | 9 | dirname = os.path.dirname |
michael@0 | 10 | |
michael@0 | 11 | distutils_path = os.path.join(os.path.dirname(opcode.__file__), 'distutils') |
michael@0 | 12 | if os.path.normpath(distutils_path) == os.path.dirname(os.path.normpath(__file__)): |
michael@0 | 13 | warnings.warn( |
michael@0 | 14 | "The virtualenv distutils package at %s appears to be in the same location as the system distutils?") |
michael@0 | 15 | else: |
michael@0 | 16 | __path__.insert(0, distutils_path) |
michael@0 | 17 | real_distutils = imp.load_module("_virtualenv_distutils", None, distutils_path, ('', '', imp.PKG_DIRECTORY)) |
michael@0 | 18 | # Copy the relevant attributes |
michael@0 | 19 | try: |
michael@0 | 20 | __revision__ = real_distutils.__revision__ |
michael@0 | 21 | except AttributeError: |
michael@0 | 22 | pass |
michael@0 | 23 | __version__ = real_distutils.__version__ |
michael@0 | 24 | |
michael@0 | 25 | from distutils import dist, sysconfig |
michael@0 | 26 | |
michael@0 | 27 | try: |
michael@0 | 28 | basestring |
michael@0 | 29 | except NameError: |
michael@0 | 30 | basestring = str |
michael@0 | 31 | |
michael@0 | 32 | ## patch build_ext (distutils doesn't know how to get the libs directory |
michael@0 | 33 | ## path on windows - it hardcodes the paths around the patched sys.prefix) |
michael@0 | 34 | |
michael@0 | 35 | if sys.platform == 'win32': |
michael@0 | 36 | from distutils.command.build_ext import build_ext as old_build_ext |
michael@0 | 37 | class build_ext(old_build_ext): |
michael@0 | 38 | def finalize_options (self): |
michael@0 | 39 | if self.library_dirs is None: |
michael@0 | 40 | self.library_dirs = [] |
michael@0 | 41 | elif isinstance(self.library_dirs, basestring): |
michael@0 | 42 | self.library_dirs = self.library_dirs.split(os.pathsep) |
michael@0 | 43 | |
michael@0 | 44 | self.library_dirs.insert(0, os.path.join(sys.real_prefix, "Libs")) |
michael@0 | 45 | old_build_ext.finalize_options(self) |
michael@0 | 46 | |
michael@0 | 47 | from distutils.command import build_ext as build_ext_module |
michael@0 | 48 | build_ext_module.build_ext = build_ext |
michael@0 | 49 | |
michael@0 | 50 | ## distutils.dist patches: |
michael@0 | 51 | |
michael@0 | 52 | old_find_config_files = dist.Distribution.find_config_files |
michael@0 | 53 | def find_config_files(self): |
michael@0 | 54 | found = old_find_config_files(self) |
michael@0 | 55 | system_distutils = os.path.join(distutils_path, 'distutils.cfg') |
michael@0 | 56 | #if os.path.exists(system_distutils): |
michael@0 | 57 | # found.insert(0, system_distutils) |
michael@0 | 58 | # What to call the per-user config file |
michael@0 | 59 | if os.name == 'posix': |
michael@0 | 60 | user_filename = ".pydistutils.cfg" |
michael@0 | 61 | else: |
michael@0 | 62 | user_filename = "pydistutils.cfg" |
michael@0 | 63 | user_filename = os.path.join(sys.prefix, user_filename) |
michael@0 | 64 | if os.path.isfile(user_filename): |
michael@0 | 65 | for item in list(found): |
michael@0 | 66 | if item.endswith('pydistutils.cfg'): |
michael@0 | 67 | found.remove(item) |
michael@0 | 68 | found.append(user_filename) |
michael@0 | 69 | return found |
michael@0 | 70 | dist.Distribution.find_config_files = find_config_files |
michael@0 | 71 | |
michael@0 | 72 | ## distutils.sysconfig patches: |
michael@0 | 73 | |
michael@0 | 74 | old_get_python_inc = sysconfig.get_python_inc |
michael@0 | 75 | def sysconfig_get_python_inc(plat_specific=0, prefix=None): |
michael@0 | 76 | if prefix is None: |
michael@0 | 77 | prefix = sys.real_prefix |
michael@0 | 78 | return old_get_python_inc(plat_specific, prefix) |
michael@0 | 79 | sysconfig_get_python_inc.__doc__ = old_get_python_inc.__doc__ |
michael@0 | 80 | sysconfig.get_python_inc = sysconfig_get_python_inc |
michael@0 | 81 | |
michael@0 | 82 | old_get_python_lib = sysconfig.get_python_lib |
michael@0 | 83 | def sysconfig_get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
michael@0 | 84 | if standard_lib and prefix is None: |
michael@0 | 85 | prefix = sys.real_prefix |
michael@0 | 86 | return old_get_python_lib(plat_specific, standard_lib, prefix) |
michael@0 | 87 | sysconfig_get_python_lib.__doc__ = old_get_python_lib.__doc__ |
michael@0 | 88 | sysconfig.get_python_lib = sysconfig_get_python_lib |
michael@0 | 89 | |
michael@0 | 90 | old_get_config_vars = sysconfig.get_config_vars |
michael@0 | 91 | def sysconfig_get_config_vars(*args): |
michael@0 | 92 | real_vars = old_get_config_vars(*args) |
michael@0 | 93 | if sys.platform == 'win32': |
michael@0 | 94 | lib_dir = os.path.join(sys.real_prefix, "libs") |
michael@0 | 95 | if isinstance(real_vars, dict) and 'LIBDIR' not in real_vars: |
michael@0 | 96 | real_vars['LIBDIR'] = lib_dir # asked for all |
michael@0 | 97 | elif isinstance(real_vars, list) and 'LIBDIR' in args: |
michael@0 | 98 | real_vars = real_vars + [lib_dir] # asked for list |
michael@0 | 99 | return real_vars |
michael@0 | 100 | sysconfig_get_config_vars.__doc__ = old_get_config_vars.__doc__ |
michael@0 | 101 | sysconfig.get_config_vars = sysconfig_get_config_vars |