diff --git a/components/polylith/commands/check.py b/components/polylith/commands/check.py index 9dc6a6f3..c66506b5 100644 --- a/components/polylith/commands/check.py +++ b/components/polylith/commands/check.py @@ -1,26 +1,14 @@ -import importlib.metadata from pathlib import Path from typing import Set -from polylith import alias, check, distributions +from polylith import check, distributions -def collect_known_aliases_and_sub_dependencies( - project_data: dict, options: dict -) -> Set[str]: - third_party_libs = project_data["deps"] - library_alias = options["alias"] - - dists = list(importlib.metadata.distributions()) - dist_packages = distributions.distributions_packages(dists) - sub_packages = distributions.distributions_sub_packages(dists) - custom_aliases = alias.parse(library_alias) - - a = alias.pick(dist_packages, third_party_libs) - b = alias.pick(sub_packages, third_party_libs) - c = alias.pick(custom_aliases, third_party_libs) +def collect_known_aliases(project_data: dict, options: dict) -> Set[str]: + deps = project_data["deps"] + library_alias = options["alias"] - return third_party_libs.union(a, b, c) + return distributions.known_aliases_and_sub_dependencies(deps, library_alias) def run(root: Path, ns: str, project_data: dict, options: dict) -> bool: @@ -31,7 +19,7 @@ def run(root: Path, ns: str, project_data: dict, options: dict) -> bool: name = project_data["name"] collected_imports = check.report.collect_all_imports(root, ns, project_data) - collected_libs = collect_known_aliases_and_sub_dependencies(project_data, options) + collected_libs = collect_known_aliases(project_data, options) details = check.report.create_report( project_data, diff --git a/components/polylith/commands/libs.py b/components/polylith/commands/libs.py index 8f4bcb7a..53d019fd 100644 --- a/components/polylith/commands/libs.py +++ b/components/polylith/commands/libs.py @@ -1,6 +1,6 @@ from pathlib import Path -from polylith import alias +from polylith import distributions from polylith.libs import report @@ -9,17 +9,14 @@ def run(root: Path, ns: str, project_data: dict, options: dict) -> bool: library_alias = options["alias"] name = project_data["name"] - third_party_libs = project_data["deps"] + deps = project_data["deps"] brick_imports = report.get_third_party_imports(root, ns, project_data) report.print_libs_summary(brick_imports, project_data) report.print_libs_in_bricks(brick_imports) - library_aliases = alias.parse(library_alias) - extra = alias.pick(library_aliases, third_party_libs) - - libs = third_party_libs.union(extra) + libs = distributions.known_aliases_and_sub_dependencies(deps, library_alias) return report.print_missing_installed_libs( brick_imports, diff --git a/components/polylith/distributions/__init__.py b/components/polylith/distributions/__init__.py index 959031e4..b4b4b607 100644 --- a/components/polylith/distributions/__init__.py +++ b/components/polylith/distributions/__init__.py @@ -1,7 +1,13 @@ +from polylith.distributions.collect import known_aliases_and_sub_dependencies from polylith.distributions.core import ( distributions_packages, distributions_sub_packages, get_distributions, ) -__all__ = ["distributions_packages", "distributions_sub_packages", "get_distributions"] +__all__ = [ + "distributions_packages", + "distributions_sub_packages", + "get_distributions", + "known_aliases_and_sub_dependencies", +] diff --git a/components/polylith/distributions/collect.py b/components/polylith/distributions/collect.py new file mode 100644 index 00000000..8f813b1d --- /dev/null +++ b/components/polylith/distributions/collect.py @@ -0,0 +1,33 @@ +import importlib.metadata +from typing import Set + +from polylith import alias +from polylith.distributions.core import ( + distributions_packages, + distributions_sub_packages, +) + + +def known_aliases_and_sub_dependencies( + deps: dict, library_alias: list +) -> Set[str]: + """Collect known aliases (packages) for third-party libraries. + + When the library origin is not from a lock-file: + collect sub-dependencies for each library, and append to the result. + """ + + third_party_libs = deps["items"] + lock_file = str.endswith(deps["source"], ".lock") + + dists = list(importlib.metadata.distributions()) + + dist_packages = distributions_packages(dists) + custom_aliases = alias.parse(library_alias) + sub_deps = distributions_sub_packages(dists) if not lock_file else {} + + a = alias.pick(dist_packages, third_party_libs) + b = alias.pick(custom_aliases, third_party_libs) + c = alias.pick(sub_deps, third_party_libs) + + return third_party_libs.union(a, b, c) diff --git a/components/polylith/poetry/commands/check.py b/components/polylith/poetry/commands/check.py index bb48a2c3..f503ca27 100644 --- a/components/polylith/poetry/commands/check.py +++ b/components/polylith/poetry/commands/check.py @@ -39,7 +39,10 @@ def print_report(self, root: Path, ns: str, project_data: dict) -> bool: try: third_party_libs = internals.find_third_party_libs(self.poetry, path) - merged = {**project_data, **{"deps": third_party_libs}} + merged = { + **project_data, + **{"deps": {"items": third_party_libs, "source": "poetry.lock"}}, + } return commands.check.run(root, ns, merged, options) except ValueError as e: diff --git a/components/polylith/poetry/commands/libs.py b/components/polylith/poetry/commands/libs.py index c9db0034..97ba70e5 100644 --- a/components/polylith/poetry/commands/libs.py +++ b/components/polylith/poetry/commands/libs.py @@ -20,7 +20,10 @@ def print_report(self, root: Path, ns: str, data: dict) -> bool: try: third_party_libs = find_third_party_libs(self.poetry, path) - merged = {**data, **{"deps": third_party_libs}} + merged = { + **data, + **{"deps": {"items": third_party_libs, "source": "poetry.lock"}}, + } return commands.libs.run(root, ns, merged, options) except ValueError as e: diff --git a/components/polylith/project/get.py b/components/polylith/project/get.py index 2a48a8cf..c9102f7f 100644 --- a/components/polylith/project/get.py +++ b/components/polylith/project/get.py @@ -1,7 +1,7 @@ import re from functools import lru_cache from pathlib import Path -from typing import List, Set +from typing import List import tomlkit from polylith import repo, workspace @@ -32,15 +32,17 @@ def get_project_name(data) -> str: return data["tool"]["poetry"]["name"] -def get_project_dependencies(data) -> Set: +def get_project_dependencies(data) -> dict: if repo.is_poetry(data): deps = data["tool"]["poetry"].get("dependencies", []) - return set(deps.keys()) + items = set(deps.keys()) else: deps = data["project"].get("dependencies", []) - return {re.split(r"[\^~=!<>]", dep)[0] for dep in deps} + items = {re.split(r"[\^~=!<>]", dep)[0] for dep in deps} + + return {"items": items, "source": repo.default_toml} @lru_cache diff --git a/projects/poetry_polylith_plugin/pyproject.toml b/projects/poetry_polylith_plugin/pyproject.toml index b23464a3..f53f5e21 100644 --- a/projects/poetry_polylith_plugin/pyproject.toml +++ b/projects/poetry_polylith_plugin/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "poetry-polylith-plugin" -version = "1.14.0" +version = "1.14.1" description = "A Poetry plugin that adds tooling support for the Polylith Architecture" authors = ["David Vujic"] homepage = "https://davidvujic.github.io/python-polylith-docs/" diff --git a/test/components/polylith/commands/test_check.py b/test/components/polylith/commands/test_check.py index ecae22f7..10d72649 100644 --- a/test/components/polylith/commands/test_check.py +++ b/test/components/polylith/commands/test_check.py @@ -2,10 +2,12 @@ def test_collect_known_aliases_and_sub_dependencies(): - fake_project_data = {"deps": {"typer", "hello-world-library"}} + fake_project_data = { + "deps": {"items": {"typer", "hello-world-library"}, "source": "unit-test"} + } fake_options = {"alias": ["hello-world-library=hello"]} - res = check.collect_known_aliases_and_sub_dependencies(fake_project_data, fake_options) + res = check.collect_known_aliases(fake_project_data, fake_options) assert "typer" in res assert "typing-extensions" in res