From 4eae4aca838d3649688ad99c010bef9e8e08200e Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Tue, 11 Feb 2020 13:29:48 +0800 Subject: [PATCH] Convert virtualenv usages in test to 20+ API virtualenv 20.0+ completely revamps its API: * There's no path_locations() anymore. Stdlib sysconfig can replace it completely. * The Python API to create an environment is split into several pieces. Use the entry point function for now to provide the functionality. --- tests/lib/venv.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/tests/lib/venv.py b/tests/lib/venv.py index cc94e29f254..ee634041d27 100644 --- a/tests/lib/venv.py +++ b/tests/lib/venv.py @@ -2,11 +2,11 @@ import compileall import shutil -import sys +import sysconfig import textwrap import six -import virtualenv as _virtualenv +import virtualenv.run as _virtualenv_run from .path import Path @@ -32,16 +32,13 @@ def __init__(self, location, template=None, venv_type=None): self._create() def _update_paths(self): - home, lib, inc, bin = _virtualenv.path_locations(self.location) - self.bin = Path(bin) - self.site = Path(lib) / 'site-packages' - # Workaround for https://github.com/pypa/virtualenv/issues/306 - if hasattr(sys, "pypy_version_info"): - version_fmt = '{0}' if six.PY3 else '{0}.{1}' - version_dir = version_fmt.format(*sys.version_info) - self.lib = Path(home, 'lib-python', version_dir) - else: - self.lib = Path(lib) + paths = sysconfig.get_paths(vars={ + "base": self.location, + "platbase": self.location, + }) + self.bin = Path(paths["scripts"]) + self.site = Path(paths["purelib"]) + self.lib = Path(paths["stdlib"]) def __repr__(self): return "".format(self.location) @@ -50,10 +47,6 @@ def _create(self, clear=False): if clear: shutil.rmtree(self.location) if self._template: - # On Windows, calling `_virtualenv.path_locations(target)` - # will have created the `target` directory... - if sys.platform == 'win32' and self.location.exists(): - self.location.rmdir() # Clone virtual environment from template. shutil.copytree( self._template.location, self.location, symlinks=True @@ -63,12 +56,12 @@ def _create(self, clear=False): else: # Create a new virtual environment. if self._venv_type == 'virtualenv': - _virtualenv.create_environment( + _virtualenv_run.run_via_cli([ self.location, - no_pip=True, - no_wheel=True, - no_setuptools=True, - ) + "--no-pip", + "--no-wheel", + "--no-setuptools", + ]) self._fix_virtualenv_site_module() elif self._venv_type == 'venv': builder = _venv.EnvBuilder() @@ -76,6 +69,8 @@ def _create(self, clear=False): builder.create_configuration(context) builder.setup_python(context) self.site.mkdir(parents=True, exist_ok=True) + else: + raise ValueError("venv type must be 'virtualenv' or 'venv'") self.sitecustomize = self._sitecustomize self.user_site_packages = self._user_site_packages