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