#!/usr/bin/env python3
-# This script downloads the contents of the cache from a Redis secondary storage
+# This script downloads the contents of the cache from a Redis secondary
+# storage.
import redis
import os
host, port, sock = None, None, config
else:
host, port, sock = config, 6379, None
+
username = os.getenv("REDIS_USERNAME")
password = os.getenv("REDIS_PASSWORD")
context = redis.Redis(host=host, port=port, unix_socket_path=sock, password=password)
pipe = context.pipeline(transaction=False)
ccache = os.getenv("CCACHE_DIR", os.path.expanduser("~/.cache/ccache"))
+
try:
count = context.info()["db0"]["keys"]
-except:
+except Exception:
count = None # only for showing progress
files = result = manifest = objects = 0
size = 0
)
if not count:
bar = progress.spinner.Spinner("Downloading... ")
-keys = context.scan_iter()
+
# Note: doesn't work with the SSDB SCAN command
# syntax: keys key_start, key_end, limit
-for key in keys:
+for key in context.scan_iter():
if not key.startswith(b"ccache:"):
bar.next()
continue
if val is None:
continue
if val[0:2] == b"\xcc\xac": # magic
- objects = objects + 1
+ objects += 1
if val[2] == 0 and val[3] == 0:
ext = "R"
- result = result + 1
+ result += 1
elif val[2] == 0 and val[3] == 1:
ext = "M"
- manifest = manifest + 1
+ manifest += 1
else:
bar.next()
continue
os.makedirs(dirname, mode=0o755, exist_ok=True)
with open(filename, "wb") as out:
out.write(val)
- files = files + 1
- size = size + len(val)
+ files += 1
+ size += len(val)
bar.next()
bar.finish()
+
print(
"%d files, %d result (%d manifest) = %d objects (%s)"
% (files, result, manifest, objects, humanize.naturalsize(size, binary=True))
stat = os.stat(os.path.join(dirpath, filename))
filelist.append((stat.st_mtime, dirpath, filename, stat.st_size))
filelist.sort()
+
files = result = manifest = objects = 0
size = 0
batchsize = 0
continue
elif filename == "CACHEDIR.TAG" or filename == "stats":
# ignore these
- files = files + 1
+ files += 1
else:
(base, ext) = filename[:-1], filename[-1:]
if ext == "R" or ext == "M":
if ext == "R":
- result = result + 1
+ result += 1
if ext == "M":
- manifest = manifest + 1
+ manifest += 1
key = "ccache:" + "".join(list(os.path.split(dirname)) + [base])
val = open(os.path.join(dirpath, filename), "rb").read() or None
if val:
# print("%s: %s %d" % (key, ext, len(val)))
pipe.setnx(key, val)
- objects = objects + 1
- files = files + 1
- size = size + filesize
- batchsize = batchsize + filesize
+ objects += 1
+ files += 1
+ size += filesize
+ batchsize += filesize
if batchsize > 64 * 1024 * 1024:
pipe.execute()
batchsize = 0
bar.next()
pipe.execute()
bar.finish()
+
print(
"%d files, %d result (%d manifest) = %d objects (%s)"
% (files, result, manifest, objects, humanize.naturalsize(size, binary=True))