This commit is contained in:
Amber 2023-12-28 12:27:46 +01:00
parent 523c4e6b81
commit 9af0458960
11 changed files with 108 additions and 29 deletions

Binary file not shown.

3
src/conf/snapshot.conf Normal file
View File

@ -0,0 +1,3 @@
[snapshot]
dump_filename .snapshot.json.gz
dump_path /home/luca/

View File

@ -1,14 +1,27 @@
from lib.snapshot import dump as _dump
from lib.snapshot.generate import local as _genlocal
def dmpsnap(root_tree: str):
snapshot = _genlocal.generate_recursive_filemap_hash(root_tree)
_dump.dump_snapshot(snapshot)
from tools import parse_conf as _pconf
def dcsnap(path: str, filename=None):
return _dump.decode_snapshot(path, dump_file_name = filename)
def dump_snap(root_tree: str):
snapshot = _genlocal.generate_tree_hash(root_tree)
def diffsnap(last_tree, current_tree, path='./', bres={}):
conf = _pconf.read_conf('./conf/snapshot.conf')
snap_path = conf.get('dump_path')
filename = conf.get('dump_filename')
_dump.dump_snapshot(snapshot, path = snap_path, dump_file_name=filename)
def decode_snap():
conf = _pconf.read_conf('./conf/snapshot.conf')
snap_path = conf.get('dump_path')
filename = conf.get('dump_filename')
return _dump.decode_snapshot(path=snap_path, dump_file_name = filename)
def diff_snap(last_tree, current_tree, path='./', bres={}):
'''
snapshot is a tree represented by dictionary {k, val} when k can be folder or file name
and val can be a dictionary or an hash
@ -36,10 +49,6 @@ def diffsnap(last_tree, current_tree, path='./', bres={}):
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
# if name_last == 'pino.txt':
# import pdb
# pdb.set_trace()
last_type = 'dir' if isinstance(hsh_last, dict) else 'file'
item = {
@ -86,22 +95,6 @@ def diffsnap(last_tree, current_tree, path='./', bres={}):
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')
# item.update({
# 'name' : key_added,
# 'path' : '%s%s' % (path, name_last),
# 'type' : 'dir' if isinstance(current_tree[name_last][key_added], dict) else 'file',
# 'hash' : current_tree[name_last][key_added],
# })
# added.append(item)
#####
###########
diffsnap(hsh_last, hsh_current, path='%s%s/' % (path,name_last), bres = {
'removed' : removed,
@ -109,7 +102,7 @@ def diffsnap(last_tree, current_tree, path='./', bres={}):
'changed' : changed,
})
def findsubtree(snapshot, subtree, path='./'):
def find_subtree(snapshot, subtree, path='./'):
'''
@param subtree dict
find the exact subtree in snapshot

11
src/iface/sync_manager.py Normal file
View File

@ -0,0 +1,11 @@
class Manager():
def __init__(conf):
self.conf = conf
def mirror():
pass
def start_sync():
self.mirror()

View File

@ -15,6 +15,7 @@ def dump_snapshot(snapshot, path=None, dump_file_name=None):
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)

View File

@ -12,7 +12,7 @@ if __name__ == '__main__':
from tools import pretty_print as _pretty_print
local_tree_hash = _genlocal.generate_tree_hash('/home/luca/sharednotes_dev/')
diff = {}
_snap.diffsnap(last_local_tree_hash, local_tree_hash, bres=diff)
_snap.diff_snap(last_local_tree_hash, local_tree_hash, bres=diff)
processed = {}

Binary file not shown.

72
src/tools/parse_conf.py Normal file
View File

@ -0,0 +1,72 @@
import os
def parse_conf_line(conf_line):
conf_line = conf_line.rstrip('\n')
conf_line_split = conf_line.split(' ')
k, val = conf_line_split[0], conf_line_split[-1]
return k, val
def read_conf_section(filepath: str, section: str):
conf_lines = []
section_line = '[%s]' % (section,)
with open(filepath, 'r') as fconf:
conf_lines = fconf.readlines()
conf_lines_section = []
append_to_section_conf = False
for conf_line in conf_lines:
if conf_line == '\n' or conf_line == ' ':
continue
if conf_line.startswith('[') and append_to_section_conf:
## section is changed
break
if section_line in conf_line:
append_to_section_conf = True
continue
if not append_to_section_conf:
continue
conf_lines_section.append(conf_line)
conf = {}
for cline in conf_lines_section:
k,v = parse_conf_line(cline)
conf[k] = v
return conf
def read_conf(filepath: str, section: str=None):
'''
@filepath str path of config file
@section str section name in a config file [snapshot]
'''
if not os.path.isfile(filepath):
print('Path %s is not a file or not exists' % (filepath,))
return {}
if section:
return read_conf_section(filepath, section)
conf = {}
with open(filepath, 'r') as fconf:
reading = True
while reading:
conf_line = fconf.readline()
if not conf_line:
break
if conf_line == '\n' or ('[' in conf_line):
continue
k, val = parse_conf_line(conf_line)
conf[k] = val
return conf

View File

@ -1,6 +1,5 @@
import os
LEVEL_INDENT = 6
EMPTY_TREE_LINE_LENGTH = 160
EMPTY_TREE_LINE = ' ' * EMPTY_TREE_LINE_LENGTH