Merge pull request #49 from enpaul/enp/log

Add internal logging wrapper to reduce copy/paste
This commit is contained in:
Ethan Paul 2021-04-16 20:16:38 -04:00 committed by GitHub
commit ab4fc1197f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 52 deletions

View File

@ -12,9 +12,9 @@ from tox.config import Parser as ToxParser
from tox.venv import VirtualEnv as ToxVirtualEnv from tox.venv import VirtualEnv as ToxVirtualEnv
from tox_poetry_installer import __about__ from tox_poetry_installer import __about__
from tox_poetry_installer import constants
from tox_poetry_installer import exceptions from tox_poetry_installer import exceptions
from tox_poetry_installer import installer from tox_poetry_installer import installer
from tox_poetry_installer import logger
from tox_poetry_installer import utilities from tox_poetry_installer import utilities
from tox_poetry_installer.datatypes import PackageMap from tox_poetry_installer.datatypes import PackageMap
@ -83,19 +83,17 @@ def tox_testenv_install_deps(venv: ToxVirtualEnv, action: ToxAction) -> Optional
and venv.envconfig.config.option.require_poetry and venv.envconfig.config.option.require_poetry
): ):
venv.status = err.__class__.__name__ venv.status = err.__class__.__name__
tox.reporter.error(str(err)) logger.error(str(err))
return False return False
tox.reporter.verbosity1(str(err)) logger.info(str(err))
return None return None
tox.reporter.verbosity1( logger.info(f"Loaded project pyproject.toml from {poetry.file}")
f"{constants.REPORTER_PREFIX} Loaded project pyproject.toml from {poetry.file}"
)
virtualenv = utilities.convert_virtualenv(venv) virtualenv = utilities.convert_virtualenv(venv)
if not poetry.locker.is_fresh(): if not poetry.locker.is_fresh():
tox.reporter.warning( logger.warning(
f"The Poetry lock file is not up to date with the latest changes in {poetry.file}" f"The Poetry lock file is not up to date with the latest changes in {poetry.file}"
) )
@ -112,42 +110,38 @@ def tox_testenv_install_deps(venv: ToxVirtualEnv, action: ToxAction) -> Optional
if venv.envconfig.install_dev_deps: if venv.envconfig.install_dev_deps:
dev_deps = utilities.find_dev_deps(packages, virtualenv, poetry) dev_deps = utilities.find_dev_deps(packages, virtualenv, poetry)
tox.reporter.verbosity1( logger.info(
f"{constants.REPORTER_PREFIX} Identified {len(dev_deps)} development dependencies to install to env" f"Identified {len(dev_deps)} development dependencies to install to env"
) )
else: else:
dev_deps = [] dev_deps = []
tox.reporter.verbosity1( logger.info("Env does not install development dependencies, skipping")
f"{constants.REPORTER_PREFIX} Env does not install development dependencies, skipping"
)
env_deps = utilities.find_additional_deps( env_deps = utilities.find_additional_deps(
packages, virtualenv, poetry, venv.envconfig.locked_deps packages, virtualenv, poetry, venv.envconfig.locked_deps
) )
tox.reporter.verbosity1( logger.info(
f"{constants.REPORTER_PREFIX} Identified {len(env_deps)} environment dependencies to install to env" f"Identified {len(env_deps)} environment dependencies to install to env"
) )
if not venv.envconfig.skip_install and not venv.envconfig.config.skipsdist: if not venv.envconfig.skip_install and not venv.envconfig.config.skipsdist:
project_deps = utilities.find_project_deps( project_deps = utilities.find_project_deps(
packages, virtualenv, poetry, venv.envconfig.extras packages, virtualenv, poetry, venv.envconfig.extras
) )
tox.reporter.verbosity1( logger.info(
f"{constants.REPORTER_PREFIX} Identified {len(project_deps)} project dependencies to install to env" f"Identified {len(project_deps)} project dependencies to install to env"
) )
else: else:
project_deps = [] project_deps = []
tox.reporter.verbosity1( logger.info("Env does not install project package, skipping")
f"{constants.REPORTER_PREFIX} Env does not install project package, skipping"
)
except exceptions.ToxPoetryInstallerException as err: except exceptions.ToxPoetryInstallerException as err:
venv.status = err.__class__.__name__ venv.status = err.__class__.__name__
tox.reporter.error(f"{constants.REPORTER_PREFIX} {err}") logger.error(str(err))
return False return False
except Exception as err: except Exception as err:
venv.status = "InternalError" venv.status = "InternalError"
tox.reporter.error(f"{constants.REPORTER_PREFIX} Internal plugin error: {err}") logger.error(f"Internal plugin error: {err}")
raise err raise err
dependencies = dev_deps + env_deps + project_deps dependencies = dev_deps + env_deps + project_deps

View File

