summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/requirements.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ansiblelint/requirements.py')
-rw-r--r--src/ansiblelint/requirements.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/ansiblelint/requirements.py b/src/ansiblelint/requirements.py
new file mode 100644
index 0000000..96381b9
--- /dev/null
+++ b/src/ansiblelint/requirements.py
@@ -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]
+ )