Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move package_order to rezplugin system #1787

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
488 changes: 47 additions & 441 deletions src/rez/package_order.py

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/rez/plugin_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ class CommandPluginType(RezPluginType):
type_name = "command"


class PackageOrderPluginType(RezPluginType):
"""Support for different ordering of packages.
"""
type_name = "package_order"


plugin_manager = RezPluginManager()


Expand All @@ -462,3 +468,4 @@ class CommandPluginType(RezPluginType):
plugin_manager.register_plugin_type(PackageRepositoryPluginType)
plugin_manager.register_plugin_type(BuildProcessPluginType)
plugin_manager.register_plugin_type(CommandPluginType)
plugin_manager.register_plugin_type(PackageOrderPluginType)
1 change: 1 addition & 0 deletions src/rez/tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _eq(prefix, expected_completions):
"plugin_path"])
_eq("plugins", ["plugins",
"plugins.command",
"plugins.package_order",
"plugins.package_repository",
"plugins.build_process",
"plugins.build_system",
Expand Down
8 changes: 4 additions & 4 deletions src/rez/tests/test_packages_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_comparison(self):
self.assertFalse(inst1 != inst2) # __ne__ negative

def test_pod(self):
"""Validate we can save and load a VersionSplitPackageOrder to it's pod representation."""
"""Validate we can save and load a VersionSplitPackageOrder to its pod representation."""
self._test_pod(NullPackageOrder())

def test_sha1(self):
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_repr(self):
self.assertEqual("SortedOrder(True)", repr(SortedOrder(descending=True)))

def test_pod(self):
"""Validate we can save and load a SortedOrder to it's pod representation."""
"""Validate we can save and load a SortedOrder to its pod representation."""
self._test_pod(SortedOrder(descending=True))


Expand Down Expand Up @@ -180,7 +180,7 @@ def test_repr(self):
self.assertEqual("PerFamilyOrder(([('family1', '2.6.0')], 'None'))", repr(inst))

def test_pod(self):
"""Validate we can save and load a PerFamilyOrder to it's pod representation."""
"""Validate we can save and load a PerFamilyOrder to its pod representation."""
self._test_pod(
PerFamilyOrder(order_dict={'foo': NullPackageOrder()}, default_order=NullPackageOrder())
)
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_repr(self):
self.assertEqual("VersionSplitPackageOrder(1,2,3)", repr(inst))

def test_pod(self):
"""Validate we can save and load a VersionSplitPackageOrder to it's pod representation."""
"""Validate we can save and load a VersionSplitPackageOrder to its pod representation."""
self._test_pod(VersionSplitPackageOrder(first_version=Version("1.2.3")))


Expand Down
6 changes: 6 additions & 0 deletions src/rezplugins/package_order/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project


from rez.plugin_managers import extend_path
__path__ = extend_path(__path__, __name__)
47 changes: 47 additions & 0 deletions src/rezplugins/package_order/no_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project


from rez.package_order import PackageOrder


class NullPackageOrder(PackageOrder):
"""An orderer that does not change the order - a no op.

This orderer is useful in cases where you want to apply some default orderer
to a set of packages, but may want to explicitly NOT reorder a particular
package. You would use a :class:`NullPackageOrder` in a :class:`PerFamilyOrder` to do this.
"""
name = "no_order"

def sort_key_implementation(self, package_name, version):
# python's sort will preserve the order of items that compare equal, so
# to not change anything, we just return the same object for all...
return 0

def __str__(self):
return "{}"

def __eq__(self, other):
return type(self) is type(other)

def to_pod(self):
"""
Example (in yaml):

.. code-block:: yaml

type: no_order
packages: ["foo"]
"""
return {
"packages": self.packages,
}

@classmethod
def from_pod(cls, data):
return cls(packages=data.get("packages"))


def register_plugin():
return NullPackageOrder
125 changes: 125 additions & 0 deletions src/rezplugins/package_order/per_family.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project


from rez.package_order import PackageOrder, to_pod, from_pod


class PerFamilyOrder(PackageOrder):
"""An orderer that applies different orderers to different package families.
"""
name = "per_family"

def __init__(self, order_dict, default_order=None):
"""Create a reorderer.

Args:
order_dict (dict[str, PackageOrder]): Orderers to apply to
each package family.
default_order (PackageOrder): Orderer to apply to any packages
not specified in ``order_dict``.
"""
super().__init__(list(order_dict))
self.order_dict = order_dict.copy()
self.default_order = default_order

def reorder(self, iterable, key=None):
package_name = self._get_package_name_from_iterable(iterable, key)
if package_name is None:
return None

orderer = self.order_dict.get(package_name)
if orderer is None:
orderer = self.default_order
if orderer is None:
return None

return orderer.reorder(iterable, key)

def sort_key_implementation(self, package_name, version):
orderer = self.order_dict.get(package_name)
if orderer is None:
if self.default_order is None:
# shouldn't get here, because applies_to should protect us...
raise RuntimeError(
"package family orderer %r does not apply to package family %r",
(self, package_name))

orderer = self.default_order

return orderer.sort_key_implementation(package_name, version)

def __str__(self):
items = sorted((x[0], str(x[1])) for x in self.order_dict.items())
return str((items, str(self.default_order)))

def __eq__(self, other):
return (
type(self) is type(other)
and self.order_dict == other.order_dict
and self.default_order == other.default_order
)

def to_pod(self):
"""
Example (in yaml):

.. code-block:: yaml

type: per_family
orderers:
- packages: ['foo', 'bah']
type: version_split
first_version: '4.0.5'
- packages: ['python']
type: sorted
descending: false
default_order:
type: sorted
descending: true
"""
orderers = {}
packages = {}

# group package fams by orderer they use
for fam, orderer in self.order_dict.items():
k = id(orderer)
orderers[k] = orderer
packages.setdefault(k, set()).add(fam)

orderlist = []
for k, fams in packages.items():
orderer = orderers[k]
data = to_pod(orderer)
data["packages"] = sorted(fams)
orderlist.append(data)

result = {"orderers": orderlist}

if self.default_order is not None:
result["default_order"] = to_pod(self.default_order)

return result

@classmethod
def from_pod(cls, data):
order_dict = {}
default_order = None

for d in data["orderers"]:
d = d.copy()
fams = d.pop("packages")
orderer = from_pod(d)

for fam in fams:
order_dict[fam] = orderer

d = data.get("default_order")
if d:
default_order = from_pod(d)

return cls(order_dict, default_order)


def register_plugin():
return PerFamilyOrder
Loading
Loading