3 # This script uploads the contents of the cache from primary storage to a Redis
9 config = os.getenv("REDIS_CONF", "localhost")
11 host, port = config.rsplit(":", 1)
13 elif config.startswith("/"):
14 host, port, sock = None, None, config
16 host, port, sock = config, 6379, None
17 username = os.getenv("REDIS_USERNAME")
18 password = os.getenv("REDIS_PASSWORD")
19 context = redis.Redis(
20 host=host, port=port, unix_socket_path=sock, password=password
23 CCACHE_MANIFEST = b"cCmF"
24 CCACHE_RESULT = b"cCrS"
26 ccache = os.getenv("CCACHE_DIR", os.path.expanduser("~/.cache/ccache"))
28 for dirpath, dirnames, filenames in os.walk(ccache):
29 # sort by modification time, most recently used last
30 for filename in filenames:
31 if filename.endswith(".lock"):
33 stat = os.stat(os.path.join(dirpath, filename))
34 filelist.append((stat.st_mtime, dirpath, filename))
36 files = result = manifest = objects = 0
37 for mtime, dirpath, filename in filelist:
38 dirname = dirpath.replace(ccache + os.path.sep, "")
41 elif filename == "CACHEDIR.TAG" or filename == "stats":
45 (base, ext) = filename[:-1], filename[-1:]
46 if ext == "R" or ext == "M":
50 manifest = manifest + 1
51 key = "ccache:" + "".join(list(os.path.split(dirname)) + [base])
52 val = open(os.path.join(dirpath, filename), "rb").read() or None
54 print("%s: %s %d" % (key, ext, len(val)))
57 assert magic == CCACHE_MANIFEST
59 assert magic == CCACHE_RESULT
64 "%d files, %d result (%d manifest) = %d objects"
65 % (files, result, manifest, objects)