-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathapply-cache-diff.py
59 lines (41 loc) · 1.69 KB
/
apply-cache-diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Script for applying a cache diff.
With some infrastructure, this can allow for distributing small cache diffs to users in
many cases instead of full cache artifacts.
"""
from __future__ import annotations
import argparse
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from mypy.metastore import FilesystemMetadataStore, MetadataStore, SqliteMetadataStore
from mypy.util import json_dumps, json_loads
def make_cache(input_dir: str, sqlite: bool) -> MetadataStore:
if sqlite:
return SqliteMetadataStore(input_dir)
else:
return FilesystemMetadataStore(input_dir)
def apply_diff(cache_dir: str, diff_file: str, sqlite: bool = False) -> None:
cache = make_cache(cache_dir, sqlite)
with open(diff_file, "rb") as f:
diff = json_loads(f.read())
old_deps = json_loads(cache.read("@deps.meta.json"))
for file, data in diff.items():
if data is None:
cache.remove(file)
else:
cache.write(file, data)
if file.endswith(".meta.json") and "@deps" not in file:
meta = json_loads(data)
old_deps["snapshot"][meta["id"]] = meta["hash"]
cache.write("@deps.meta.json", json_dumps(old_deps))
cache.commit()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--sqlite", action="store_true", default=False, help="Use a sqlite cache")
parser.add_argument("cache_dir", help="Directory for the cache")
parser.add_argument("diff", help="Cache diff file")
args = parser.parse_args()
apply_diff(args.cache_dir, args.diff, args.sqlite)
if __name__ == "__main__":
main()