Refactor package helper

This commit is contained in:
Ayaz Salikhov
2022-03-05 19:30:25 +00:00
parent 2e7b2562e0
commit ecb03a40e2
2 changed files with 31 additions and 43 deletions

View File

@@ -47,30 +47,31 @@ LOGGER = logging.getLogger(__name__)
# Mapping between package and module name
PACKAGE_MAPPING = {
# Python
"matplotlib-base": "matplotlib",
"beautifulsoup4": "bs4",
"scikit-learn": "sklearn",
"scikit-image": "skimage",
"spylon-kernel": "spylon_kernel",
"matplotlib-base": "matplotlib",
"pytables": "tables",
"scikit-image": "skimage",
"scikit-learn": "sklearn",
"spylon-kernel": "spylon_kernel",
# R
"randomforest": "randomForest",
"rsqlite": "DBI",
"rcurl": "RCurl",
"rodbc": "RODBC",
"rsqlite": "DBI",
}
# List of packages that cannot be tested in a standard way
EXCLUDED_PACKAGES = [
"python",
"hdf5",
"bzip2",
"ca-certificates",
"conda-forge::blas[build=openblas]",
"hdf5",
"mamba[version='<0.18']",
"openssl",
"protobuf",
"python",
"r-irkernel",
"unixodbc",
"bzip2",
"openssl",
"ca-certificates",
]
@@ -106,34 +107,29 @@ def r_package_predicate(package: str) -> bool:
return not excluded_package_predicate(package) and package.startswith("r-")
def _check_import_package(
package_helper: CondaPackageHelper, command: list[str]
) -> int:
def _check_import_package(package_helper: CondaPackageHelper, command: list[str]):
"""Generic function executing a command"""
LOGGER.debug(f"Trying to import a package with [{command}] ...")
rc = package_helper.running_container.exec_run(command)
return rc.exit_code # type: ignore
exec_result = package_helper.running_container.exec_run(command)
assert (
exec_result.exit_code == 0
), f"Import package failed, output: {exec_result.output}"
def check_import_python_package(
package_helper: CondaPackageHelper, package: str
) -> int:
def check_import_python_package(package_helper: CondaPackageHelper, package: str):
"""Try to import a Python package from the command line"""
return _check_import_package(package_helper, ["python", "-c", f"import {package}"])
_check_import_package(package_helper, ["python", "-c", f"import {package}"])
def check_import_r_package(package_helper: CondaPackageHelper, package: str) -> int:
def check_import_r_package(package_helper: CondaPackageHelper, package: str):
"""Try to import a R package from the command line"""
return _check_import_package(
package_helper, ["R", "--slave", "-e", f"library({package})"]
)
_check_import_package(package_helper, ["R", "--slave", "-e", f"library({package})"])
def _check_import_packages(
package_helper: CondaPackageHelper,
filtered_packages: Iterable[str],
check_function: Callable[[CondaPackageHelper, str], int],
max_failures: int,
packages_to_check: Iterable[str],
check_function: Callable[[CondaPackageHelper, str], None],
) -> None:
"""Test if packages can be imported
@@ -141,18 +137,14 @@ def _check_import_packages(
"""
failures = {}
LOGGER.info("Testing the import of packages ...")
for package in filtered_packages:
for package in packages_to_check:
LOGGER.info(f"Trying to import {package}")
try:
assert (
check_function(package_helper, package) == 0
), f"Package [{package}] import failed"
check_function(package_helper, package)
except AssertionError as err:
failures[package] = err
if len(failures) > max_failures:
if len(failures) > 0:
raise AssertionError(failures)
elif len(failures) > 0:
LOGGER.warning(f"Some import(s) has(have) failed: {failures}")
@pytest.fixture(scope="function")
@@ -166,12 +158,10 @@ def r_packages(packages: dict[str, set[str]]) -> Iterable[str]:
def test_r_packages(
package_helper: CondaPackageHelper, r_packages: Iterable[str], max_failures: int = 0
package_helper: CondaPackageHelper, r_packages: Iterable[str]
) -> None:
"""Test the import of specified R packages"""
_check_import_packages(
package_helper, r_packages, check_import_r_package, max_failures
)
_check_import_packages(package_helper, r_packages, check_import_r_package)
@pytest.fixture(scope="function")
@@ -183,9 +173,6 @@ def python_packages(packages: dict[str, set[str]]) -> Iterable[str]:
def test_python_packages(
package_helper: CondaPackageHelper,
python_packages: Iterable[str],
max_failures: int = 0,
) -> None:
"""Test the import of specified python packages"""
_check_import_packages(
package_helper, python_packages, check_import_python_package, max_failures
)
_check_import_packages(package_helper, python_packages, check_import_python_package)

View File

@@ -106,13 +106,14 @@ class CondaPackageHelper:
# default values
package = split[0]
version = set()
# checking if it's a proper version by testing if the first char is a digit
# This normally means we have package=version notation
if len(split) > 1:
# checking if it's a proper version by testing if the first char is a digit
if split[1][0].isdigit():
# package + version case
version = set(split[1:])
# The split was incorrect and the package shall not be splitted
else:
# The split was incorrect and the package shall not be splitted
package = f"{split[0]}={split[1]}"
packages_dict[package] = version
return packages_dict