-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make linter aware of its own requirements (#4159)
- Loading branch information
Showing
10 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Utilities for checking python packages requirements.""" | ||
|
||
import importlib_metadata | ||
from packaging.requirements import Requirement | ||
from packaging.specifiers import SpecifierSet | ||
from packaging.version import Version | ||
|
||
|
||
class Reqs(dict[str, SpecifierSet]): | ||
"""Utility class for working with package dependencies.""" | ||
|
||
reqs: dict[str, SpecifierSet] | ||
|
||
def __init__(self, name: str = "ansible-lint") -> None: | ||
"""Load linter metadata requirements.""" | ||
for req_str in importlib_metadata.metadata(name).json["requires_dist"]: | ||
req = Requirement(req_str) | ||
if req.name: | ||
self[req.name] = req.specifier | ||
|
||
def matches(self, req_name: str, req_version: str | Version) -> bool: | ||
"""Verify if given version is matching current metadata dependencies.""" | ||
if req_name not in self: | ||
return False | ||
return all( | ||
specifier.contains(str(req_version), prereleases=True) | ||
for specifier in self[req_name] | ||
) |
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,18 @@ | ||
"""Tests requirements module.""" | ||
|
||
from ansible_compat.runtime import Runtime | ||
|
||
from ansiblelint.requirements import Reqs | ||
|
||
|
||
def test_reqs() -> None: | ||
"""Performs basic testing of Reqs class.""" | ||
reqs = Reqs() | ||
runtime = Runtime() | ||
assert "ansible-core" in reqs | ||
# checks that this ansible core version is not supported: | ||
assert reqs.matches("ansible-core", "0.0") is False | ||
# assert that invalid package name | ||
assert reqs.matches("this-package-does-not-exist", "0.0") is False | ||
# check the current ansible core version is supported: | ||
assert reqs.matches("ansible-core", runtime.version) |
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