This commit is contained in:
Amber 2023-12-07 10:08:15 +01:00
parent 14771e92b3
commit 6d46b83fe4
6 changed files with 65 additions and 25 deletions

View File

@ -27,6 +27,7 @@ class SyncAgent():
self.pkey = paramiko.RSAKey.from_private_key_file(PKEY_PATH)
self.client = paramiko.SSHClient()
self.client.load_host_keys(filename=HOSTS_KEYS_PATH)
self.sftpc = None
def connect(self):
'''
@ -49,11 +50,14 @@ class SyncAgent():
return ostream.strip()
def get_sftp_client(self):
if self.sftpc:
return self.sftpc
if not self.client.get_transport():
print('Transport not present proceed to create it... ')
self.connect()
print('--> OK!')
return self.client.open_sftp()
self.sftpc = self.client.open_sftp()
return self.sftpc
def test_is_dir(self, path='.'):
sftpc = self.get_sftp_client()

View File

@ -120,13 +120,13 @@ async def generate_tree_hash_oversftp_async(root_path :str):
await asyncio.gather(*tasks)
return rtreemap
def test_sync_rtree():
start = time.time()
print('Start task')
rtree = generate_tree_hash_oversftp('/home/notanamber/notes_dev/')
end = time.time()
print('task done in %.2f' % (end - start))
return rtree
def test_async_rtree():
start = time.time()
@ -134,6 +134,7 @@ def test_async_rtree():
rtree = asyncio.run(generate_tree_hash_oversftp_async('/home/notanamber/notes_dev/'))
end = time.time()
print('task done in %.2f' % (end - start))
return rtree

View File

@ -8,36 +8,71 @@ def dmpsnap(root_tree: str):
def dcsnap(path: str, filename=None):
return _dump.decode_snapshot(path, dump_file_name = filename)
def diffsnap(snapshot_a, snapshot_b, path='./'):
def diffsnap(last_tree, current_tree, path='./', removed=[], changed=[], added=[]):
'''
snapshot is a tree represented by dictionary {k, val} when k can be folder or file
and val can be a dictionary or an hash
@param snapshot_a snapshot computed at time t0
@param snapshot_b snapshot computed at time t1
@param last_tree snapshot computed at time t0
@param current_tree snapshot computed at time t1
'''
for name_a, hsh_a in snapshot_a.items():
hsh_b = snapshot_b.get(name_a)
if hsh_b is None:
print(f'{path}{name_a} present in snapshot_a but removed in snapshot_b, subtree {name_a} removed in snapshot_b')
removed = []
changed = []
added = []
for name_last, hsh_last in last_tree.items():
hsh_current = current_tree.get(name_last)
# is_dir_last = True if isinstance(hsh_last, dict) else False
last_type = 'dir' if isinstance(hsh_last, dict) else 'file'
item = {
'name' : name_last,
'path' : path,
}
if hsh_current is None:
## the name in not founded in current snapshot - Subtree removed (or moved)
print(f'{path}{name_last} present in last_tree but removed in current_tree, subtree {name_last} removed in current_tree')
removed.append(item.update({
'last_type' : 'dir',
}))
continue
if type(hsh_a) != type(hsh_a):
print(f'{path}{name_a} present in snapshot_a and in snapshot_b, but different type')
current_type = 'dir' if isinstance(hsh_current, dict) else 'file'
if type(hsh_last) != type(hsh_current):
print(f'{path}{name_last} changed his type in {current_type}')
changed.append(item.update({
'last_type' : last_type,
'current_type' : current_type,
}))
continue
if isinstance(hsh_a, dict):
# name is dir
diffsnap(hsh_a, hsh_b, path='%s%s/' % (path,name_a))
else:
# name is a file
if hsh_b is None:
print(f'{path}{name_a} present in snapshot_a but not in snapshot_b, removed in snapshot_b')
continue
if hsh_a != hsh_b:
print(f'{path}{name_a} present in snapshot_a but changed in snapshot_b, snapshot_a: {hsh_a}, snapshot_b: {hsh_b}')
continue
else:
print(f'{path}{name_a} present in snapshot_a and in snapshot_b, snapshot_a: {hsh_a}, snapshot_b: {hsh_b}, unchanged')
if isinstance(hsh_last, str):
if hsh_last != hsh_current:
print(f'file {path}{name_last} changed his hash')
changed.append(item.update({
'last_type' : last_type,
'current_type' : current_type,
'last_hash' : hsh_last,
'cur_hash' : hsh_current,
}))
continue
# name is dir
if (hsh_last == hsh_current):
print(f'file {path}{name_last} subtree unchanged ')
continue
print(f'file {path}{name_last} subtree changed in current snapshot')
keys_added = set(hsh_current.keys()) - set(hsh_last.keys())
## name last is a dir
if keys_added:
for key_added in keys_added:
print(f'file {path}{name_last}/{key_added} subtree added in current snapshot')
diffsnap(hsh_last, hsh_current, path='%s%s/' % (path,name_last))
def findsubtree(snapshot, subtree, path='./'):
'''