diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3b995d2f1..25658fa14 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -47,6 +47,7 @@ repos: "requests", "urllib3", "types-beautifulsoup4", + "types-PyYAML", "types-requests", "types-tabulate", "types-urllib3", diff --git a/requirements-dev.txt b/requirements-dev.txt index 3ab2be954..9caa0df30 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,5 +6,6 @@ pytest-retry # `pytest-xdist` is a plugin that provides the `--numprocesses` flag, # allowing us to run `pytest` tests in parallel pytest-xdist +PyYAML requests tabulate diff --git a/tests/package_helper.py b/tests/package_helper.py index 2fe85f534..5f2f636c7 100644 --- a/tests/package_helper.py +++ b/tests/package_helper.py @@ -22,13 +22,13 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -import json import logging import re from collections import defaultdict from itertools import chain from typing import Any, Optional +import yaml from docker.models.containers import Container from tabulate import tabulate @@ -61,7 +61,7 @@ def start_container(container: TrackedContainer) -> Container: @staticmethod def _conda_export_command(from_history: bool) -> list[str]: """Return the mamba export command with or without history""" - cmd = ["mamba", "env", "export", "-n", "base", "--json", "--no-builds"] + cmd = ["mamba", "env", "export", "--no-build"] if from_history: cmd.append("--from-history") return cmd @@ -70,7 +70,7 @@ def installed_packages(self) -> dict[str, set[str]]: """Return the installed packages""" if self.installed is None: LOGGER.info("Grabbing the list of installed packages ...") - self.installed = CondaPackageHelper._packages_from_json( + self.installed = CondaPackageHelper._parse_package_versions( self._execute_command( CondaPackageHelper._conda_export_command(from_history=False) ) @@ -81,7 +81,7 @@ def requested_packages(self) -> dict[str, set[str]]: """Return the requested package (i.e. `mamba install `)""" if self.requested is None: LOGGER.info("Grabbing the list of manually requested packages ...") - self.requested = CondaPackageHelper._packages_from_json( + self.requested = CondaPackageHelper._parse_package_versions( self._execute_command( CondaPackageHelper._conda_export_command(from_history=True) ) @@ -94,12 +94,12 @@ def _execute_command(self, command: list[str]) -> str: return rc.output.decode("utf-8") # type: ignore @staticmethod - def _packages_from_json(env_export: str) -> dict[str, set[str]]: + def _parse_package_versions(env_export: str) -> dict[str, set[str]]: """Extract packages and versions from the lines returned by the list of specifications""" - # dependencies = filter(lambda x: isinstance(x, str), json.loads(env_export).get("dependencies")) - dependencies = json.loads(env_export).get("dependencies") - # Filtering packages installed through pip in this case it's a dict {'pip': ['toree==0.3.0']} - # Since we only manage packages installed through mamba here + dependencies = yaml.safe_load(env_export).get("dependencies") + # Filtering packages installed through pip + # since we only manage packages installed through mamba here + # They are represented by a dict with a key 'pip' dependencies = filter(lambda x: isinstance(x, str), dependencies) packages_dict: dict[str, set[str]] = dict() for split in map(lambda x: re.split("=?=", x), dependencies):