Bump to ccache 4.4
[platform/upstream/ccache.git] / misc / upload-redis
1 #!/usr/bin/env python3
2
3 # This script uploads the contents of the cache from primary storage to a Redis
4 # secondary storage.
5
6 import redis
7 import os
8
9 config = os.getenv("REDIS_CONF", "localhost")
10 if ":" in config:
11     host, port = config.rsplit(":", 1)
12     sock = None
13 elif config.startswith("/"):
14     host, port, sock = None, None, config
15 else:
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
21 )
22
23 CCACHE_MANIFEST = b"cCmF"
24 CCACHE_RESULT = b"cCrS"
25
26 ccache = os.getenv("CCACHE_DIR", os.path.expanduser("~/.cache/ccache"))
27 filelist = []
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"):
32             continue
33         stat = os.stat(os.path.join(dirpath, filename))
34         filelist.append((stat.st_mtime, dirpath, filename))
35 filelist.sort()
36 files = result = manifest = objects = 0
37 for mtime, dirpath, filename in filelist:
38     dirname = dirpath.replace(ccache + os.path.sep, "")
39     if dirname == "tmp":
40         continue
41     elif filename == "CACHEDIR.TAG" or filename == "stats":
42         # ignore these
43         files = files + 1
44     else:
45         (base, ext) = filename[:-1], filename[-1:]
46         if ext == "R" or ext == "M":
47             if ext == "R":
48                 result = result + 1
49             if 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
53             if val:
54                 print("%s: %s %d" % (key, ext, len(val)))
55                 magic = val[0:4]
56                 if ext == "M":
57                     assert magic == CCACHE_MANIFEST
58                 if ext == "R":
59                     assert magic == CCACHE_RESULT
60                 context.set(key, val)
61                 objects = objects + 1
62         files = files + 1
63 print(
64     "%d files, %d result (%d manifest) = %d objects"
65     % (files, result, manifest, objects)
66 )