__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. pip._vendor is for vendoring dependencies of pip to prevent needing pip to
  3. depend on something external.
  4. Files inside of pip._vendor should be considered immutable and should only be
  5. updated to versions from upstream.
  6. """
  7. from __future__ import absolute_import
  8. import glob
  9. import os.path
  10. import sys
  11. # By default, look in this directory for a bunch of .whl files which we will
  12. # add to the beginning of sys.path before attempting to import anything. This
  13. # is done to support downstream re-distributors like Debian and Fedora who
  14. # wish to create their own Wheels for our dependencies to aid in debundling.
  15. WHEEL_DIR = os.path.abspath(os.path.dirname(__file__))
  16. # Actually look inside of WHEEL_DIR to find .whl files and add them to the
  17. # front of our sys.path.
  18. sys.path = glob.glob(os.path.join(WHEEL_DIR, "*.whl")) + sys.path
  19. class VendorAlias(object):
  20. def __init__(self, package_names):
  21. self._package_names = package_names
  22. self._vendor_name = __name__
  23. self._vendor_pkg = self._vendor_name + "."
  24. self._vendor_pkgs = [
  25. self._vendor_pkg + name for name in self._package_names
  26. ]
  27. def find_module(self, fullname, path=None):
  28. if fullname.startswith(self._vendor_pkg):
  29. return self
  30. def load_module(self, name):
  31. # Ensure that this only works for the vendored name
  32. if not name.startswith(self._vendor_pkg):
  33. raise ImportError(
  34. "Cannot import %s, must be a subpackage of '%s'." % (
  35. name, self._vendor_name,
  36. )
  37. )
  38. if not (name == self._vendor_name or
  39. any(name.startswith(pkg) for pkg in self._vendor_pkgs)):
  40. raise ImportError(
  41. "Cannot import %s, must be one of %s." % (
  42. name, self._vendor_pkgs
  43. )
  44. )
  45. # Check to see if we already have this item in sys.modules, if we do
  46. # then simply return that.
  47. if name in sys.modules:
  48. return sys.modules[name]
  49. # Check to see if we can import the vendor name
  50. try:
  51. # We do this dance here because we want to try and import this
  52. # module without hitting a recursion error because of a bunch of
  53. # VendorAlias instances on sys.meta_path
  54. real_meta_path = sys.meta_path[:]
  55. try:
  56. sys.meta_path = [
  57. m for m in sys.meta_path
  58. if not isinstance(m, VendorAlias)
  59. ]
  60. __import__(name)
  61. module = sys.modules[name]
  62. finally:
  63. # Re-add any additions to sys.meta_path that were made while
  64. # during the import we just did, otherwise things like
  65. # pip._vendor.six.moves will fail.
  66. for m in sys.meta_path:
  67. if m not in real_meta_path:
  68. real_meta_path.append(m)
  69. # Restore sys.meta_path with any new items.
  70. sys.meta_path = real_meta_path
  71. except ImportError:
  72. # We can't import the vendor name, so we'll try to import the
  73. # "real" name.
  74. real_name = name[len(self._vendor_pkg):]
  75. try:
  76. __import__(real_name)
  77. module = sys.modules[real_name]
  78. except ImportError:
  79. raise ImportError("No module named '%s'" % (name,))
  80. # If we've gotten here we've found the module we're looking for, either
  81. # as part of our vendored package, or as the real name, so we'll add
  82. # it to sys.modules as the vendored name so that we don't have to do
  83. # the lookup again.
  84. sys.modules[name] = module
  85. # Finally, return the loaded module
  86. return module
  87. sys.meta_path.append(VendorAlias([
  88. "_markerlib", "cachecontrol", "certifi", "colorama", "distlib", "html5lib",
  89. "ipaddress", "lockfile", "packaging", "pkg_resources", "progress",
  90. "requests", "retrying", "six",
  91. ]))