diff --git a/src/iface/sync_manager.py b/src/iface/sync_manager.py index e8cce5d..b1b196a 100644 --- a/src/iface/sync_manager.py +++ b/src/iface/sync_manager.py @@ -346,6 +346,8 @@ class Manager(): local_snap_diff = self.get_local_snap_diff() changes = local_snap_diff.get('added') or [] + dones = [] + for node in added: node_name = node['name'] last_hash = node.get('last_hash') @@ -359,6 +361,10 @@ class Manager(): raise Exception('This case must be managed!!! remote node appeared but in the local tree there was not') self.copy_node_to_remote(node) + ## mark node as synched with remote, + dones.append(node) + + return dones def sync_changed(self, changes=None): @@ -368,6 +374,8 @@ class Manager(): unmerged = [] + dones = [] + for node in changes: node_name = node['name'] last_hash = node['last_hash'] @@ -383,12 +391,16 @@ class Manager(): unmerged.append(node) continue + dones.added(node) + print(f'You can proceed to push {node_name} file it is not changed from the last version') self.copy_node_to_remote(node) if unmerged: self.store_unmerged_diff(unmerged) + return dones + def sync(self): ''' sync: @@ -398,19 +410,54 @@ class Manager(): if there are not differencies simply do necessary actions to sync if there is differences put the file diff in a specific folder ''' - # self.init_rh_agent() - # last_tree = self.load_local_hash() - # current_tree = self.compute_local_hash() + ## check if there are unmerged files + + unmerged_path = self.get_unmerged_path() + if os.listdir(unmerged_path): + print('You have this unmerged file(s):\n') + self.show_conflicts() + return + local_snap_diff = self.get_local_snap_diff() - # rhagent = self.init_rh_agent() - # agent = self.get_agent() + local_tree = self.load_local_hash() + changes = local_snap_diff.get('changed') or [] - self.sync_changed(changes=changes) + changed_done = self.sync_changed(changes=changes) + + for node in changed_done: + local_tree = self.update_local_hash('changed', node, tree=local_tree) added = local_snap_diff.get('added') or [] - self.sync_added(added=added) + added_done = self.sync_added(added=added) + for node in added_done: + local_tree = self.update_local_hash('add', node, tree=local_tree) - return + return local_tree + def update_local_hash(self,action, node_changed, tree=None): + if not tree: + tree = self.load_local_hash() + + rel_path_list = node_changed['rel_path'].split('/') + rel_path_list = [r for r in rel_path_list if r] + node_name = node_changed['name'] + cur_hash = node_changed['cur_hash'] + subtree = tree + for kpath in rel_path_list: + if kpath == '.': continue + subtree = subtree[kpath] + + if action == 'add': + subtree[node_name] = {} + + try: + subtree[node_name].update(cur_hash) + except Exception as e: + print(f'Exception: {e}, {cur_hash}') + ## if key is an hash pass and modify the key + subtree[node_name] = cur_hash + + return tree + diff --git a/src/testing/sync.py b/src/testing/sync.py index 6823a26..d7de998 100644 --- a/src/testing/sync.py +++ b/src/testing/sync.py @@ -8,4 +8,10 @@ def test_init_sync(): m = sync_manager.Manager(local_path='/home/luca/sharednotes_dev', remote_path='notanamber@myvps:/home/notanamber/notes_dev') m.init_sync() - +def test_sync(): + ''' + notanamber@e65109ddf4:~/notes_dev$ rm -rf spartiti_sepu/added_local/ + notanamber@e65109ddf4:~/notes_dev$ rm -rf spartiti_sepu/added_remote/added_file.txt + ''' + m = get_test_manager() + m.sync()