Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nox task to verify dependency declarations #236

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
13e9f39
add dependancy_check.py + test
Jannis-Mittenzwei Aug 15, 2024
d96fb65
fix dependancy_check.py
Jannis-Mittenzwei Aug 20, 2024
da4854e
add output for problematic dependencies
Jannis-Mittenzwei Aug 20, 2024
767e38c
output changed
Jannis-Mittenzwei Aug 21, 2024
1ffe47d
add nox task + test
Jannis-Mittenzwei Sep 3, 2024
4e13573
fix f-string new line problem
Jannis-Mittenzwei Sep 3, 2024
9c33b61
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 3, 2024
6d14d58
new version
Jannis-Mittenzwei Sep 3, 2024
21a7aff
new version
Jannis-Mittenzwei Sep 3, 2024
b7c04a8
Merge remote-tracking branch 'origin/feature/#233-add-nox-task-to-ver…
Jannis-Mittenzwei Sep 3, 2024
7b88101
new version
Jannis-Mittenzwei Sep 3, 2024
5f41253
resolves the conversations
Jannis-Mittenzwei Sep 11, 2024
06d6594
fix type error
Jannis-Mittenzwei Sep 17, 2024
1757ed0
add capsys to dependencies check test
Jannis-Mittenzwei Sep 17, 2024
f48801b
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 24, 2024
2a11ca0
resolves the conversations
Jannis-Mittenzwei Sep 24, 2024
836ab35
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 24, 2024
5414dd7
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 25, 2024
cbd41da
update changelog
Jannis-Mittenzwei Sep 25, 2024
44d6cd5
resolves the conversations
Jannis-Mittenzwei Sep 25, 2024
ca17f92
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions exasol/toolbox/nox/_dependencies_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import tomlkit
import sys
from pathlib import Path
import nox
from nox import Session
from noxconfig import PROJECT_CONFIG
from typing import List


FILTERS = ['url', 'git', 'path']
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved


@nox.session(name="dependencies-check", python=False)
def dependency_check(session: Session) -> None:
file = Path(PROJECT_CONFIG.root, "pyproject.toml")
output = _dependencies_check(file.read_text())
print("\033[31m"+output) if output else print("\033[32m"+"Success: no wrong dependencies found")
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
sys.exit(0 if not output else 1)


def _source_filter(version, filters) -> bool:
for f in filters:
if f in version:
return True
return False


def extract_dependencies(section, filters) -> List[str]:
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
dependencies = []
for name, version in section.items():
if _source_filter(version, filters):
dependencies.append(f"{name} = {version}")
return dependencies


def _dependencies_check(string: str) -> str:
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
toml = tomlkit.loads(string)
group_dependencies = {}

poetry = toml.get("tool", {}).get("poetry", {})

dependencies = extract_dependencies(poetry.get("dependencies", {}), FILTERS)

dev_section = poetry.get("dev", {}).get("dependencies", {})
dev_dependencies = extract_dependencies(dev_section, FILTERS)

group_section = poetry.get("group", {})
for group, content in group_section.items():
group_deps = extract_dependencies(content.get("dependencies", {}), FILTERS)
if group_deps:
group_key = f'[tool.poetry.group.{group}.dependencies]'
group_dependencies[group_key] = group_deps

total_count = len(dependencies) + len(dev_dependencies) + sum(len(deps) for deps in group_dependencies.values())
if total_count > 0:
suffix = "y" if total_count == 1 else "ies"
output = [f"{total_count} illegal dependenc{suffix}:\n"]

if dependencies:
output.append(f"\n[tool.poetry.dependencies]\n" + "\n".join(dependencies) + "\n")
if dev_dependencies:
output.append(f"\n[tool.poetry.dev.dependencies]\n" + "\n".join(dev_dependencies) + "\n")
for key, value in group_dependencies.items():
output.append(f"\n{key}\n" + "\n".join(value) + "\n")

return "".join(output)
return ""


if __name__ == "__main__":
print(_dependencies_check(Path("pyproject.toml").read_text()))
3 changes: 3 additions & 0 deletions exasol/toolbox/nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
integration_tests,
unit_tests,
)
from exasol.toolbox.nox._dependencies_check import (
dependency_check
)
from noxconfig import PROJECT_CONFIG


Expand Down
80 changes: 80 additions & 0 deletions test/unit/dependencies_check_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from tomlkit import loads
import pytest
from exasol.toolbox.nox._dependencies_check import (
_source_filter,
_dependencies_check
)


@pytest.mark.parametrize(
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
"filters,source,expected",
[
(
['url', 'git', 'path'],
"""example-url = {url = "https://example.com/my-package-0.1.0.tar.gz"}""",
'url'),
(
['url', 'git', 'path'],
"""example-git = {git = "[email protected]:requests/requests.git"}""",
'git'),
(
['url', 'git', 'path'],
"""example-path = {path = "../my-package/dist/my-package-0.1.0.tar.gz"}""",
'path'),
(
['url', 'git', 'path'],
"""example-url = {platform = "darwin", url = "https://example.com/my-package-0.1.0.tar.gz"}""",
'url'),
(
['url', 'git', 'path'],
"""example = "^2.31.0.6" """,
None),
(
['url', 'git', 'path'],
"""python = ">=3.8.0,<4.0" """,
None
)
]
)
def test_dependencies_check(filters, source, expected):
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
for _, version in loads(source).items():
assert _source_filter(version, filter) == expected


@pytest.mark.parametrize(
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
"toml, expected",
[
(
"""[tool.poetry.dependencies]
python = "^3.8"
example-url = {url = "https://example.com/my-package-0.1.0.tar.gz"}

[tool.poetry.dev.dependencies]
nox = ">=2022.8.7"

[tool.poetry.group.test.dependencies]
sphinx = ">=5.3,<8"
example-git = {git = "[email protected]:requests/requests.git"}

[tool.poetry.group.dev.dependencies]
pytest = ">=7.2.2,<9"
example-path1 = {path = "../my-package/dist/my-package-0.1.0.tar.gz"}
example-path2 = {path = "../my-package/dist/my-package-0.2.0.tar.gz"}
""",
"""4 illegal dependencies:

[tool.poetry.dependencies]
example-url = {'url': 'https://example.com/my-package-0.1.0.tar.gz'}

[tool.poetry.group.test.dependencies]
example-git = {'git': '[email protected]:requests/requests.git'}

[tool.poetry.group.dev.dependencies]
example-path1 = {'path': '../my-package/dist/my-package-0.1.0.tar.gz'}
example-path2 = {'path': '../my-package/dist/my-package-0.2.0.tar.gz'}
"""
),
]
)
def test_dependencies_check(toml, expected):
assert _dependencies_check(toml) == expected
Loading