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

Attempt at reducing the possibility of having import cycles when user configs import rez #1918

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/rez/package_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from hashlib import sha1
from typing import Dict, Iterable, List, Optional, Union

from rez.config import config
from rez.utils.data_utils import cached_class_property
from rez.version import Version, VersionRange
from rez.version._version import _Comparable, _ReversedComparable, _LowerBound, _UpperBound, _Bound
Expand Down Expand Up @@ -637,6 +636,7 @@ def from_pod(cls, data):
@cached_class_property
def singleton(cls):
"""Filter list as configured by rezconfig.package_filter."""
from rez.config import config
return cls.from_pod(config.package_orderers)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion src/rez/package_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from rez.utils.resources import ResourcePool, ResourceHandle
from rez.utils.data_utils import cached_property
from rez.plugin_managers import plugin_manager
from rez.config import config
from rez.exceptions import ResourceError
from contextlib import contextmanager
import threading
Expand Down Expand Up @@ -525,6 +524,7 @@ def __init__(self, resource_pool=None):
None, a default pool is created based on config settings.
"""
if resource_pool is None:
from rez.config import config
cache_size = config.resource_caching_maxsize
if cache_size < 0: # -1 == disable caching
cache_size = None
Expand Down
20 changes: 18 additions & 2 deletions src/rez/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright Contributors to the Rez Project


from rez.package_repository import package_repository_manager
from rez.package_resources import PackageFamilyResource, PackageResource, \
VariantResource, package_family_schema, package_schema, variant_schema, \
package_release_keys, late_requires_schema
Expand All @@ -17,7 +16,6 @@
from rez.version import Version, VersionRange
from rez.version import VersionedObject
from rez.serialise import FileFormat
from rez.config import config

import os
import sys
Expand Down Expand Up @@ -99,11 +97,13 @@ def config(self):
Defaults to global config if this package did not provide a 'config'
section.
"""
from rez.config import config
return self.resource.config or config

@cached_property
def is_local(self):
"""Returns True if the package is in the local package repository"""
from rez.package_repository import package_repository_manager
local_repo = package_repository_manager.get_repository(
self.config.local_packages_path)
return (self.resource._repository.uid == local_repo.uid)
Expand Down Expand Up @@ -262,6 +262,7 @@ def is_relocatable(self):
if self.relocatable is not None:
return self.relocatable

from rez.config import config
if config.default_relocatable_per_repository:
value = config.default_relocatable_per_repository.get(
self.repository.location)
Expand All @@ -282,6 +283,7 @@ def is_cachable(self):
if self.cachable is not None:
return self.cachable

from rez.config import config
if config.default_cachable_per_repository:
# TODO: The location of filesystem repository is canonical path,
# so if the path in `default_cachable_per_repository` isn't
Expand Down Expand Up @@ -451,6 +453,7 @@ def install(self, path, dry_run=False, overrides=None):
`Variant` object - the (existing or newly created) variant in the
specified repository. If `dry_run` is True, None may be returned.
"""
from rez.package_repository import package_repository_manager
repo = package_repository_manager.get_repository(path)
resource = repo.install_variant(self.resource,
dry_run=dry_run,
Expand Down Expand Up @@ -507,6 +510,7 @@ def __contains__(self, package):

@cached_property
def _repository_uids(self):
from rez.package_repository import package_repository_manager
uids = set()
for path in self.paths:
repo = package_repository_manager.get_repository(path)
Expand All @@ -532,6 +536,8 @@ def iter_package_families(paths=None):
Returns:
`PackageFamily` iterator.
"""
from rez.config import config
from rez.package_repository import package_repository_manager
for path in (paths or config.packages_path):
repo = package_repository_manager.get_repository(path)
for resource in repo.iter_package_families():
Expand Down Expand Up @@ -607,6 +613,7 @@ def get_package_family_from_repository(name, path):
Returns:
`PackageFamily` object, or None if the family was not found.
"""
from rez.package_repository import package_repository_manager
repo = package_repository_manager.get_repository(path)

family_resource = repo.get_package_family(name)
Expand All @@ -626,6 +633,7 @@ def get_package_from_repository(name, version, path):
Returns:
`Package` object, or None if the package was not found.
"""
from rez.package_repository import package_repository_manager
repo = package_repository_manager.get_repository(path)

