diff --git a/src/client/__pycache__/agent.cpython-39.pyc b/src/client/__pycache__/agent.cpython-39.pyc index d0237ba..569b845 100644 Binary files a/src/client/__pycache__/agent.cpython-39.pyc and b/src/client/__pycache__/agent.cpython-39.pyc differ diff --git a/src/client/agent.py b/src/client/agent.py index 92e55a6..8036900 100644 --- a/src/client/agent.py +++ b/src/client/agent.py @@ -65,3 +65,5 @@ class SyncAgent(): print('name: %s is regular FILE' % (attr.filename)) +synca = _agent.SyncAgent() +# sftpc = a.get_sftp_client() diff --git a/src/masync.py b/src/masync.py index a3e47d2..b5559fd 100755 --- a/src/masync.py +++ b/src/masync.py @@ -1,3 +1,10 @@ if __name__ == '__main__': print('python masync script: #!/usr/bin/env python ') + + ## suppose last tree is + + last_local_tree_hash = {'taglioCapelli4.jpg': '5af62402ec7716e8d729b0683061f0c4', 'taglioCapelli5.jpg': '9c42961af589a279c4c828925291153b', 'pino.txt': 'd41d8cd98f00b204e9800998ecf8427e', 'taglioCapelli6.jpg': '4059905eab817c33ee48f912af80fdb7', 'spartiti_sepu': {'IMG_20220626_081839.jpg': '3c14e508124c928d59b393a571e2f751', 'IMG_20220626_081951.jpg': 'd484638ac09cbe40f8753de3f1b3c4a6'}, 'preferiti.txt': 'af45981ef2be534dbb37f96833d3fd04'} + + from snapshot.generate import local as _genlocal + local_tree_hash = _genlocal.generate_tree_hash('/home/luca/sharednotes_dev/') diff --git a/src/snapshot/__pycache__/dump.cpython-39.pyc b/src/snapshot/__pycache__/dump.cpython-39.pyc index ba5ce7b..f3ba4fc 100644 Binary files a/src/snapshot/__pycache__/dump.cpython-39.pyc and b/src/snapshot/__pycache__/dump.cpython-39.pyc differ diff --git a/src/snapshot/__pycache__/snap.cpython-39.pyc b/src/snapshot/__pycache__/snap.cpython-39.pyc index 5281cf8..444dfc6 100644 Binary files a/src/snapshot/__pycache__/snap.cpython-39.pyc and b/src/snapshot/__pycache__/snap.cpython-39.pyc differ diff --git a/src/snapshot/dump.py b/src/snapshot/dump.py index a5f8798..b40c6ad 100644 --- a/src/snapshot/dump.py +++ b/src/snapshot/dump.py @@ -1,13 +1,13 @@ import gzip import json -from snapshot import gen as _gen +from snapshot.generate import local as _genlocal DUMP_FILE_NAME = '.snapshot.json.gz' SNAPSHOT_PATH = '../' def dump_snapshot(snapshot, path=None, dump_file_name=None): path = path or SNAPSHOT_PATH - path = _gen.check_isdir(path) + path = _genlocal.check_isdir(path) dump = json.dumps(snapshot) dump = gzip.compress(dump.encode()) @@ -20,7 +20,7 @@ def dump_snapshot(snapshot, path=None, dump_file_name=None): def decode_snapshot(path=None, dump_file_name=None): path = path or SNAPSHOT_PATH - path = _gen.check_isdir(path) + path = _genlocal.check_isdir(path) dump_file_name = dump_file_name or DUMP_FILE_NAME diff --git a/src/snapshot/generate/__pycache__/local.cpython-39.pyc b/src/snapshot/generate/__pycache__/local.cpython-39.pyc new file mode 100644 index 0000000..3357391 Binary files /dev/null and b/src/snapshot/generate/__pycache__/local.cpython-39.pyc differ diff --git a/src/snapshot/generate/__pycache__/remote.cpython-39.pyc b/src/snapshot/generate/__pycache__/remote.cpython-39.pyc new file mode 100644 index 0000000..861dfa3 Binary files /dev/null and b/src/snapshot/generate/__pycache__/remote.cpython-39.pyc differ diff --git a/src/snapshot/gen.py b/src/snapshot/generate/local.py similarity index 100% rename from src/snapshot/gen.py rename to src/snapshot/generate/local.py diff --git a/src/snapshot/generate/remote.py b/src/snapshot/generate/remote.py new file mode 100644 index 0000000..a62c338 --- /dev/null +++ b/src/snapshot/generate/remote.py @@ -0,0 +1,84 @@ +import stat +import os +import hashlib +import json +import gzip + +from client import agent as _agent + + +def generate_rfile_hash(file_path, hexdigest=True, client=None): + if not client: + a = _agent.synca + client = a.get_sftp_client() + + with client.open(file_path, "rb") as f: + buf = f.read() + + if hexdigest: return hashlib.md5(buf).hexdigest() + return hashlib.md5(buf).digest() + +# def check_isdir(path: str): +# if not os.path.isdir(path): +# raise Exception('Provide a valid folder to start the hashing') +# +# if not path.endswith(os.path.sep): +# path = path + os.path.sep +# return path + +def generate_tree_hash_oversftp(root_path :str): + ''' + @param root_path string, root_path in remote server + generate a map of hashes starting from `root_path` recursively + ''' + # if not os.path.isdir(root_path): + # raise Exception('Provide a valid folder to start the hashing') + # + # if not root_path.endswith(os.path.sep): + # root_path = root_path + os.path.sep + # root_path = check_isdir(root_path) + + rtreemap = {} + + if not root_path.endswith(os.path.sep): + root_path = root_path + os.path.sep + + a = _agent.synca + sftpc = a.get_sftp_client() + + for item in sftpc.listdir_attr(root_path): + absolute_item_path = root_path + item.filename + print('absolute_item_path: %s, item %s, isdir: %s' % (absolute_item_path, item.filename, stat.S_ISDIR(item.st_mode))) + if stat.S_ISDIR(item.st_mode): + rtreemap[item.filename] = generate_tree_hash_oversftp(absolute_item_path) + else: + rtreemap[item.filename] = generate_rfile_hash(absolute_item_path, client=sftpc) + + return rtreemap + + +''' +a + - b + - c + - k.txt + i.txt + g.txt +j.txt +k.txt + +tree['a'] = { + 'b' : { + + }, + 'j.txt' : '012349jasdfh9934', + + + } + +''' + +root_path = '/home/luca/rsyn_test_fap' + + + diff --git a/src/snapshot/snap.py b/src/snapshot/snap.py index d3b67a7..961e081 100644 --- a/src/snapshot/snap.py +++ b/src/snapshot/snap.py @@ -1,8 +1,8 @@ -from snapshot import gen as _gen from snapshot import dump as _dump +from snapshot.generate import local as _genlocal def dmpsnap(root_tree: str): - snapshot = _gen.generate_recursive_filemap_hash(root_tree) + snapshot = _genlocal.generate_recursive_filemap_hash(root_tree) _dump.dump_snapshot(snapshot) def dcsnap(path: str, filename=None):