starting python core
This commit is contained in:
parent
99f76f796c
commit
519ade0e99
BIN
.snapshot.json.gz
Normal file
BIN
.snapshot.json.gz
Normal file
Binary file not shown.
1
src/.hashes
Normal file
1
src/.hashes
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"caff\u00e8": 1, "briosche": 2}
|
Binary file not shown.
@ -1,8 +0,0 @@
|
|||||||
import hashlib
|
|
||||||
|
|
||||||
def generate_file_hash(file_path, hexdigest=True):
|
|
||||||
with open(file_path, "rb") as f:
|
|
||||||
buf = f.read()
|
|
||||||
|
|
||||||
if hexdigest: return hashlib.md5(buf).hexdigest()
|
|
||||||
return hashlib.md5(buf).digest()
|
|
BIN
src/snapshot/__pycache__/dump.cpython-39.pyc
Normal file
BIN
src/snapshot/__pycache__/dump.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/snapshot/__pycache__/gen.cpython-39.pyc
Normal file
BIN
src/snapshot/__pycache__/gen.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/snapshot/__pycache__/snap.cpython-39.pyc
Normal file
BIN
src/snapshot/__pycache__/snap.cpython-39.pyc
Normal file
Binary file not shown.
32
src/snapshot/dump.py
Normal file
32
src/snapshot/dump.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import gzip
|
||||||
|
import json
|
||||||
|
from snapshot import gen as _gen
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
dump = json.dumps(snapshot)
|
||||||
|
dump = gzip.compress(dump.encode())
|
||||||
|
|
||||||
|
dump_file_name = dump_file_name or DUMP_FILE_NAME
|
||||||
|
|
||||||
|
file = '%s%s' % (path, dump_file_name)
|
||||||
|
with open(file, "wb") as f:
|
||||||
|
f.write(dump)
|
||||||
|
|
||||||
|
def decode_snapshot(path=None, dump_file_name=None):
|
||||||
|
path = path or SNAPSHOT_PATH
|
||||||
|
path = _gen.check_isdir(path)
|
||||||
|
|
||||||
|
dump_file_name = dump_file_name or DUMP_FILE_NAME
|
||||||
|
|
||||||
|
file = '%s%s' % (path, dump_file_name)
|
||||||
|
with open(file, "rb") as f:
|
||||||
|
buf = f.read()
|
||||||
|
|
||||||
|
buf = gzip.decompress(buf)
|
||||||
|
return json.loads(buf)
|
72
src/snapshot/gen.py
Normal file
72
src/snapshot/gen.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
import os
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import gzip
|
||||||
|
|
||||||
|
|
||||||
|
def generate_file_hash(file_path, hexdigest=True):
|
||||||
|
with 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(root_path :str):
|
||||||
|
'''
|
||||||
|
@param root_path string
|
||||||
|
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)
|
||||||
|
|
||||||
|
treemap = {}
|
||||||
|
|
||||||
|
items = os.listdir(root_path)
|
||||||
|
for item in items:
|
||||||
|
absolute_item_path = root_path + item
|
||||||
|
print('absolute_item_path: %s, item %s, isdir: %s' % (absolute_item_path, item, os.path.isdir(absolute_item_path)))
|
||||||
|
if os.path.isdir(absolute_item_path):
|
||||||
|
treemap[item] = generate_tree_hash(absolute_item_path)
|
||||||
|
else:
|
||||||
|
treemap[item] = generate_file_hash(absolute_item_path)
|
||||||
|
|
||||||
|
return treemap
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
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'
|
||||||
|
|
||||||
|
|
||||||
|
|
34
src/snapshot/snap.py
Normal file
34
src/snapshot/snap.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from snapshot import gen as _gen
|
||||||
|
from snapshot import dump as _dump
|
||||||
|
|
||||||
|
def dmpsnap(root_tree: str):
|
||||||
|
snapshot = _gen.generate_recursive_filemap_hash(root_tree)
|
||||||
|
_dump.dump_snapshot(snapshot)
|
||||||
|
|
||||||
|
def dcsnap(path: str, filename=None):
|
||||||
|
return _dump.decode_snapshot(path, dump_file_name = filename)
|
||||||
|
|
||||||
|
|
||||||
|
def diffsnap(snapshot_a, snapshot_b, path='./'):
|
||||||
|
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 {hsh_b} removed in snapshot_b')
|
||||||
|
continue
|
||||||
|
if type(hsh_a) != type(hsh_a):
|
||||||
|
print(f'{path}{name_a} present in snapshot_a and in snapshot_b, but different 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')
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user