Skip to content

Commit

Permalink
Only return the latest version for major+minor python versions instea…
Browse files Browse the repository at this point in the history
…d of returning all versions. Returning all versions is useless in the vast majority of cases.

Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso committed Dec 16, 2023
1 parent 92d2079 commit 2103c47
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Modern rez-pip implementation. Very WIP.
* [x] Create rez package
* [x] Copy distribution files to rez package.
* [ ] Make it available as a rez plugin/sub-command
* [ ] Discover Python package using rez and use that when available. I think it's still fine to support non-rezified Python interpreters though.
* [x] Discover Python package using rez and use that when available. I think it's still fine to support non-rezified Python interpreters though.
* [ ] Only download+convert package if it's not already in the rez repositories.
* etc
* [x] Accept multiple package names as input
Expand Down
18 changes: 16 additions & 2 deletions src/rez_pip/rez.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import typing
import logging
import pathlib
import itertools

if sys.version_info >= (3, 10):
import importlib.metadata as importlib_metadata
Expand Down Expand Up @@ -233,15 +234,28 @@ def getPythonExecutables(
:param packageFamily: Name of the rez package family for the python package. This allows ot support PyPy, etc.
:returns: Dict where the keys are the python versions and values are abolute paths to executables.
"""
packages = sorted(
all_packages = sorted(
rez.packages.iter_packages(
packageFamily, range_=range_ if range_ != "latest" else None
),
key=lambda x: x.version,
)

packages: typing.List[rez.packages.Package]
if range_ == "latest":
packages = [packages[-1]]
packages = [list(all_packages)[-1]]
else:
# Get the latest x.x (major+minor) and ignore anything else.
# We don't want to return 3.7.8 AND 3.7.9 for example. It doesn't
# make sense. We only need 3.7.x.
groups = [
list(group)
for _, group in itertools.groupby(
all_packages, key=lambda x: x.version.as_tuple()[:2]
)
]
# Note that "pkgs" is already in the right order since all_packages is sorted.
packages = [pkgs[-1] for pkgs in groups]

pythons: typing.Dict[str, pathlib.Path] = {}
for package in packages:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_rez.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,23 @@ def test_convertMetadata(
{"1.0.0": pathlib.Path("/path/python1.0")},
id="with-range-less-than-2",
),
pytest.param(
["3.9.0", "3.7.1", "3.7.6", "3.7.5", "2.7.4", "2.7.9"],
None,
[
# Due to how the test is structured, these have to be in the same
# order as the output.
"python2.7",
"python3.7",
"python3.9",
],
{
"2.7.9": pathlib.Path("/path/python2.7"),
"3.7.6": pathlib.Path("/path/python3.7"),
"3.9.0": pathlib.Path("/path/python3.9"),
},
id="select-latest-major+minor",
),
],
)
def test_getPythonExecutables(
Expand Down

0 comments on commit 2103c47

Please sign in to comment.