-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2jlap.py
164 lines (127 loc) · 4.29 KB
/
json2jlap.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Simple json to jlap "*/repodata.json" -> "*/repodata.jlap tool.
Copy */repodata.json to */.cache/repodata.json.last
Read */repodata.jlap
Diff */repodata.json with */.cache/repodata.json
Write */repodata.jlap
Same for current_repodata.jlap
If output jlap is larger than a set size, remove older diffs.
"""
from __future__ import annotations
import itertools
import json
import logging
import shutil
from hashlib import blake2b
from io import IOBase
from pathlib import Path
import click
import jsonpatch
from truncateable import JlapReader, JlapWriter
from jlaptrim import trim_if_larger
log = logging.getLogger("__name__")
# attempt to control individual patch size (will fall back to re-downloading
# repodata.json) without serializing to bytes just to measure
PATCH_STEPS_LIMIT = 8192
DIGEST_SIZE = 32
def hfunc(data: bytes):
return blake2b(data, digest_size=DIGEST_SIZE)
class HashReader:
"""
Hash a file while it is being read.
"""
def __init__(self, fp: IOBase):
self.fp = fp
self.hash = blake2b(digest_size=DIGEST_SIZE)
def read(self, bytes=None):
data = self.fp.read(bytes)
self.hash.update(data)
return data
def hash_and_load(path):
with path.open("rb") as fp:
h = HashReader(fp)
obj = json.load(h)
return obj, h.hash.digest()
def json2jlap_one(cache: Path, repodata: Path):
previous_repodata = cache / (repodata.name + ".last")
patches = []
jlapfile = (repodata.parent / repodata.name).with_suffix(".jlap")
if jlapfile.exists():
with jlapfile.open("rb") as jlap:
patchfile = JlapReader(jlap)
*patches, metadata = list(patch for patch, _ in patchfile.readobjs())
if (
previous_repodata.exists()
and repodata.stat().st_mtime > previous_repodata.stat().st_mtime
):
current, current_digest = hash_and_load(repodata)
previous, previous_digest = hash_and_load(previous_repodata)
jpatch = jsonpatch.make_patch(previous, current)
# inconvenient to add bytes size limit here; limit number of steps?
if previous_digest == current_digest:
log.warn("Skip identical %s", repodata)
elif len(jpatch.patch) > PATCH_STEPS_LIMIT:
log.warn("Skip large %s-step patch", len(jpatch.patch))
else:
patches.append(
{
"to": current_digest.hex(),
"from": previous_digest.hex(),
"patch": jpatch.patch,
}
)
# metadata
patches.append({"url": repodata.name, "latest": current_digest.hex()})
with jlapfile.open("wb+") as jlap:
patchfile = JlapWriter(jlap)
for patch in patches:
patchfile.write(patch)
patchfile.finish()
if (
not previous_repodata.exists()
or repodata.stat().st_mtime > previous_repodata.stat().st_mtime
):
shutil.copyfile(repodata, previous_repodata)
@click.command()
@click.option("--cache", required=True, help="Cache directory.")
@click.option("--repodata", required=True, help="Repodata directory.")
@click.option(
"--trim-low",
required=False,
default=2**20 * 3,
show_default=True,
help="Maximum size after trim.",
)
@click.option(
"--trim-high",
required=False,
default=0,
show_default=True,
help="Trim if larger than size; 0 to disable.",
)
def json2jlap(cache, repodata, trim_low, trim_high):
cache = Path(cache).expanduser()
repodata = Path(repodata).expanduser()
repodatas = itertools.chain(
repodata.glob("*/repodata.json"), repodata.glob("*/current_repodata.json")
)
for repodata in repodatas:
# require conda-index's .cache folder
cachedir = Path(cache, repodata.parent.name, ".cache")
if not cachedir.is_dir():
continue
json2jlap_one(cachedir, repodata)
if trim_high > trim_low:
repodata_jlap = repodata.with_suffix(".jlap")
if not repodata_jlap.exists():
continue
trim_if_larger(trim_high, trim_low, repodata_jlap)
def go():
logging.basicConfig(
format="%(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
level=logging.INFO,
)
json2jlap()
if __name__ == "__main__":
go()