-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from packaging.requirements import Requirement | ||
from packaging.utils import canonicalize_name | ||
from packaging.version import Version | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Iterable | ||
|
||
from packaging.specifiers import Specifier | ||
|
||
__all__ = ["get_min_requires"] | ||
|
||
|
||
def __dir__() -> list[str]: | ||
return __all__ | ||
|
||
|
||
def get_min_requires(package: str, reqlist: Iterable[str]) -> Version | None: | ||
""" | ||
Read the build requirements from a pyproject.toml file | ||
and return the minimum version required for the given package. | ||
Returns None if no minimum version is discovered. | ||
""" | ||
|
||
norm_package = canonicalize_name(package) | ||
|
||
requires = [Requirement(req) for req in reqlist] | ||
|
||
for req in requires: | ||
if canonicalize_name(req.name) == norm_package: | ||
specset = req.specifier | ||
versions = (min_from_spec(v) for v in specset) | ||
return min((v for v in versions if v is not None), default=None) | ||
|
||
return None | ||
|
||
|
||
def min_from_spec(spec: Specifier) -> Version | None: | ||
""" | ||
Return the minimum version from a specifier. | ||
Returns None if no minimum version is discovered. | ||
">" technically doesn't include the minimum, but any | ||
larger version is acceptable, so it is treated as the | ||
minimum. | ||
""" | ||
|
||
if spec.operator in {">=", ">", "==", "~="}: | ||
return Version(spec.version) | ||
return None |
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,27 @@ | ||
import pytest | ||
from packaging.version import Version | ||
|
||
from scikit_build_core.settings.auto_requires import get_min_requires | ||
|
||
|
||
def test_auto_requires_pkg_no_spec(): | ||
reqlist = ["scikit-build-core"] | ||
|
||
assert get_min_requires("scikit-build-core", reqlist) is None | ||
assert get_min_requires("other", reqlist) is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("spec", "version"), | ||
[ | ||
("==1.0", Version("1.0")), | ||
(">=1.0", Version("1.0")), | ||
(">1.0", Version("1.0")), | ||
("~=1.0", Version("1.0")), | ||
(">=0.3,<0.4", Version("0.3")), | ||
("", None), | ||
], | ||
) | ||
def test_auto_requires_pkg_version(spec: str, version: Version): | ||
reqlist = [f"scikit_build_core{spec}"] | ||
assert get_min_requires("scikit-build-core", reqlist) == 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