def test_2(self): """basic resource loading test.""" pool = ResourcePool(cache_size=None) pool.register_resource(ResourceA) pool.register_resource(ResourceB) # test that resource matches our request, and its data isn't loaded variables = dict(foo="hey", bah="ho") resource = pool.get_resource("resource.a", variables) self.assertTrue(isinstance(resource, ResourceA)) self.assertEqual(resource.variables, variables) # test that a request via a resource's own handle gives the same resource resource_ = pool.get_resource_from_handle(resource.handle) self.assertTrue(resource_ is resource) # test that the same request again gives the cached resource resource_ = pool.get_resource("resource.a", variables) self.assertTrue(resource_ is resource) # clear caches, then test that the same request gives a new resource pool.clear_caches() resource_ = pool.get_resource("resource.a", variables) self.assertEqual(resource_.variables, variables) self.assertTrue(resource_ is not resource)
class PackageRepositoryManager(object): """Package repository manager. Contains instances of `PackageRepository` for each repository pointed to by the 'packages_path' config setting (also commonly set using the environment variable REZ_PACKAGES_PATH). """ def __init__(self): cache_size = config.resource_caching_maxsize if cache_size < 0: cache_size = None self.cache_size = cache_size self.pool = ResourcePool(cache_size=cache_size) @lru_cache(maxsize=None) def get_repository(self, path): """Get a package repository. Args: path (str): Entry from the 'packages_path' config setting. This may simply be a path (which is managed by the 'filesystem' package repository plugin), or a string in the form "type@location", where 'type' identifies the repository plugin type to use. Returns: `PackageRepository` instance. """ # normalise parts = path.split('@', 1) if len(parts) == 1: parts = ("filesystem", parts[0]) repo_type, location = parts if repo_type == "filesystem": # choice of abspath here vs realpath is deliberate. Realpath gives # canonical path, which can be a problem if two studios are sharing # packages, and have mirrored package paths, but some are actually # different paths, symlinked to look the same. It happened! location = os.path.abspath(location) normalised_path = "%s@%s" % (repo_type, location) return self._get_repository(normalised_path) def are_same(self, path_1, path_2): """Test that `path_1` and `path_2` refer to the same repository. This is more reliable than testing that the strings match, since slightly different strings might refer to the same repository (consider small differences in a filesystem path for example, eg '//svr/foo', '/svr/foo'). Returns: True if the paths refer to the same repository, False otherwise. """ if path_1 == path_2: return True repo_1 = self.get_repository(path_1) repo_2 = self.get_repository(path_2) return (repo_1.uid == repo_2.uid) def get_resource(self, resource_key, repository_type, location, **variables): """Get a resource. Attempts to get and return a cached version of the resource if available, otherwise a new resource object is created and returned. Args: resource_key (`str`): Name of the type of `Resources` to find repository_type (`str`): What sort of repository to look for the resource in location (`str`): location for the repository variables: data to identify / store on the resource Returns: `PackageRepositoryResource` instance. """ path = "%s@%s" % (repository_type, location) repo = self.get_repository(path) resource = repo.get_resource(**variables) return resource def get_resource_from_handle(self, resource_handle): """Get a resource. Args: resource_handle (`ResourceHandle`): Handle of the resource. Returns: `PackageRepositoryResource` instance. """ repo_type = resource_handle.get("repository_type") location = resource_handle.get("location") if not (repo_type and location): raise ValueError("PackageRepositoryManager requires " "resource_handle objects to have a " "repository_type and location defined") path = "%s@%s" % (repo_type, location) repo = self.get_repository(path) resource = repo.get_resource_from_handle(resource_handle) return resource def clear_caches(self): """Clear all cached data.""" self.get_repository.cache_clear() self._get_repository.cache_clear() self.pool.clear_caches() @lru_cache(maxsize=None) def _get_repository(self, path): repo_type, location = path.split('@', 1) cls = plugin_manager.get_plugin_class('package_repository', repo_type) repo = cls(location, self.pool) return repo
class PackageRepositoryManager(object): """Package repository manager. Contains instances of `PackageRepository` for each repository pointed to by the 'packages_path' config setting (also commonly set using the environment variable REZ_PACKAGES_PATH). """ def __init__(self): cache_size = config.resource_caching_maxsize if cache_size < 0: cache_size = None self.pool = ResourcePool(cache_size=cache_size) @lru_cache(maxsize=None) def get_repository(self, path): """Get a package repository. Args: path (str): Entry from the 'packages_path' config setting. This may simply be a path (which is managed by the 'filesystem' package repository plugin), or a string in the form "type@location", where 'type' identifies the repository plugin type to use. Returns: `PackageRepository` instance. """ # normalise parts = path.split('@', 1) if len(parts) == 1: parts = ("filesystem", parts[0]) repo_type, location = parts if repo_type == "filesystem": location = os.path.realpath(location) normalised_path = "%s@%s" % (repo_type, location) return self._get_repository(normalised_path) def are_same(self, path_1, path_2): """Test that `path_1` and `path_2` refer to the same repository. This is more reliable than testing that the strings match, since slightly different strings might refer to the same repository (consider small differences in a filesystem path for example, eg '//svr/foo', '/svr/foo'). Returns: True if the paths refer to the same repository, False otherwise. """ if path_1 == path_2: return True repo_1 = self.get_repository(path_1) repo_2 = self.get_repository(path_2) return (repo_1.uid == repo_2.uid) def get_resource(self, resource_handle): """Get a resource. Args: resource_handle (`ResourceHandle`): Handle of the resource. Returns: `PackageRepositoryResource` instance. """ repo_type = resource_handle.get("repository_type") location = resource_handle.get("location") path = "%s@%s" % (repo_type, location) repo = self.get_repository(path) resource = repo.get_resource_from_handle(resource_handle) return resource def clear_caches(self): """Clear all cached data.""" self.get_repository.cache_clear() self._get_repository.cache_clear() self.pool.clear_caches() @lru_cache(maxsize=None) def _get_repository(self, path): repo_type, location = path.split('@', 1) cls = plugin_manager.get_plugin_class('package_repository', repo_type) repo = cls(location, self.pool) return repo