| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Script for applying a cache diff. |
| 3 |
|
| 4 |
With some infrastructure, this can allow for distributing small cache diffs to users in |
| 5 |
many cases instead of full cache artifacts. |
| 6 |
|
| 7 |
Use diff-cache.py to generate a cache diff. |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import argparse |
| 13 |
import os |
| 14 |
import sys |
| 15 |
|
| 16 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 |
|
| 18 |
from librt import base64 |
| 19 |
from librt.internal import ReadBuffer |
| 20 |
|
| 21 |
from mypy.cache import CacheMeta |
| 22 |
from mypy.defaults import SQLITE_NUM_SHARDS |
| 23 |
from mypy.metastore import FilesystemMetadataStore, MetadataStore, SqliteMetadataStore |
| 24 |
from mypy.util import json_dumps, json_loads |
| 25 |
|
| 26 |
|
| 27 |
def make_cache(input_dir: str, sqlite: bool, num_shards: int = SQLITE_NUM_SHARDS) -> MetadataStore: |
| 28 |
if sqlite: |
| 29 |
return SqliteMetadataStore(input_dir, num_shards=num_shards) |
| 30 |
else: |
| 31 |
return FilesystemMetadataStore(input_dir) |
| 32 |
|
| 33 |
|
| 34 |
def apply_diff( |
| 35 |
cache_dir: str, diff_file: str, sqlite: bool = False, num_shards: int = SQLITE_NUM_SHARDS |
| 36 |
) -> None: |
| 37 |
cache = make_cache(cache_dir, sqlite, num_shards=num_shards) |
| 38 |
with open(diff_file, "rb") as f: |
| 39 |
diff = json_loads(f.read()) |
| 40 |
|
| 41 |
old_deps = json_loads(cache.read("@deps.meta.json")) |
| 42 |
|
| 43 |
for file, data in diff.items(): |
| 44 |
if data is None: |
| 45 |
cache.remove(file) |
| 46 |
else: |
| 47 |
if file.endswith(".ff"): |
| 48 |
data_bytes = base64.b64decode(data) |
| 49 |
else: |
| 50 |
data_bytes = data.encode() if isinstance(data, str) else data |
| 51 |
cache.write(file, data_bytes) |
| 52 |
if file.endswith(".meta.ff") and "@deps" not in file: |
| 53 |
buf = ReadBuffer(data_bytes[2:]) |
| 54 |
meta = CacheMeta.read(buf, data_file="") |
| 55 |
assert meta is not None |
| 56 |
old_deps["snapshot"][meta.id] = meta.hash |
| 57 |
elif file.endswith(".meta.json") and "@deps" not in file: |
| 58 |
meta_dict = json_loads(data_bytes) |
| 59 |
old_deps["snapshot"][meta_dict["id"]] = meta_dict["hash"] |
| 60 |
|
| 61 |
cache.write("@deps.meta.json", json_dumps(old_deps)) |
| 62 |
|
| 63 |
cache.commit() |
| 64 |
|
| 65 |
|
| 66 |
def main() -> None: |
| 67 |
parser = argparse.ArgumentParser() |
| 68 |
parser.add_argument("--sqlite", action="store_true", default=False, help="Use a sqlite cache") |
| 69 |
parser.add_argument( |
| 70 |
"--num-shards", type=int, default=SQLITE_NUM_SHARDS, help=argparse.SUPPRESS |
| 71 |
) |
| 72 |
parser.add_argument("cache_dir", help="Directory for the cache") |
| 73 |
parser.add_argument("diff", help="Cache diff file") |
| 74 |
args = parser.parse_args() |
| 75 |
|
| 76 |
apply_diff(args.cache_dir, args.diff, args.sqlite, num_shards=args.num_shards) |
| 77 |
|
| 78 |
|
| 79 |
if __name__ == "__main__": |
| 80 |
main() |