]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
chore: Touch up {download,upload}-redis scripts
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 9 Aug 2022 12:48:54 +0000 (14:48 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 15 Aug 2022 20:13:30 +0000 (22:13 +0200)
misc/download-redis
misc/upload-redis

index 021c7011a061539482b4c57593352a27b8c40ffa..05e0d033536cced3a7ef2a9da9daab9788d3a6fd 100755 (executable)
@@ -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))
index 87a34b3615ac412cac4ff72182885a6077e9c039..4de656a95fe70c2e5e5dfca880633dc3a9031578 100755 (executable)
@@ -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))