Skip to content

Commit

Permalink
solver is now nearly fully complete
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Jun 22, 2024
1 parent ed75a41 commit 53b2b37
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 85 deletions.
3 changes: 2 additions & 1 deletion src/rez/build_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os.path
import sys
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from rez.build_system import BuildSystem
from rez.packages import Package, Variant
Expand Down Expand Up @@ -57,7 +58,7 @@ def create_build_process(process_type: str,
if process_type not in process_types:
raise BuildProcessError("Unknown build process: %r" % process_type)

cls = plugin_manager.get_plugin_class('build_process', process_type)
cls = plugin_manager.get_plugin_class('build_process', process_type, BuildProcess)

return cls(working_dir, # ignored (deprecated)
build_system,
Expand Down
9 changes: 5 additions & 4 deletions src/rez/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def get_valid_build_systems(working_dir: str,

# package explicitly specifies build system
if buildsys_name:
cls = plugin_manager.get_plugin_class('build_system', buildsys_name)
cls = plugin_manager.get_plugin_class('build_system', buildsys_name, BuildSystem)
return [cls]

# detect valid build systems
clss = []
for buildsys_name in get_buildsys_types():
cls = plugin_manager.get_plugin_class('build_system', buildsys_name)
cls = plugin_manager.get_plugin_class('build_system', buildsys_name, BuildSystem)
if cls.is_valid_root(working_dir, package=package):
clss.append(cls)

Expand All @@ -82,7 +82,8 @@ def get_valid_build_systems(working_dir: str,
return clss


def create_build_system(working_dir: str, buildsys_type=None, package=None, opts=None,
def create_build_system(working_dir: str, buildsys_type: str | None = None,
package=None, opts=None,
write_build_scripts=False, verbose=False,
build_args=[], child_build_args=[]) -> BuildSystem:
"""Return a new build system that can build the source in working_dir."""
Expand All @@ -104,7 +105,7 @@ def create_build_system(working_dir: str, buildsys_type=None, package=None, opts
buildsys_type = next(iter(clss)).name()

# create instance of build system
cls_ = plugin_manager.get_plugin_class('build_system', buildsys_type)
cls_ = plugin_manager.get_plugin_class('build_system', buildsys_type, BuildSystem)

return cls_(working_dir,
opts=opts,
Expand Down
5 changes: 3 additions & 2 deletions src/rez/package_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def create_memory_package_repository(repository_data: dict) -> PackageRepository
Returns:
`PackageRepository` object.
"""
cls_: type[MemoryPackageRepository] = plugin_manager.get_plugin_class("package_repository", "memory")
from rezplugins.package_repository.memory import MemoryPackageRepository
cls_ = plugin_manager.get_plugin_class("package_repository", "memory", MemoryPackageRepository)
return cls_.create_repository(repository_data)


Expand Down Expand Up @@ -656,7 +657,7 @@ def clear_caches(self) -> None:

def _get_repository(self, path: str, **repo_args) -> PackageRepository:
repo_type, location = path.split('@', 1)
cls = plugin_manager.get_plugin_class('package_repository', repo_type)
cls = plugin_manager.get_plugin_class('package_repository', repo_type, PackageRepository)
repo = cls(location, self.pool, **repo_args)
return repo

Expand Down
18 changes: 16 additions & 2 deletions src/rez/plugin_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
from rez.utils.logging_ import print_debug, print_warning
from rez.exceptions import RezPluginError
from zipimport import zipimporter
from typing import overload, TypeVar
import pkgutil
import os.path
import sys

T = TypeVar("T")

# modified from pkgutil standard library:
# this function is called from the __init__.py files of each plugin type inside
Expand Down Expand Up @@ -355,10 +357,22 @@ def get_plugins(self, plugin_type):
type."""
return self._get_plugin_type(plugin_type).plugin_classes.keys()

def get_plugin_class(self, plugin_type, plugin_name):
@overload
def get_plugin_class(self, plugin_type: str, plugin_name: str) -> type:
pass

@overload
def get_plugin_class(self, plugin_type: str, plugin_name: str, expected_type: type[T]) -> type[T]:
pass

def get_plugin_class(self, plugin_type, plugin_name, expected_type=None):
"""Return the class registered under the given plugin name."""
plugin = self._get_plugin_type(plugin_type)
return plugin.get_plugin_class(plugin_name)
cls = plugin.get_plugin_class(plugin_name)
if expected_type is not None and not isinstance(cls, type) and issubclass(cls, expected_type):
raise RezPluginError("%s: Plugin class for %s was not the expected type: %s != %s"
% (plugin.pretty_type_name, plugin_name, cls, expected_type))
return cls

def get_plugin_module(self, plugin_type, plugin_name):
"""Return the module defining the class registered under the given
Expand Down
31 changes: 22 additions & 9 deletions src/rez/resolved_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from rez.rex_bindings import VersionBinding, VariantBinding, \
VariantsBinding, RequirementsBinding, EphemeralsBinding, intersects
from rez import package_order
from rez.packages import get_variant, iter_packages
from rez.packages import get_variant, iter_packages, Package
from rez.package_filter import PackageFilterList
from rez.package_order import PackageOrder, PackageOrderList
from rez.package_cache import PackageCache
Expand All @@ -42,7 +42,7 @@
from contextlib import contextmanager
from functools import wraps
from enum import Enum
from typing import Any, Iterable
from typing import Any, Callable, Iterable, TYPE_CHECKING
import getpass
import json
import socket
Expand All @@ -52,6 +52,9 @@
import os
import os.path

if TYPE_CHECKING:
from rez.solver import SolverState, SupportsWrite


class RezToolsVisibility(Enum):
"""Determines if/how rez cli tools are added back to PATH within a
Expand Down Expand Up @@ -174,18 +177,28 @@ def __call__(self, state):
return self.callback(state)
return SolverCallbackReturn.keep_going, ''

def __init__(self, package_requests: Iterable[str | PackageRequest], verbosity=0, timestamp=None,
building=False, caching=None, package_paths=None,
def __init__(self,
package_requests: Iterable[str | Requirement],
verbosity=0,
timestamp: float | None = None,
building=False,
caching: bool | None = None,
package_paths: list[str] | None = None,
package_filter: PackageFilterList | None = None,
package_orderers: list[PackageOrder] | None = None,
max_fails=-1,
add_implicit_packages=True, time_limit=-1, callback=None,
package_load_callback=None, buf=None, suppress_passive=False,
print_stats=False, package_caching=None):
add_implicit_packages=True,
time_limit=-1,
callback: Callable[[SolverState], tuple[SolverCallbackReturn, str]] | None = None,
package_load_callback: Callable[[Package], Any] | None = None,
buf: SupportsWrite | None = None,
suppress_passive=False,
print_stats=False,
package_caching=None):
"""Perform a package resolve, and store the result.
Args:
package_requests (list[typing.Union[str, PackageRequest]]): request
package_requests (list[typing.Union[str, Requirement]]): request
verbosity (int): Verbosity level. One of [0,1,2].
timestamp (float): Ignore packages released after this epoch time. Packages
released at exactly this time will not be ignored.
Expand Down Expand Up @@ -865,7 +878,7 @@ def _rt(t):
_pr()

_pr("requested packages:", heading)
rows: list[tuple[str, str]] = []
rows = []
colors = []
for request in self._package_requests:
if request.name.startswith('.'):
Expand Down
27 changes: 22 additions & 5 deletions src/rez/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright Contributors to the Rez Project
from __future__ import annotations

from rez.solver import PackageVariant, Solver, SolverStatus
from rez.solver import Solver, SolverCallbackReturn, SolverState, SolverStatus
from rez.package_repository import package_repository_manager
from rez.packages import get_variant, get_last_release_time, Variant
from rez.package_filter import PackageFilterList, TimestampRule
Expand All @@ -13,6 +13,12 @@
from contextlib import contextmanager
from enum import Enum
from hashlib import sha1
from typing import Callable, TYPE_CHECKING

if TYPE_CHECKING:
from rez.resolved_context import ResolvedContext
from rez.package_order import PackageOrder
from rez.resolved_context import ResolvedContext


class ResolverStatus(Enum):
Expand All @@ -35,10 +41,21 @@ class Resolver(object):
The Resolver uses a combination of Solver(s) and cache(s) to resolve a
package request as quickly as possible.
"""
def __init__(self, context, package_requests, package_paths, package_filter=None,
package_orderers=None, timestamp=0, callback=None, building=False,
verbosity=False, buf=None, package_load_callback=None, caching=True,
suppress_passive=False, print_stats=False):
def __init__(self,
context: ResolvedContext,
package_requests: list[Requirement],
package_paths: list[str],
package_filter: PackageFilterList | None = None,
package_orderers: list[PackageOrder] | None = None,
timestamp=0,
callback: Callable[[SolverState], tuple[SolverCallbackReturn, str]] | None = None,
building=False,
verbosity=False,
buf=None,
package_load_callback=None,
caching=True,
suppress_passive=False,
print_stats=False):
"""Create a Resolver.
Args:
Expand Down
Loading

0 comments on commit 53b2b37

Please sign in to comment.