From 2f3801196d353ab4e7cb4c9cda35eaeb96ed40f9 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 30 Oct 2023 12:52:47 +0100 Subject: binary_distribution.py: fix type annotation singleton (#40572) Convince the language server it's really just a BinaryCacheIndex, otherwise it defaults to thinking it's Singleton, and can't autocomplete etc. --- lib/spack/spack/binary_distribution.py | 38 ++++++++++++++-------------------- lib/spack/spack/bootstrap/core.py | 2 +- lib/spack/spack/ci.py | 2 +- lib/spack/spack/test/conftest.py | 6 +++--- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/spack/spack/binary_distribution.py b/lib/spack/spack/binary_distribution.py index af04dfefb0..6a49ab445e 100644 --- a/lib/spack/spack/binary_distribution.py +++ b/lib/spack/spack/binary_distribution.py @@ -25,7 +25,7 @@ import urllib.request import warnings from contextlib import closing, contextmanager from gzip import GzipFile -from typing import Dict, List, NamedTuple, Optional, Tuple, Union +from typing import Dict, List, NamedTuple, Optional, Set, Tuple from urllib.error import HTTPError, URLError import llnl.util.filesystem as fsys @@ -53,6 +53,7 @@ import spack.traverse as traverse import spack.util.crypto import spack.util.file_cache as file_cache import spack.util.gpg +import spack.util.path import spack.util.spack_json as sjson import spack.util.spack_yaml as syaml import spack.util.timer as timer @@ -130,25 +131,25 @@ class BinaryCacheIndex: mean we should have paid the price to update the cache earlier? """ - def __init__(self, cache_root): - self._index_cache_root = cache_root + def __init__(self, cache_root: Optional[str] = None): + self._index_cache_root: str = cache_root or binary_index_location() # the key associated with the serialized _local_index_cache self._index_contents_key = "contents.json" # a FileCache instance storing copies of remote binary cache indices - self._index_file_cache = None + self._index_file_cache: Optional[file_cache.FileCache] = None # stores a map of mirror URL to index hash and cache key (index path) - self._local_index_cache = None + self._local_index_cache: Optional[dict] = None # hashes of remote indices already ingested into the concrete spec # cache (_mirrors_for_spec) - self._specs_already_associated = set() + self._specs_already_associated: Set[str] = set() # mapping from mirror urls to the time.time() of the last index fetch and a bool indicating # whether the fetch succeeded or not. - self._last_fetch_times = {} + self._last_fetch_times: Dict[str, float] = {} # _mirrors_for_spec is a dictionary mapping DAG hashes to lists of # entries indicating mirrors where that concrete spec can be found. @@ -158,7 +159,7 @@ class BinaryCacheIndex: # - the concrete spec itself, keyed by ``spec`` (including the # full hash, since the dag hash may match but we want to # use the updated source if available) - self._mirrors_for_spec = {} + self._mirrors_for_spec: Dict[str, dict] = {} def _init_local_index_cache(self): if not self._index_file_cache: @@ -529,15 +530,8 @@ def binary_index_location(): return spack.util.path.canonicalize_path(cache_root) -def _binary_index(): - """Get the singleton store instance.""" - return BinaryCacheIndex(binary_index_location()) - - -#: Singleton binary_index instance -binary_index: Union[BinaryCacheIndex, llnl.util.lang.Singleton] = llnl.util.lang.Singleton( - _binary_index -) +#: Default binary cache index instance +BINARY_INDEX: BinaryCacheIndex = llnl.util.lang.Singleton(BinaryCacheIndex) # type: ignore class NoOverwriteException(spack.error.SpackError): @@ -2255,7 +2249,7 @@ def get_mirrors_for_spec(spec=None, mirrors_to_check=None, index_only=False): tty.debug("No Spack mirrors are currently configured") return {} - results = binary_index.find_built_spec(spec, mirrors_to_check=mirrors_to_check) + results = BINARY_INDEX.find_built_spec(spec, mirrors_to_check=mirrors_to_check) # The index may be out-of-date. If we aren't only considering indices, try # to fetch directly since we know where the file should be. @@ -2264,7 +2258,7 @@ def get_mirrors_for_spec(spec=None, mirrors_to_check=None, index_only=False): # We found a spec by the direct fetch approach, we might as well # add it to our mapping. if results: - binary_index.update_spec(spec, results) + BINARY_INDEX.update_spec(spec, results) return results @@ -2280,12 +2274,12 @@ def update_cache_and_get_specs(): Throws: FetchCacheError """ - binary_index.update() - return binary_index.get_all_built_specs() + BINARY_INDEX.update() + return BINARY_INDEX.get_all_built_specs() def clear_spec_cache(): - binary_index.clear() + BINARY_INDEX.clear() def get_keys(install=False, trust=False, force=False, mirrors=None): diff --git a/lib/spack/spack/bootstrap/core.py b/lib/spack/spack/bootstrap/core.py index d7b39b02e0..9fb04453c4 100644 --- a/lib/spack/spack/bootstrap/core.py +++ b/lib/spack/spack/bootstrap/core.py @@ -214,7 +214,7 @@ class BuildcacheBootstrapper(Bootstrapper): with spack.config.override(self.mirror_scope): # This index is currently needed to get the compiler used to build some # specs that we know by dag hash. - spack.binary_distribution.binary_index.regenerate_spec_cache() + spack.binary_distribution.BINARY_INDEX.regenerate_spec_cache() index = spack.binary_distribution.update_cache_and_get_specs() if not index: diff --git a/lib/spack/spack/ci.py b/lib/spack/spack/ci.py index fca2836254..cda7a622d6 100644 --- a/lib/spack/spack/ci.py +++ b/lib/spack/spack/ci.py @@ -932,7 +932,7 @@ def generate_gitlab_ci_yaml( # Speed up staging by first fetching binary indices from all mirrors try: - bindist.binary_index.update() + bindist.BINARY_INDEX.update() except bindist.FetchCacheError as e: tty.warn(e) diff --git a/lib/spack/spack/test/conftest.py b/lib/spack/spack/test/conftest.py index 3505d72130..514b1e9154 100644 --- a/lib/spack/spack/test/conftest.py +++ b/lib/spack/spack/test/conftest.py @@ -495,7 +495,7 @@ def mock_binary_index(monkeypatch, tmpdir_factory): tmpdir = tmpdir_factory.mktemp("mock_binary_index") index_path = tmpdir.join("binary_index").strpath mock_index = spack.binary_distribution.BinaryCacheIndex(index_path) - monkeypatch.setattr(spack.binary_distribution, "binary_index", mock_index) + monkeypatch.setattr(spack.binary_distribution, "BINARY_INDEX", mock_index) yield @@ -1710,8 +1710,8 @@ def inode_cache(): @pytest.fixture(autouse=True) def brand_new_binary_cache(): yield - spack.binary_distribution.binary_index = llnl.util.lang.Singleton( - spack.binary_distribution._binary_index + spack.binary_distribution.BINARY_INDEX = llnl.util.lang.Singleton( + spack.binary_distribution.BinaryCacheIndex ) -- cgit v1.2.3-70-g09d2