From: Joel Rosdahl Date: Tue, 9 Aug 2022 12:48:54 +0000 (+0200) Subject: chore: Touch up {download,upload}-redis scripts X-Git-Tag: v4.7~116 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bad194f1f97670bdb7d0e86255c59a8d914d2571;p=thirdparty%2Fccache.git chore: Touch up {download,upload}-redis scripts --- diff --git a/misc/download-redis b/misc/download-redis index 021c7011a..05e0d0335 100755 --- a/misc/download-redis +++ b/misc/download-redis @@ -1,6 +1,7 @@ #!/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 @@ -17,15 +18,17 @@ elif config.startswith("/"): 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 @@ -36,10 +39,10 @@ bar = progress.bar.Bar( ) 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 @@ -49,13 +52,13 @@ for key in keys: 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 @@ -65,10 +68,11 @@ for key in keys: 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)) diff --git a/misc/upload-redis b/misc/upload-redis index 87a34b361..4de656a95 100755 --- a/misc/upload-redis +++ b/misc/upload-redis @@ -32,6 +32,7 @@ for dirpath, dirnames, filenames in os.walk(ccache): 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 @@ -46,29 +47,30 @@ for mtime, dirpath, filename, filesize in filelist: 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))