if isinstance(version, str):
Expand All @@ -649,6 +657,7 @@ def get_package_from_handle(package_handle):
Returns:
`Package`.
"""
from rez.package_repository import package_repository_manager
if isinstance(package_handle, dict):
package_handle = ResourceHandle.from_dict(package_handle)
package_resource = package_repository_manager.get_resource_from_handle(package_handle)
Expand Down Expand Up @@ -713,6 +722,7 @@ def get_variant(variant_handle, context=None):
Returns:
`Variant`.
"""
from rez.package_repository import package_repository_manager
if isinstance(variant_handle, dict):
variant_handle = ResourceHandle.from_dict(variant_handle)

Expand All @@ -733,6 +743,7 @@ def get_package_from_uri(uri, paths=None):
Returns:
`Package`, or None if the package could not be found.
"""
from rez.package_repository import package_repository_manager
def _find_in_path(path):
repo = package_repository_manager.get_repository(path)
pkg_resource = repo.get_package_from_uri(uri)
Expand All @@ -741,6 +752,7 @@ def _find_in_path(path):
else:
return None

from rez.config import config
for path in (paths or config.packages_path):
pkg = _find_in_path(path)
if pkg is not None:
Expand Down Expand Up @@ -780,6 +792,7 @@ def get_variant_from_uri(uri, paths=None):
Returns:
`Variant`, or None if the variant could not be found.
"""
from rez.package_repository import package_repository_manager
def _find_in_path(path):
repo = package_repository_manager.get_repository(path)
variant_resource = repo.get_variant_from_uri(uri)
Expand All @@ -788,6 +801,7 @@ def _find_in_path(path):
else:
return None

from rez.config import config
for path in (paths or config.packages_path):
variant = _find_in_path(path)
if variant is not None:
Expand Down Expand Up @@ -951,6 +965,8 @@ def get_latest_package_from_string(txt, paths=None, error=False):

def _get_families(name, paths=None):
entries = []
from rez.config import config
from rez.package_repository import package_repository_manager
for path in (paths or config.packages_path):
repo = package_repository_manager.get_repository(path)
family_resource = repo.get_package_family(name)
Expand Down
4 changes: 3 additions & 1 deletion src/rez/plugin_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""
Manages loading of all types of Rez plugins.
"""
from rez.config import config, expand_system_vars, _load_config_from_filepaths
from rez.utils.formatting import columnise
from rez.utils.schema import dict_to_schema
from rez.utils.data_utils import LazySingleton, cached_property, deep_update
Expand Down Expand Up @@ -54,6 +53,7 @@ def extend_path(path, name):
init_py = "__init__" + os.extsep + "py"
path = path[:]

from rez.config import config
def append_if_valid(dir_):
if os.path.isdir(dir_):
subdir = os.path.normcase(os.path.join(dir_, pname))
Expand Down Expand Up @@ -126,6 +126,7 @@ def load_plugins(self):
# be found before the builtin plugins (from /rezplugins).
paths = reversed(paths)

from rez.config import config, _load_config_from_filepaths
for path in paths:
if config.debug("plugins"):
print_debug("searching plugin path %s...", path)
Expand Down Expand Up @@ -226,6 +227,7 @@ def config_schema(self):
"""Returns the merged configuration data schema for this plugin
type."""
from rez.config import _plugin_config_dict
from rez.config import expand_system_vars
d = _plugin_config_dict.get(self.type_name, {})

for name, plugin_class in self.plugin_classes.items():
Expand Down
42 changes: 29 additions & 13 deletions src/rez/serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
from rez.utils.memcached import memcached
from rez.utils.execution import add_sys_paths
from rez.util import get_function_arg_names
from rez.config import config
from rez.vendor.atomicwrites import atomic_write
from rez.vendor import yaml


tmpdir_manager = TempDirs(config.tmpdir, prefix="rez_write_")
debug_print = config.debug_printer("file_loads")
file_cache = {}


Expand Down Expand Up @@ -60,14 +57,19 @@ def open_file_for_write(filepath, mode=None):
Yields:
File-like object.
"""
from rez.config import config

