2019-07-17 15:42:23 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
2020-08-10 11:58:17 +02:00
|
|
|
import subprocess
|
2019-07-17 15:42:23 +02:00
|
|
|
import sys
|
2019-07-17 13:54:09 +02:00
|
|
|
import tox
|
2020-09-15 14:16:40 +02:00
|
|
|
import warnings
|
|
|
|
import argparse
|
2019-07-17 13:54:09 +02:00
|
|
|
|
2020-08-27 17:35:19 +02:00
|
|
|
try:
|
|
|
|
import importlib.metadata as importlib_metadata
|
|
|
|
except ImportError:
|
|
|
|
import importlib_metadata
|
|
|
|
|
2019-07-17 13:54:09 +02:00
|
|
|
|
2019-07-17 15:42:23 +02:00
|
|
|
@tox.hookimpl
|
|
|
|
def tox_addoption(parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"--current-env",
|
|
|
|
action="store_true",
|
|
|
|
dest="current_env",
|
|
|
|
default=False,
|
|
|
|
help="Run tests in current environment, not creating any virtual environment",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--print-deps-only",
|
|
|
|
action="store_true",
|
|
|
|
dest="print_deps_only",
|
|
|
|
default=False,
|
2020-09-15 14:16:40 +02:00
|
|
|
help="Deprecated, equivalent to `--print-deps-to -`",
|
2019-07-17 15:42:23 +02:00
|
|
|
)
|
2019-08-01 12:39:12 +02:00
|
|
|
parser.add_argument(
|
2020-09-15 14:16:40 +02:00
|
|
|
"--print-deps-to",
|
2019-08-01 12:39:12 +02:00
|
|
|
"--print-deps-to-file",
|
|
|
|
action="store",
|
2020-09-15 14:16:40 +02:00
|
|
|
type=argparse.FileType('w'),
|
|
|
|
metavar="FILE",
|
2019-08-01 12:39:12 +02:00
|
|
|
default=None,
|
2020-09-15 14:16:40 +02:00
|
|
|
help="Don't run tests, only print the dependencies to the given file "
|
|
|
|
+ "(use `-` for stdout)",
|
2019-08-01 12:39:12 +02:00
|
|
|
)
|
2020-09-11 15:20:03 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--print-extras-to",
|
|
|
|
"--print-extras-to-file",
|
|
|
|
action="store",
|
|
|
|
type=argparse.FileType('w'),
|
|
|
|
metavar="FILE",
|
|
|
|
default=None,
|
|
|
|
help="Don't run tests, only print the names of the required extras to the given file "
|
|
|
|
+ "(use `-` for stdout)",
|
|
|
|
)
|
2019-07-17 15:42:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@tox.hookimpl
|
|
|
|
def tox_configure(config):
|
2019-07-18 11:33:42 +02:00
|
|
|
"""Stores options in the config. Makes all commands external and skips sdist"""
|
2020-09-15 14:16:40 +02:00
|
|
|
if config.option.print_deps_only:
|
|
|
|
warnings.warn(
|
|
|
|
"--print-deps-only is deprecated; use `--print-deps-to -`",
|
|
|
|
DeprecationWarning,
|
2019-08-12 16:17:41 +02:00
|
|
|
)
|
2020-09-15 14:16:40 +02:00
|
|
|
if not config.option.print_deps_to:
|
|
|
|
config.option.print_deps_to = sys.stdout
|
|
|
|
else:
|
|
|
|
raise tox.exception.ConfigError(
|
|
|
|
"--print-deps-only cannot be used together "
|
|
|
|
+ "with --print-deps-to"
|
|
|
|
)
|
2020-09-11 15:20:03 +02:00
|
|
|
if config.option.current_env or config.option.print_deps_to or config.option.print_extras_to:
|
2019-07-17 15:42:23 +02:00
|
|
|
config.skipsdist = True
|
|
|
|
for testenv in config.envconfigs:
|
|
|
|
config.envconfigs[testenv].whitelist_externals = "*"
|
|
|
|
|
2020-09-11 15:20:03 +02:00
|
|
|
if (getattr(config.option.print_deps_to, "name", object()) ==
|
|
|
|
getattr(config.option.print_extras_to, "name", object())):
|
|
|
|
raise tox.exception.ConfigError(
|
|
|
|
"The paths given to --print-deps-to and --print-extras-to cannot be identical."
|
|
|
|
)
|
|
|
|
|
2019-07-17 15:42:23 +02:00
|
|
|
return config
|
|
|
|
|
2019-07-17 18:59:50 +02:00
|
|
|
|
2019-07-17 16:48:12 +02:00
|
|
|
class InterpreterMismatch(tox.exception.InterpreterNotFound):
|
|
|
|
"""Interpreter version in current env does not match requested version"""
|
2019-07-17 15:42:23 +02:00
|
|
|
|
2019-07-17 18:59:50 +02:00
|
|
|
|
2019-07-26 13:35:26 +02:00
|
|
|
def _python_activate_exists(venv):
|
|
|
|
python = venv.envconfig.get_envpython()
|
|
|
|
bindir = os.path.dirname(python)
|
|
|
|
activate = os.path.join(bindir, "activate")
|
|
|
|
return os.path.exists(python), os.path.exists(activate)
|
|
|
|
|
|
|
|
|
|
|
|
def is_current_env_link(venv):
|
|
|
|
python, activate = _python_activate_exists(venv)
|
|
|
|
return python and not activate
|
|
|
|
|
|
|
|
|
|
|
|
def is_proper_venv(venv):
|
|
|
|
python, activate = _python_activate_exists(venv)
|
|
|
|
return python and activate
|
|
|
|
|
|
|
|
|
2019-07-26 13:49:54 +02:00
|
|
|
def is_any_env(venv):
|
|
|
|
python, activate = _python_activate_exists(venv)
|
|
|
|
return python
|
|
|
|
|
|
|
|
|
2019-08-01 13:24:41 +02:00
|
|
|
def rm_venv(venv):
|
|
|
|
link = venv.envconfig.get_envpython()
|
|
|
|
shutil.rmtree(os.path.dirname(os.path.dirname(link)), ignore_errors=True)
|
|
|
|
|
|
|
|
|
2019-07-26 13:35:26 +02:00
|
|
|
def unsupported_raise(config, venv):
|
|
|
|
if config.option.recreate:
|
|
|
|
return
|
2020-09-11 15:20:03 +02:00
|
|
|
regular = not (config.option.current_env or config.option.print_deps_to or config.option.print_extras_to)
|
2019-07-26 13:35:26 +02:00
|
|
|
if regular and is_current_env_link(venv):
|
2019-08-01 14:46:01 +02:00
|
|
|
if hasattr(tox.hookspecs, "tox_cleanup"):
|
|
|
|
raise tox.exception.ConfigError(
|
2020-09-11 15:20:03 +02:00
|
|
|
"Looks like previous --current-env, --print-deps-to or --print-extras-to tox run didn't finish the cleanup. "
|
2019-08-01 14:46:01 +02:00
|
|
|
"Run tox run with --recreate (-r) or manually remove the environment in .tox."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise tox.exception.ConfigError(
|
2020-09-11 15:20:03 +02:00
|
|
|
"Regular tox run after --current-env, --print-deps-to or --print-extras-to tox run "
|
|
|
|
"is not supported without --recreate (-r)."
|
2019-08-01 14:46:01 +02:00
|
|
|
)
|
2019-07-26 13:35:26 +02:00
|
|
|
elif config.option.current_env and is_proper_venv(venv):
|
|
|
|
raise tox.exception.ConfigError(
|
|
|
|
"--current-env after regular tox run is not supported without --recreate (-r)."
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-07-17 13:54:09 +02:00
|
|
|
@tox.hookimpl
|
|
|
|
def tox_testenv_create(venv, action):
|
2019-07-17 18:59:50 +02:00
|
|
|
"""We create a fake virtualenv with just the symbolic link"""
|
2019-07-17 15:42:23 +02:00
|
|
|
config = venv.envconfig.config
|
2019-07-26 13:49:54 +02:00
|
|
|
create_fake_env = check_version = config.option.current_env
|
2020-09-11 15:20:03 +02:00
|
|
|
if config.option.print_deps_to or config.option.print_extras_to:
|
2019-07-26 13:49:54 +02:00
|
|
|
if is_any_env(venv):
|
|
|
|
# We don't need anything
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# We need at least some kind of environment,
|
|
|
|
# or tox fails without a python command
|
|
|
|
# We fallback to --current-env behavior,
|
|
|
|
# because it's cheaper, faster and won't install stuff
|
|
|
|
create_fake_env = True
|
|
|
|
if check_version:
|
2020-09-11 15:20:03 +02:00
|
|
|
# With real --current-env, we check this, but not with --print-deps/extras-to only
|
2019-07-17 16:48:12 +02:00
|
|
|
version_info = venv.envconfig.python_info.version_info
|
2019-07-23 14:37:06 +02:00
|
|
|
if version_info is None:
|
|
|
|
raise tox.exception.InterpreterNotFound(venv.envconfig.basepython)
|
2019-07-17 16:48:12 +02:00
|
|
|
if version_info[:2] != sys.version_info[:2]:
|
|
|
|
raise InterpreterMismatch(
|
2019-07-17 18:59:50 +02:00
|
|
|
f"tox_current_env: interpreter versions do not match:\n"
|
|
|
|
+ f" in current env: {tuple(sys.version_info)}\n"
|
|
|
|
+ f" requested: {version_info}"
|
2019-07-17 16:48:12 +02:00
|
|
|
)
|
2019-07-26 13:49:54 +02:00
|
|
|
if create_fake_env:
|
2019-07-17 16:04:55 +02:00
|
|
|
# Make sure the `python` command on path is sys.executable.
|
|
|
|
# (We might have e.g. /usr/bin/python3, not `python`.)
|
|
|
|
# Remove the rest of the virtualenv.
|
2019-07-17 15:42:23 +02:00
|
|
|
link = venv.envconfig.get_envpython()
|
|
|
|
target = sys.executable
|
2020-01-13 00:33:21 +01:00
|
|
|
rm_venv(venv)
|
2019-07-17 15:42:23 +02:00
|
|
|
os.makedirs(os.path.dirname(link))
|
2020-08-10 11:58:17 +02:00
|
|
|
if sys.platform == "win32":
|
|
|
|
# Avoid requiring admin rights on Windows
|
|
|
|
subprocess.check_call(f'mklink /J "{link}" "{target}"', shell=True)
|
|
|
|
else:
|
|
|
|
os.symlink(target, link)
|
2020-01-13 00:33:21 +01:00
|
|
|
# prevent tox from creating the venv
|
2019-07-17 15:42:23 +02:00
|
|
|
return True
|
2020-01-13 00:33:21 +01:00
|
|
|
if not is_proper_venv(venv):
|
2019-08-01 13:24:41 +02:00
|
|
|
rm_venv(venv)
|
2020-01-13 00:33:21 +01:00
|
|
|
return None # let tox handle the rest
|
2019-07-17 15:42:23 +02:00
|
|
|
|
|
|
|
|
2019-07-29 14:37:17 +02:00
|
|
|
@tox.hookimpl
|
|
|
|
def tox_package(session, venv):
|
|
|
|
"""Fail early when unsupported"""
|
|
|
|
config = venv.envconfig.config
|
|
|
|
unsupported_raise(config, venv)
|
|
|
|
|
|
|
|
|
2019-07-17 15:42:23 +02:00
|
|
|
@tox.hookimpl
|
|
|
|
def tox_testenv_install_deps(venv, action):
|
|
|
|
"""We don't install anything"""
|
|
|
|
config = venv.envconfig.config
|
2019-07-17 18:59:50 +02:00
|
|
|
unsupported_raise(config, venv)
|
2020-09-15 14:16:40 +02:00
|
|
|
if config.option.current_env or config.option.print_deps_to:
|
2019-07-17 15:42:23 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@tox.hookimpl
|
|
|
|
def tox_runtest(venv, redirect):
|
2020-09-11 15:20:03 +02:00
|
|
|
"""If --print-deps-to, prints deps instead of running tests.
|
|
|
|
If --print-extras-to, prints extras instead of running tests.
|
|
|
|
Both options can be used together."""
|
2019-07-17 18:59:50 +02:00
|
|
|
config = venv.envconfig.config
|
|
|
|
unsupported_raise(config, venv)
|
2020-09-11 15:20:03 +02:00
|
|
|
ret = None
|
|
|
|
|
2020-09-15 14:16:40 +02:00
|
|
|
if config.option.print_deps_to:
|
|
|
|
print(
|
|
|
|
*venv.get_resolved_dependencies(),
|
|
|
|
sep="\n",
|
|
|
|
file=config.option.print_deps_to,
|
|
|
|
)
|
2020-09-11 15:20:03 +02:00
|
|
|
ret = True
|
|
|
|
|
|
|
|
if config.option.print_extras_to:
|
|
|
|
print(
|
|
|
|
*venv.envconfig.extras,
|
|
|
|
sep="\n",
|
|
|
|
file=config.option.print_extras_to,
|
|
|
|
)
|
|
|
|
ret = True
|
|
|
|
|
|
|
|
return ret
|
2019-08-01 13:24:41 +02:00
|
|
|
|
|
|
|
|
2020-08-12 17:04:58 +02:00
|
|
|
@tox.hookimpl
|
|
|
|
def tox_cleanup(session):
|
|
|
|
"""Remove the fake virtualenv not to collide with regular tox
|
|
|
|
Collisions can happen anyway (when tox is killed forcefully before this happens)
|
|
|
|
Note that we don't remove real venvs, as recreating them is expensive"""
|
|
|
|
for venv in session.venv_dict.values():
|
|
|
|
if is_current_env_link(venv):
|
|
|
|
rm_venv(venv)
|
2020-08-27 17:35:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
@tox.hookimpl
|
|
|
|
def tox_runenvreport(venv, action):
|
2020-09-11 14:42:56 +02:00
|
|
|
"""Prevent using pip to display installed packages,
|
|
|
|
use importlib.metadata instead, but fallback to default without our flags."""
|
|
|
|
option = venv.envconfig.config.option
|
|
|
|
if not (option.current_env or option.print_deps_only):
|
2020-08-27 17:35:19 +02:00
|
|
|
return None
|
|
|
|
return (
|
|
|
|
"{}=={}".format(d.metadata.get("name"), d.version)
|
2020-09-11 14:42:56 +02:00
|
|
|
for d in sorted(
|
|
|
|
importlib_metadata.distributions(), key=lambda d: d.metadata.get("name")
|
|
|
|
)
|
2020-08-27 17:35:19 +02:00
|
|
|
)
|