lib/snapshot/diff

This commit is contained in:
Amber 2024-12-16 17:10:25 +01:00
parent 50d7d069d8
commit daebf01225
2 changed files with 78 additions and 25 deletions

View File

@ -1,5 +1,6 @@
import os
from lib.snapshot import diff as _diff
from lib.snapshot import dump as _dump
from lib.snapshot.generate import local as _genlocal
@ -107,31 +108,81 @@ def recursive_diff_snap(last_tree, current_tree, path='./', bres={}):
'changed' : changed,
})
def diff_snap(last_tree, current_tree, path='./'):
res = {}
if not path.endswith(os.sep): path = path + os.sep
recursive_diff_snap(last_tree, current_tree, path=path, bres=res)
# return res
## Managing moved files is too difficult - for now skip this step
# compute moved could save bandwidth
res['moved'] = []
for n, r in enumerate(res['removed']):
if r.get('hash') is None:
continue
found, path = find_subtree(current_tree, {
r['name'] : r['hash'],
})
if not found: continue
res['moved'].append({
'name' : r['name'],
'old_path' : r['path'],
'new_path' : path,
'hash' : r['hash'],
})
# def diff_snap(last_tree, current_tree, path='./'):
# res = {}
# if not path.endswith(os.sep): path = path + os.sep
# recursive_diff_snap(last_tree, current_tree, path=path, bres=res)
# # return res
# ## Managing moved files is too difficult - for now skip this step
# # compute moved could save bandwidth
# res['moved'] = []
# for n, r in enumerate(res['removed']):
# if r.get('hash') is None:
# continue
# found, path = find_subtree(current_tree, {
# r['name'] : r['hash'],
# })
# if not found: continue
# res['moved'].append({
# 'name' : r['name'],
# 'old_path' : r['path'],
# 'new_path' : path,
# 'hash' : r['hash'],
# })
#
# del res['removed'][n]
#
# return res
def diff_snap(last_tree, current_tree, path='./'):
tree_diff = _diff.raw_diff(last_tree, current_tree, tree_a_tag='last', tree_b_tag='cur')
## we need 3 keys
## 'removed', 'changed', 'added'
removed = []
added = []
changed = []
for node in tree_diff.keys():
## node must have a structure as reported in lib/repr/node.py
##
last_index = node.rfind(os.sep)
node_name = node[last_index+1:]
node_rel_path = node[:last_index+1]
noded = tree_diff[node]
n = {
'name' : node_name,
'rel_path': node_rel_path,
}
if noded.get('last_type') is not None:
n['last_type'] = noded['last_type']
if noded.get('cur_type') is not None:
n['cur_type'] = noded['cur_type']
if noded.get('last_hash') is not None:
n['last_hash'] = noded['last_hash']
if noded.get('cur_hash') is not None:
n['cur_hash'] = noded['cur_hash']
if not noded.get('cur') and noded.get('last'):
removed.append(n)
if not noded.get('last') and noded.get('cur'):
added.append(n)
if noded.get('last') and noded.get('cur'):
changed.append(n)
return {
'removed' : removed,
'changed' : changed,
'added' : added,
}
del res['removed'][n]
return res
def find_subtree(snapshot, subtree, path='./'):
'''

View File

@ -8,10 +8,10 @@ def recursive_raw_diff(tree_a, tree_b, tree_a_tag, tree_b_tag, path, res):
res[f'{path}{key_a}'] = {
tree_a_tag : 'x',
}
res[f'{path}{key_a}']['%s_type' % (tree_a_tag,)] = 'd' if isinstance(hsh_a, dict) else 'f'
continue
if isinstance(hsh_a, str):
if hsh_a == hsh_b: continue
@ -22,6 +22,8 @@ def recursive_raw_diff(tree_a, tree_b, tree_a_tag, tree_b_tag, path, res):
res[f'{path}{key_a}']['%s_hash' %(tree_a_tag,)] = hsh_a
res[f'{path}{key_a}']['%s_hash' %(tree_b_tag,)] = hsh_b
res[f'{path}{key_a}']['%s_type' %(tree_a_tag,)] = tree_a_type
res[f'{path}{key_a}']['%s_type' %(tree_b_tag,)] = 'd' if isinstance(hsh_b, dict) else 'f'
continue