-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathconvert-cache.py
executable file
·61 lines (49 loc) · 1.85 KB
/
convert-cache.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
60
61
#!/usr/bin/env python3
"""Script for converting between cache formats.
We support a filesystem tree based cache and a sqlite based cache.
See mypy/metastore.py for details.
"""
from __future__ import annotations
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import argparse
from mypy.metastore import FilesystemMetadataStore, MetadataStore, SqliteMetadataStore
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--to-sqlite",
action="store_true",
default=False,
help="Convert to a sqlite cache (default: convert from)",
)
parser.add_argument(
"--output_dir",
action="store",
default=None,
help="Output cache location (default: same as input)",
)
parser.add_argument("input_dir", help="Input directory for the cache")
args = parser.parse_args()
input_dir = args.input_dir
output_dir = args.output_dir or input_dir
assert os.path.isdir(output_dir), f"{output_dir} is not a directory"
if args.to_sqlite:
input: MetadataStore = FilesystemMetadataStore(input_dir)
output: MetadataStore = SqliteMetadataStore(output_dir)
else:
fnam = os.path.join(input_dir, "cache.db")
msg = f"{fnam} does not exist"
if not re.match(r"[0-9]+\.[0-9]+$", os.path.basename(input_dir)):
msg += f" (are you missing Python version at the end, e.g. {input_dir}/3.11)"
assert os.path.isfile(fnam), msg
input, output = SqliteMetadataStore(input_dir), FilesystemMetadataStore(output_dir)
for s in input.list_all():
if s.endswith(".json"):
assert output.write(
s, input.read(s), input.getmtime(s)
), f"Failed to write cache file {s}!"
output.commit()
if __name__ == "__main__":
main()