-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #4020
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import importlib_metadata | ||
from packaging.requirements import Requirement | ||
from packaging.specifiers import SpecifierSet | ||
|
||
|
||
class Req: | ||
"""Utility class for working with package dependencies.""" | ||
|
||
reqs: dict[str, SpecifierSet] | ||
|
||
def __init__(self) -> None: | ||
"""Load linter metadata requirements.""" | ||
self.reqs = {} | ||
for req_str in importlib_metadata.metadata("ansible-lint").json[ | ||
"requires_dist" | ||
]: | ||
req = Requirement(req_str) | ||
# print(a.name) | ||
if req.name: | ||
# breakpoint() | ||
self.reqs[req.name] = req.specifier | ||
# breakpoint() | ||
|
||
def contains(self, req_name: str, req_version: str) -> bool: | ||
"""Verify if given version is matching current metadata depedendencies.""" | ||
if req_name not in self.reqs: | ||
return False | ||
return all(specifier.contains(req_version) for specifier in self.reqs[req_name]) | ||
|
||
|
||
req = Req() | ||
print(req.contains("ansible-core", "2.2")) | ||
# breakpoint() | ||
|
||
# def _process_requires_dist( | ||
# self, | ||
# value: list[str], | ||
# ) -> list[requirements.Requirement]: | ||
# reqs = [] | ||
# try: | ||
# for req in value: | ||
# reqs.append(requirements.Requirement(req)) | ||
# except requirements.InvalidRequirement as exc: | ||
# raise self._invalid_metadata(f"{req!r} is invalid for {{field}}", cause=exc) | ||
# else: | ||
# return reqs |
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,25 @@ | ||
"""Utilities for checking python packages requirements.""" | ||
|
||
import importlib_metadata | ||
from packaging.requirements import Requirement | ||
from packaging.specifiers import SpecifierSet | ||
|
||
|
||
class Reqs: | ||
"""Utility class for working with package dependencies.""" | ||
|
||
reqs: dict[str, SpecifierSet] | ||
|
||
def __init__(self, name: str = "ansible-lint") -> None: | ||
"""Load linter metadata requirements.""" | ||
self.reqs = {} | ||
for req_str in importlib_metadata.metadata(name).json["requires_dist"]: | ||
req = Requirement(req_str) | ||
if req.name: | ||
self.reqs[req.name] = req.specifier | ||
|
||
def contains(self, req_name: str, req_version: str) -> bool: | ||
"""Verify if given version is matching current metadata depedendencies.""" | ||
if req_name not in self.reqs: | ||
return False | ||
return all(specifier.contains(req_version) for specifier in self.reqs[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