summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util
diff options
context:
space:
mode:
authorHarmen Stoppels <harmenstoppels@gmail.com>2023-02-18 19:22:48 +0100
committerGitHub <noreply@github.com>2023-02-18 19:22:48 +0100
commit86320eb5699036698d03b23c9cbf2dbf75422880 (patch)
tree52308cb32a1a108e86fd2c78538e408c5dd2e3ce /lib/spack/llnl/util
parentc42a4ec1ec3ea8b339752c9257d91f0e07c254b1 (diff)
downloadspack-86320eb5699036698d03b23c9cbf2dbf75422880.tar.gz
spack-86320eb5699036698d03b23c9cbf2dbf75422880.tar.bz2
spack-86320eb5699036698d03b23c9cbf2dbf75422880.tar.xz
spack-86320eb5699036698d03b23c9cbf2dbf75422880.zip
Improve error handling in buildcache downloads (#35568)
The checksum exception was not detailed enough and not reraised when using cache only, resulting in useless error messages. Now it dumps the file path, expected hash, computed hash, and the downloaded file summary.
Diffstat (limited to 'lib/spack/llnl/util')
-rw-r--r--lib/spack/llnl/util/filesystem.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py
index 330ee35911..4dfea7975d 100644
--- a/lib/spack/llnl/util/filesystem.py
+++ b/lib/spack/llnl/util/filesystem.py
@@ -2630,3 +2630,28 @@ def temporary_dir(
yield tmp_dir
finally:
remove_directory_contents(tmp_dir)
+
+
+def filesummary(path, print_bytes=16) -> Tuple[int, bytes]:
+ """Create a small summary of the given file. Does not error
+ when file does not exist.
+
+ Args:
+ print_bytes (int): Number of bytes to print from start/end of file
+
+ Returns:
+ Tuple of size and byte string containing first n .. last n bytes.
+ Size is 0 if file cannot be read."""
+ try:
+ n = print_bytes
+ with open(path, "rb") as f:
+ size = os.fstat(f.fileno()).st_size
+ if size <= 2 * n:
+ short_contents = f.read(2 * n)
+ else:
+ short_contents = f.read(n)
+ f.seek(-n, 2)
+ short_contents += b"..." + f.read(n)
+ return size, short_contents
+ except OSError:
+ return 0, b""