-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reworked poly check command (#60)
* wip: poly check v2 * fix: accidental debug print statement * fix(parse imports): extract node names when parsing 'from x import x, y z' * docs: add docs about the poetry poly check command * bump version to 1.4.0 * docs: fix typo in README
- Loading branch information
1 parent
23a8021
commit 77fa7e4
Showing
11 changed files
with
174 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Set, Union | ||
|
||
|
||
def only_brick_imports(imports: Set[str], top_ns: str) -> Set[str]: | ||
return {i for i in imports if i.startswith(top_ns)} | ||
|
||
|
||
def only_bricks(import_data: dict, top_ns: str) -> dict: | ||
return {k: only_brick_imports(v, top_ns) for k, v in import_data.items()} | ||
|
||
|
||
def brick_import_to_name(brick_import: str) -> Union[str, None]: | ||
parts = brick_import.split(".") | ||
|
||
return parts[1] if len(parts) > 1 else None | ||
|
||
|
||
def only_brick_name(brick_imports: Set[str]) -> Set: | ||
res = {brick_import_to_name(i) for i in brick_imports} | ||
|
||
return {i for i in res if i} | ||
|
||
|
||
def only_brick_names(import_data: dict) -> dict: | ||
return {k: only_brick_name(v) for k, v in import_data.items() if v} | ||
|
||
|
||
def exclude_empty(import_data: dict) -> dict: | ||
return {k: v for k, v in import_data.items() if v} | ||
|
||
|
||
def extract_brick_imports(all_imports: dict, top_ns) -> dict: | ||
with_only_bricks = only_bricks(all_imports, top_ns) | ||
with_only_brick_names = only_brick_names(with_only_bricks) | ||
|
||
return exclude_empty(with_only_brick_names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
from polylith.libs.grouping import get_third_party_imports | ||
from polylith.libs import report | ||
from polylith.libs.grouping import ( | ||
extract_third_party_imports, | ||
fetch_all_imports, | ||
get_third_party_imports, | ||
) | ||
|
||
__all__ = ["get_third_party_imports", "report"] | ||
__all__ = [ | ||
"report", | ||
"extract_third_party_imports", | ||
"fetch_all_imports", | ||
"get_third_party_imports", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,68 @@ | ||
from pathlib import Path | ||
from typing import List, Set, Union | ||
|
||
from poetry.console.commands.command import Command | ||
from polylith import check, project, repo | ||
from poetry.factory import Factory | ||
from polylith import check, info, project, repo, workspace | ||
|
||
|
||
def get_projects_data(root: Path, ns: str) -> List[dict]: | ||
bases = info.get_bases(root, ns) | ||
components = info.get_components(root, ns) | ||
|
||
return info.get_bricks_in_projects(root, components, bases, ns) | ||
|
||
|
||
class CheckCommand(Command): | ||
name = "poly check" | ||
description = "Validates the <comment>Polylith</> workspace." | ||
|
||
def find_third_party_libs(self, path: Union[Path, None]) -> Set: | ||
project_poetry = Factory().create_poetry(path) if path else self.poetry | ||
|
||
if not project_poetry.locker.is_locked(): | ||
raise ValueError("poetry.lock not found. Run `poetry lock` to create it.") | ||
|
||
packages = project_poetry.locker.locked_repository().packages | ||
|
||
return {p.name for p in packages} | ||
|
||
def print_report(self, root: Path, ns: str, project_data: dict) -> bool: | ||
path = project_data["path"] | ||
name = project_data["name"] | ||
|
||
try: | ||
third_party_libs = self.find_third_party_libs(path) | ||
return check.report.print_report(root, ns, project_data, third_party_libs) | ||
except ValueError as e: | ||
self.line_error(f"{name}: <error>{e}</error>") | ||
return False | ||
|
||
def handle(self) -> int: | ||
root = repo.find_workspace_root(Path.cwd()) | ||
|
||
if not root: | ||
raise ValueError( | ||
"Didn't find the workspace root. Expected to find a workspace.toml file." | ||
) | ||
|
||
projects = project.get_project_names_and_paths(root) | ||
ns = workspace.parser.get_namespace_from_config(root) | ||
|
||
projects_data = get_projects_data(root, ns) | ||
|
||
if self.option("directory"): | ||
project_name = project.get_project_name(self.poetry.pyproject.data) | ||
|
||
data = next((p for p in projects_data if p["name"] == project_name), None) | ||
|
||
if not data: | ||
raise ValueError(f"Didn't find project in {self.option('directory')}") | ||
|
||
res = self.print_report(root, ns, data) | ||
result_code = 0 if res else 1 | ||
else: | ||
results = {self.print_report(root, ns, data) for data in projects_data} | ||
|
||
res = [check.report.run(proj) for proj in projects] | ||
result_code = 0 if all(results) else 1 | ||
|
||
return 0 if all(res) else 1 | ||
return result_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters