1"""
2pip._vendor is for vendoring dependencies of pip to prevent needing pip to
3depend on something external.
4
5Files inside of pip._vendor should be considered immutable and should only be
6updated to versions from upstream.
7"""
8from __future__ import absolute_import
9
10import glob
11import os.path
12import sys
13
14# Downstream redistributors which have debundled our dependencies should also
15# patch this value to be true. This will trigger the additional patching
16# to cause things like "six" to be available as pip.
17DEBUNDLED = False
18
19# By default, look in this directory for a bunch of .whl files which we will
20# add to the beginning of sys.path before attempting to import anything. This
21# is done to support downstream re-distributors like Debian and Fedora who
22# wish to create their own Wheels for our dependencies to aid in debundling.
23WHEEL_DIR = os.path.abspath(os.path.dirname(__file__))
24
25
26# Define a small helper function to alias our vendored modules to the real ones
27# if the vendored ones do not exist. This idea of this was taken from
28# https://github.com/kennethreitz/requests/pull/2567.
29def vendored(modulename):
30    vendored_name = "{0}.{1}".format(__name__, modulename)
31
32    try:
33        __import__(modulename, globals(), locals(), level=0)
34    except ImportError:
35        # We can just silently allow import failures to pass here. If we
36        # got to this point it means that ``import pip._vendor.whatever``
37        # failed and so did ``import whatever``. Since we're importing this
38        # upfront in an attempt to alias imports, not erroring here will
39        # just mean we get a regular import error whenever pip *actually*
40        # tries to import one of these modules to use it, which actually
41        # gives us a better error message than we would have otherwise
42        # gotten.
43        pass
44    else:
45        sys.modules[vendored_name] = sys.modules[modulename]
46        base, head = vendored_name.rsplit(".", 1)
47        setattr(sys.modules[base], head, sys.modules[modulename])
48
49
50# If we're operating in a debundled setup, then we want to go ahead and trigger
51# the aliasing of our vendored libraries as well as looking for wheels to add
52# to our sys.path. This will cause all of this code to be a no-op typically
53# however downstream redistributors can enable it in a consistent way across
54# all platforms.
55if DEBUNDLED:
56    # Actually look inside of WHEEL_DIR to find .whl files and add them to the
57    # front of our sys.path.
58    sys.path[:] = glob.glob(os.path.join(WHEEL_DIR, "*.whl")) + sys.path
59
60    # Actually alias all of our vendored dependencies.
61    vendored("appdirs")
62    vendored("cachecontrol")
63    vendored("certifi")
64    vendored("colorama")
65    vendored("distlib")
66    vendored("distro")
67    vendored("html5lib")
68    vendored("six")
69    vendored("six.moves")
70    vendored("six.moves.urllib")
71    vendored("six.moves.urllib.parse")
72    vendored("packaging")
73    vendored("packaging.version")
74    vendored("packaging.specifiers")
75    vendored("pep517")
76    vendored("pkg_resources")
77    vendored("progress")
78    vendored("requests")
79    vendored("requests.exceptions")
80    vendored("requests.packages")
81    vendored("requests.packages.urllib3")
82    vendored("requests.packages.urllib3._collections")
83    vendored("requests.packages.urllib3.connection")
84    vendored("requests.packages.urllib3.connectionpool")
85    vendored("requests.packages.urllib3.contrib")
86    vendored("requests.packages.urllib3.contrib.ntlmpool")
87    vendored("requests.packages.urllib3.contrib.pyopenssl")
88    vendored("requests.packages.urllib3.exceptions")
89    vendored("requests.packages.urllib3.fields")
90    vendored("requests.packages.urllib3.filepost")
91    vendored("requests.packages.urllib3.packages")
92    vendored("requests.packages.urllib3.packages.ordered_dict")
93    vendored("requests.packages.urllib3.packages.six")
94    vendored("requests.packages.urllib3.packages.ssl_match_hostname")
95    vendored("requests.packages.urllib3.packages.ssl_match_hostname."
96             "_implementation")
97    vendored("requests.packages.urllib3.poolmanager")
98    vendored("requests.packages.urllib3.request")
99    vendored("requests.packages.urllib3.response")
100    vendored("requests.packages.urllib3.util")
101    vendored("requests.packages.urllib3.util.connection")
102    vendored("requests.packages.urllib3.util.request")
103    vendored("requests.packages.urllib3.util.response")
104    vendored("requests.packages.urllib3.util.retry")
105    vendored("requests.packages.urllib3.util.ssl_")
106    vendored("requests.packages.urllib3.util.timeout")
107    vendored("requests.packages.urllib3.util.url")
108    vendored("resolvelib")
109    vendored("tenacity")
110    vendored("tomli")
111    vendored("urllib3")
112