From fd45839c04b1130a8da7ad31fa9abc316fd2ddf6 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Fri, 12 May 2023 11:55:13 +0200 Subject: Improve error message for buildcaches (#37626) --- lib/spack/spack/binary_distribution.py | 19 +++++++++++++++---- lib/spack/spack/database.py | 10 ++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/spack/spack/binary_distribution.py b/lib/spack/spack/binary_distribution.py index ca69695f11..06ed2ff5a5 100644 --- a/lib/spack/spack/binary_distribution.py +++ b/lib/spack/spack/binary_distribution.py @@ -193,10 +193,17 @@ class BinaryCacheIndex(object): db_root_dir = os.path.join(tmpdir, "db_root") db = spack_db.Database(None, db_dir=db_root_dir, enable_transaction_locking=False) - self._index_file_cache.init_entry(cache_key) - cache_path = self._index_file_cache.cache_path(cache_key) - with self._index_file_cache.read_transaction(cache_key): - db._read_from_file(cache_path) + try: + self._index_file_cache.init_entry(cache_key) + cache_path = self._index_file_cache.cache_path(cache_key) + with self._index_file_cache.read_transaction(cache_key): + db._read_from_file(cache_path) + except spack_db.InvalidDatabaseVersionError as e: + msg = ( + f"you need a newer Spack version to read the buildcache index for the " + f"following mirror: '{mirror_url}'. {e.database_version_message}" + ) + raise BuildcacheIndexError(msg) from e spec_list = db.query_local(installed=False, in_buildcache=True) @@ -2429,6 +2436,10 @@ class FetchIndexError(Exception): return "{}, due to: {}".format(self.args[0], self.args[1]) +class BuildcacheIndexError(spack.error.SpackError): + """Raised when a buildcache cannot be read for any reason""" + + FetchIndexResult = collections.namedtuple("FetchIndexResult", "etag hash data fresh") diff --git a/lib/spack/spack/database.py b/lib/spack/spack/database.py index 9fa041ea35..47cdb51774 100644 --- a/lib/spack/spack/database.py +++ b/lib/spack/spack/database.py @@ -1654,8 +1654,14 @@ class InvalidDatabaseVersionError(SpackError): """Exception raised when the database metadata is newer than current Spack.""" def __init__(self, database, expected, found): + self.expected = expected + self.found = found msg = ( - f"you need a newer Spack version to read the database in '{database.root}'. " - f"The expected database version is '{expected}', but '{found}' was found." + f"you need a newer Spack version to read the DB in '{database.root}'. " + f"{self.database_version_message}" ) super(InvalidDatabaseVersionError, self).__init__(msg) + + @property + def database_version_message(self): + return f"The expected DB version is '{self.expected}', but '{self.found}' was found." -- cgit v1.2.3-70-g09d2