@ -9,11 +9,10 @@ from typing import Optional
from typing import Sequence from typing import Sequence
from typing import Set from typing import Set
import tox
from poetry.core.packages import Package as PoetryPackage from poetry.core.packages import Package as PoetryPackage
from tox.venv import VirtualEnv as ToxVirtualEnv from tox.venv import VirtualEnv as ToxVirtualEnv
from tox_poetry_installer import constants from tox_poetry_installer import logger
from tox_poetry_installer import utilities from tox_poetry_installer import utilities
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
@ -36,8 +35,8 @@ def install(
""" """
from tox_poetry_installer import _poetry from tox_poetry_installer import _poetry
tox.reporter.verbosity1( logger.info(
f"{constants.REPORTER_PREFIX} Installing {len(packages)} packages to environment at {venv.envconfig.envdir}" f"Installing {len(packages)} packages to environment at {venv.envconfig.envdir}"
) )
pip = _poetry.PipInstaller( pip = _poetry.PipInstaller(
@ -68,14 +67,8 @@ def install(
for dependency in packages: for dependency in packages:
if dependency not in installed: if dependency not in installed:
installed.add(dependency) installed.add(dependency)
tox.reporter.verbosity2( logger.debug(f"Installing {dependency}")
f"{constants.REPORTER_PREFIX} Installing {dependency}"
)
executor(pip.install, dependency) executor(pip.install, dependency)
else: else:
tox.reporter.verbosity2( logger.debug(f"Skipping {dependency}, already installed")
f"{constants.REPORTER_PREFIX} Skipping {dependency}, already installed" logger.debug("Waiting for installs to finish...")
)
tox.reporter.verbosity2(
f"{constants.REPORTER_PREFIX} Waiting for installs to finish..."
)

View File

@ -0,0 +1,29 @@
"""Logging wrappers to reduce duplication elsewhere
Calling ``tox.reporter.something()`` and having to format a string with the prefix
gets really old fast, but more importantly it also makes the flow of the main code
more difficult to follow because of the added complexity.
"""
import tox
from tox_poetry_installer import constants
def error(message: str):
"""Wrapper around :func:`tox.reporter.error`"""
tox.reporter.error(f"{constants.REPORTER_PREFIX} {message}")
def warning(message: str):
"""Wrapper around :func:`tox.reporter.warning`"""
tox.reporter.warning(f"{constants.REPORTER_PREFIX} {message}")
def info(message: str):
"""Wrapper around :func:`tox.reporter.verbosity1`"""
tox.reporter.verbosity1(f"{constants.REPORTER_PREFIX} {message}")
def debug(message: str):
"""Wrapper around :func:`tox.reporter.verbosity2`"""
tox.reporter.verbosity2(f"{constants.REPORTER_PREFIX} {message}")

View File

@ -9,7 +9,6 @@ from typing import Sequence
from typing import Set from typing import Set
from typing import Union from typing import Union
import tox
from poetry.core.packages import Dependency as PoetryDependency from poetry.core.packages import Dependency as PoetryDependency
from poetry.core.packages import Package as PoetryPackage from poetry.core.packages import Package as PoetryPackage
from tox.action import Action as ToxAction from tox.action import Action as ToxAction
@ -17,6 +16,7 @@ from tox.venv import VirtualEnv as ToxVirtualEnv
from tox_poetry_installer import constants from tox_poetry_installer import constants
from tox_poetry_installer import exceptions from tox_poetry_installer import exceptions
from tox_poetry_installer import logger
from tox_poetry_installer.datatypes import PackageMap from tox_poetry_installer.datatypes import PackageMap
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
@ -101,14 +101,10 @@ def identify_transients(
for requirement in packages[transient.name].requires: for requirement in packages[transient.name].requires:
if requirement.name not in searched: if requirement.name not in searched:
_deps_of_dep(requirement) _deps_of_dep(requirement)
tox.reporter.verbosity2( logger.debug(f"Including {transient} for installation")
f"{constants.REPORTER_PREFIX} Including {transient} for installation"
)
transients.append(packages[transient.name]) transients.append(packages[transient.name])
else: else:
tox.reporter.verbosity2( logger.debug(f"Skipping {transient}: package requires {transient.marker}")
f"{constants.REPORTER_PREFIX} Skipping {transient}: package requires {transient.marker}"
)
try: try:
if isinstance(dep, str): if isinstance(dep, str):
@ -119,18 +115,14 @@ def identify_transients(
dep_name = err.args[0] dep_name = err.args[0]
if dep_name in _poetry.Provider.UNSAFE_PACKAGES: if dep_name in _poetry.Provider.UNSAFE_PACKAGES:
tox.reporter.warning( logger.warning(
f"{constants.REPORTER_PREFIX} Installing package '{dep_name}' using Poetry is not supported and will be skipped" f"Installing package '{dep_name}' using Poetry is not supported and will be skipped"
)
tox.reporter.verbosity2(
f"{constants.REPORTER_PREFIX} Skipping {dep_name}: designated unsafe by Poetry"
) )
logger.debug(f"Skipping {dep_name}: designated unsafe by Poetry")
return [] return []
if dep_name in allow_missing: if dep_name in allow_missing:
tox.reporter.verbosity2( logger.debug(f"Skipping {dep_name}: package is allowed to be unlocked")
f"{constants.REPORTER_PREFIX} Skipping {dep_name}: package is allowed to be unlocked"
)
return [] return []
if any( if any(
@ -171,9 +163,7 @@ def find_project_deps(
extra_deps: List[PoetryPackage] = [] extra_deps: List[PoetryPackage] = []
for extra in extras: for extra in extras:
tox.reporter.verbosity1( logger.info(f"Processing project extra '{extra}'")
f"{constants.REPORTER_PREFIX} Processing project extra '{extra}'"
)
try: try:
extra_deps += [packages[item.name] for item in poetry.package.extras[extra]] extra_deps += [packages[item.name] for item in poetry.package.extras[extra]]
except KeyError: except KeyError: