Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
mkvenv: use pip's vendored distlib as a fallback
distlib is usually not installed on Linux distribution, but it is vendored
into pip.  Because the virtual environment has pip via ensurepip, we
can piggy-back on pip's vendored version.  This could break if they move
our cheese in the future, but the fix would be simply to require distlib.

If it is debundled, as it is on msys, it is simply available directly.

Signed-off-by: John Snow <jsnow@redhat.com>
[Move to toplevel. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
jnsnow authored and bonzini committed May 16, 2023
1 parent 65d50a0 commit 8616c06
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
25 changes: 22 additions & 3 deletions python/scripts/mkvenv.py
Expand Up @@ -69,10 +69,25 @@
import venv
import warnings

import distlib.database
import distlib.scripts
import distlib.version

# Try to load distlib, with a fallback to pip's vendored version.
# HAVE_DISTLIB is checked below, just-in-time, so that mkvenv does not fail
# outside the venv or before a potential call to ensurepip in checkpip().
HAVE_DISTLIB = True
try:
import distlib.database
import distlib.scripts
import distlib.version
except ImportError:
try:
# Reach into pip's cookie jar. pylint and flake8 don't understand
# that these imports will be used via distlib.xxx.
from pip._vendor import distlib
import pip._vendor.distlib.database # noqa, pylint: disable=unused-import
import pip._vendor.distlib.scripts # noqa, pylint: disable=unused-import
import pip._vendor.distlib.version # noqa, pylint: disable=unused-import
except ImportError:
HAVE_DISTLIB = False

# Do not add any mandatory dependencies from outside the stdlib:
# This script *must* be usable standalone!
Expand Down Expand Up @@ -664,6 +679,10 @@ def ensure(
bellwether for the presence of 'sphinx'.
"""
print(f"mkvenv: checking for {', '.join(dep_specs)}", file=sys.stderr)

if not HAVE_DISTLIB:
raise Ouch("a usable distlib could not be found, please install it")

try:
_do_ensure(dep_specs, online, wheels_dir)
except subprocess.CalledProcessError as exc:
Expand Down
18 changes: 18 additions & 0 deletions python/setup.cfg
Expand Up @@ -125,6 +125,24 @@ ignore_missing_imports = True
[mypy-distlib.version]
ignore_missing_imports = True

[mypy-pip]
ignore_missing_imports = True

[mypy-pip._vendor]
ignore_missing_imports = True

[mypy-pip._vendor.distlib]
ignore_missing_imports = True

[mypy-pip._vendor.distlib.database]
ignore_missing_imports = True

[mypy-pip._vendor.distlib.scripts]
ignore_missing_imports = True

[mypy-pip._vendor.distlib.version]
ignore_missing_imports = True

[pylint.messages control]
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
Expand Down

0 comments on commit 8616c06

Please sign in to comment.