-
Notifications
You must be signed in to change notification settings - Fork 18
/
tree.py
348 lines (275 loc) · 9.71 KB
/
tree.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import json
import logging
import posixpath
from typing import TYPE_CHECKING, Dict, Final, Iterable, Optional, Tuple
from dvc_objects.errors import ObjectFormatError
from dvc_objects.obj import Object
from funcy import cached_property
from ..hashfile.hash import hash_file
from ..hashfile.meta import Meta
from ..hashfile.obj import HashFile
if TYPE_CHECKING:
from dvc_objects.db import ObjectDB
from ..hashfile.hash_info import HashInfo
from ..index import BaseDataIndex, DataIndexKey
logger = logging.getLogger(__name__)
class TreeError(Exception):
pass
class MergeError(Exception):
pass
def _try_load(
odbs: Iterable["ObjectDB"],
hash_info: "HashInfo",
) -> Optional["Object"]:
for odb in odbs:
if not odb:
continue
try:
return Tree.load(odb, hash_info)
except (FileNotFoundError, ObjectFormatError):
pass
return None
class Tree(HashFile):
PARAM_RELPATH: Final = "relpath"
def __init__(self): # pylint: disable=super-init-not-called
self.fs = None
self.path = None
self.hash_info = None
self.oid = None
self._dict: Dict[
Tuple[str, ...], Tuple[Optional["Meta"], "HashInfo"]
] = {}
@cached_property
def _trie(self):
from pygtrie import Trie
return Trie(self._dict)
def add(
self, key: Tuple[str, ...], meta: Optional["Meta"], oid: "HashInfo"
):
self.__dict__.pop("_trie", None)
self._dict[key] = (meta, oid)
def get(
self, key: Tuple[str, ...], default=None
) -> Optional[Tuple[Optional["Meta"], "HashInfo"]]:
return self._dict.get(key, default)
def digest(self):
from dvc_objects.fs import MemoryFileSystem
from dvc_objects.fs.utils import tmp_fname
memfs = MemoryFileSystem()
path = "memory://{}".format(tmp_fname(""))
memfs.pipe_file(path, self.as_bytes())
self.fs = memfs
self.path = path
_, self.hash_info = hash_file(path, memfs, "md5")
assert self.hash_info.value
self.hash_info.value += ".dir"
self.oid = self.hash_info.value
def _load(self, key, meta, hash_info):
if hash_info and hash_info.isdir and not meta.obj:
meta.obj = _try_load([meta.odb, meta.remote], hash_info)
if meta.obj:
for ikey, value in meta.obj.iteritems():
self._trie[key + ikey] = value
self._dict[key + ikey] = value
def iteritems(self, prefix=None):
kwargs = {}
if prefix:
kwargs = {"prefix": prefix}
item = self._trie.longest_prefix(prefix)
if item:
key, (meta, hash_info) = item
self._load(key, meta, hash_info)
for key, (meta, hash_info) in self._trie.iteritems(**kwargs):
self._load(key, meta, hash_info)
yield key, (meta, hash_info)
def shortest_prefix(self, *args, **kwargs):
return self._trie.shortest_prefix(*args, **kwargs)
def __len__(self):
return len(self._dict)
def __iter__(self):
yield from (
(key, value[0], value[1]) for key, value in self._dict.items()
)
def as_dict(self):
return self._dict.copy()
def as_list(self, with_meta: bool = False):
from operator import itemgetter
# Sorting the list by path to ensure reproducibility
return sorted(
(
{
**(meta.to_dict() if with_meta else {}),
**hi.to_dict(),
self.PARAM_RELPATH: posixpath.sep.join(parts),
}
for parts, meta, hi in self # noqa: B301
),
key=itemgetter(self.PARAM_RELPATH),
)
def as_bytes(self):
return json.dumps(self.as_list(), sort_keys=True).encode("utf-8")
@classmethod
def from_list(cls, lst, hash_name: Optional[str] = None):
from ..hashfile.hash_info import HashInfo
tree = cls()
for _entry in lst:
entry = _entry.copy()
relpath = entry.pop(cls.PARAM_RELPATH)
parts = tuple(relpath.split(posixpath.sep))
meta = Meta.from_dict(entry)
if hash_name:
hash_info = HashInfo(hash_name, getattr(meta, hash_name))
else:
hash_info = HashInfo.from_dict(entry)
tree.add(parts, meta, hash_info)
return tree
@classmethod
def load(cls, odb, hash_info) -> "Tree":
obj = odb.get(hash_info.value)
try:
with obj.fs.open(obj.path, "r") as fobj:
raw = json.load(fobj)
except ValueError as exc:
raise ObjectFormatError(f"{obj} is corrupted") from exc
if not isinstance(raw, list):
logger.debug(
"dir cache file format error '%s' [skipping the file]",
obj.path,
)
raise ObjectFormatError(f"{obj} is corrupted")
tree = cls.from_list(raw)
tree.path = obj.path
tree.fs = obj.fs
tree.hash_info = hash_info
tree.oid = hash_info.value
return tree
def filter(self, prefix: Tuple[str]) -> Optional["Tree"]:
"""Return a filtered copy of this tree that only contains entries
inside prefix.
The returned tree will contain the original tree's hash_info and
path.
Returns an empty tree if no object exists at the specified prefix.
"""
tree = Tree()
tree.path = self.path
tree.fs = self.fs
tree.hash_info = self.hash_info
tree.oid = self.oid
try:
for key, (meta, oid) in self._trie.items(prefix):
tree.add(key, meta, oid)
except KeyError:
pass
return tree
def get_obj(self, odb, prefix: Tuple[str]) -> Optional[Object]:
"""Return object at the specified prefix in this tree.
Returns None if no object exists at the specified prefix.
"""
_, hi = self._dict.get(prefix) or (None, None)
if hi:
return odb.get(hi.value)
tree = Tree()
depth = len(prefix)
try:
for key, (meta, entry_oid) in self._trie.items(prefix):
tree.add(key[depth:], meta, entry_oid)
except KeyError:
return None
tree.digest()
return tree
def ls(self, prefix=None):
kwargs = {}
if prefix:
kwargs["prefix"] = prefix
meta, hash_info = self._trie.get(prefix, (None, None))
if hash_info and hash_info.isdir and meta and not meta.obj:
raise TreeError
ret = []
def node_factory(_, key, children, *args):
if key == prefix:
list(children)
else:
ret.append(key[-1])
self._trie.traverse(node_factory, **kwargs)
return ret
def du(odb, tree):
try:
return sum(
odb.fs.size(odb.oid_to_path(oid.value)) for _, _, oid in tree
)
except FileNotFoundError:
return None
def _diff(ancestor, other, allowed=None):
from dictdiffer import diff
if not allowed:
allowed = ["add"]
result = list(diff(ancestor, other))
for typ, _, _ in result:
if typ not in allowed:
raise MergeError(
"unable to auto-merge directories with diff that contains "
f"'{typ}'ed files"
)
return result
def _merge(ancestor, our, their, allowed=None):
import copy
from dictdiffer import diff, patch
our_diff = _diff(ancestor, our, allowed=allowed)
if not our_diff:
return copy.deepcopy(their)
their_diff = _diff(ancestor, their, allowed=allowed)
if not their_diff:
return copy.deepcopy(our)
# make sure there are no conflicting files
try:
patch_ours_first = patch(our_diff + their_diff, ancestor)
patch_theirs_first = patch(their_diff + our_diff, ancestor)
except KeyError as e:
# todo: fails if both diffs delete the same object
raise MergeError(
"unable to auto-merge the following paths:" f"\nboth deleted: {e}"
)
unmergeable = list(diff(patch_ours_first, patch_theirs_first))
if unmergeable:
unmergeable_paths = []
for paths in patch(unmergeable, {}):
unmergeable_paths.append(posixpath.join(*paths))
raise MergeError(
"unable to auto-merge the following paths:\n"
+ "\n".join(unmergeable_paths)
)
return patch_ours_first
def merge(odb, ancestor_info, our_info, their_info, allowed=None):
from . import load
assert our_info
assert their_info
if ancestor_info:
ancestor = load(odb, ancestor_info)
else:
ancestor = Tree()
our = load(odb, our_info)
their = load(odb, their_info)
merged_dict = _merge(
ancestor.as_dict(), our.as_dict(), their.as_dict(), allowed=allowed
)
merged = Tree()
for key, (meta, oid) in merged_dict.items():
merged.add(key, meta, oid)
merged.digest()
return merged
def tree_from_index(
index: "BaseDataIndex",
prefix: "DataIndexKey",
) -> Tuple["Meta", "Tree"]:
tree_meta = Meta(size=0, nfiles=0, isdir=True)
assert tree_meta.size is not None and tree_meta.nfiles is not None
tree = Tree()
for key, entry in index.iteritems(prefix=prefix):
if key == prefix or entry.meta and entry.meta.isdir:
continue
assert entry.meta and entry.hash_info
tree_key = key[len(prefix) :]
tree.add(tree_key, entry.meta, entry.hash_info)
tree_meta.size += entry.meta.size or 0
tree_meta.nfiles += 1
return tree_meta, tree