diff --git a/src/lib/snapshot/diff.py b/src/lib/snapshot/diff.py new file mode 100644 index 0000000..9832068 --- /dev/null +++ b/src/lib/snapshot/diff.py @@ -0,0 +1,53 @@ +import os + +def recursive_raw_diff(tree_a, tree_b, tree_a_tag, tree_b_tag, path, res): + a_keys_not_in_b = set(tree_a.keys()) - set(tree_b.keys()) + # b_keys_not_in_a = set(tree_b.keys()) - set(tree_a.keys()) + ## name last is a dir + if a_keys_not_in_b: + for key in a_keys_not_in_b: + print(f'node {path}{key} subtree added in {tree_a_tag}') + res[f'{path}{key}'] = { + tree_a_tag : 'x', + } + + for key_a, hsh_a in tree_a.items(): + hsh_b = tree_b.get(key_a) + + if hsh_b is None: + ''' + already in `a_keys_not_in_b` + ''' + continue + + + + if isinstance(hsh_a, str): + if hsh_a == hsh_b: continue + + res[f'{path}{key_a}'] = { + tree_a_tag : 'x', + tree_b_tag : 'x', + } + + res[f'{path}{key_a}']['%s_hash' %(tree_a_tag,)] = hsh_a + res[f'{path}{key_a}']['%s_hash' %(tree_b_tag,)] = hsh_b + continue + + + recursive_raw_diff(hsh_a, hsh_b,tree_a_tag, tree_b_tag, '%s%s/' % (path, key_a,), res) + +def raw_diff(tree_a, tree_b, tree_a_tag='tree_a', tree_b_tag='tree_b', path='./'): + ''' + it returns a dict where the key is the relative path of node givin these informations + + - node present in tree_a + - node present in tree_b + - hash is the same or differ + ''' + res = {} + if not path.endswith(os.sep): path = path + os.sep + recursive_raw_diff(tree_a, tree_b, tree_a_tag=tree_a_tag, tree_b_tag=tree_b_tag, path=path, res=res) + ## note the tree inversion + recursive_raw_diff(tree_b, tree_a, tree_a_tag=tree_b_tag, tree_b_tag=tree_a_tag, path=path, res=res) + return res diff --git a/src/testing/rawdiff.py b/src/testing/rawdiff.py new file mode 100644 index 0000000..65db63b --- /dev/null +++ b/src/testing/rawdiff.py @@ -0,0 +1,12 @@ +from lib.snapshot import diff + + + +tree_a = {'taglioCapelli4.jpg': '5af62402ec7716e8d729b0683061f0c4', 'taglioCapelli5.jpg': '9c42961af589a279c4c828925291153b', 'pino.txt': 'd41d8cd98f00b204e9800998ecf8427e', 'taglioCapelli6.jpg': '4059905eab817c33ee48f912af80fdb7', 'spartiti_sepu': {'amber.py': 'a6ef147511264ecdc7d6061979892c44', 'bo.txt': 'd7aa6cbc6aa0ace3225ee1738b2d5c6b', 'ciao': {'amber.py': 'a6ef147511264ecdc7d6061979892c44', 'bo.txt': 'd7aa6cbc6aa0ace3225ee1738b2d5c6b'},'added_folder': {}}, 'preferiti.txt': 'af45981ef2be534dbb37f96833d3fd04'} + +tree_b = {'taglioCapelli4.jpg': '5af62402ec7716e8d729b0683061f0c4', 'taglioCapelli5.jpg': '9c42961af589a279c4c828925291153b', 'pino.txt': 'd41d8cd98f00b204e9800998ecf8427e', 'taglioCapelli6.jpg': '4059905eab817c33ee48f912af80fdb7', 'spartiti_sepu': {'amber.py': 'a6ef147511264ecdc7d6061979892c44', 'bo.txt': 'd7aa6cbc6aa0ace3225ee1738b2d5c6b', 'added_folder': {'preferiti.txt' : 'af45981ef2be534dbb37f96833d3fd03'}}, 'preferiti.txt': 'af45981ef2be534dbb37f9fffffffff'} + + + +def test_raw_diff(): + return diff.raw_diff(tree_a, tree_b)