stream = StringIO()
yield stream
content = stream.getvalue()

filepath = os.path.realpath(filepath)
tmpdir_manager = TempDirs(config.tmpdir, prefix="rez_write_")

tmpdir = tmpdir_manager.mkdtemp()
cache_filepath = os.path.join(tmpdir, os.path.basename(filepath))

debug_print = config.debug_printer("file_loads")
debug_print("Writing to %s (local cache of %s)", cache_filepath, filepath)

# Attempt to make file writable if it isn't already. Just fallthrough
Expand Down Expand Up @@ -136,9 +138,11 @@ def load_from_file(filepath, format_=FileFormat.py, update_data_callback=None,
format_=format_,
update_data_callback=update_data_callback)
else:
return _load_from_file(filepath=filepath,
format_=format_,
update_data_callback=update_data_callback)
return _load_from_file()(
filepath=filepath,
format_=format_,
update_data_callback=update_data_callback
)


def _load_from_file__key(filepath, format_, update_data_callback):
Expand All @@ -152,17 +156,28 @@ def _load_from_file__key(filepath, format_, update_data_callback):
int(st.st_ino), st.st_mtime))


@memcached(servers=config.memcached_uri if config.cache_package_files else None,
min_compress_len=config.memcached_package_file_min_compress_len,
key=_load_from_file__key,
debug=config.debug_memcache)
def _load_from_file(filepath, format_, update_data_callback):
return _load_file(filepath, format_, update_data_callback)
def _load_from_file():
from rez.config import config

# A lot of gymnastics to avoid habing to import rez.config at the top level.
@memcached(
servers=config.memcached_uri if config.cache_package_files else None,
min_compress_len=config.memcached_package_file_min_compress_len,
key=_load_from_file__key,
debug=config.debug_memcache
)
def _load_from_file_inner(filepath, format_, update_data_callback):
return _load_file(filepath, format_, update_data_callback)

return _load_from_file_inner


def _load_file(filepath, format_, update_data_callback, original_filepath=None):
from rez.config import config

load_func = load_functions[format_]

debug_print = config.debug_printer("file_loads")
if debug_print:
if original_filepath:
debug_print("Loading file: %s (local cache of %s)",
Expand Down Expand Up @@ -228,6 +243,7 @@ def load_py(stream, filepath=None):
Returns:
dict:
"""
from rez.config import config
with add_sys_paths(config.package_definition_build_python_paths):
return _load_py(stream, filepath=filepath)

Expand Down Expand Up @@ -441,7 +457,7 @@ def load_txt(stream, **kwargs):

def clear_file_caches():
"""Clear any cached files."""
_load_from_file.forget()
_load_from_file().forget()


load_functions = {FileFormat.py: load_py,
Expand Down
12 changes: 9 additions & 3 deletions src/rez/utils/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright Contributors to the Rez Project


from rez.config import config
from rez.vendor.memcache.memcache import Client as Client_, \
SERVER_MAX_KEY_LENGTH, __version__ as memcache_client_version
from rez.util import get_function_arg_names
Expand Down Expand Up @@ -32,7 +31,6 @@ def __bool__(self):

miss = _Miss()

logger = config.debug_printer("memcache")

def __init__(self, servers, debug=False):
"""Create a memcached client.
Expand All @@ -48,6 +46,8 @@ def __init__(self, servers, debug=False):
self._client = None
self.debug = debug
self.current = ''
from rez.config import config
self.logger = config.debug_printer("memcache")

def __bool__(self):
return bool(self.servers)
Expand Down Expand Up @@ -223,7 +223,7 @@ def release(self, key):


@contextmanager
def memcached_client(servers=config.memcached_uri, debug=config.debug_memcache):
def memcached_client(servers=None, debug=None):
"""Get a shared memcached instance.
This function shares the same memcached instance across nested invocations.
Expand All @@ -236,6 +236,12 @@ def memcached_client(servers=config.memcached_uri, debug=config.debug_memcache):
Returns:
`Client`: Memcached instance.
"""
from rez.config import config
if servers is None:
servers = config.memcached_uri
if debug is None:
debug = config.debug_memcache

key = None
try:
client, key = scoped_instance_manager.acquire(servers, debug=debug)
Expand Down
Loading