You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.9 KiB

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