def _merge_files_latest(files1: List[dict], files2: List[dict], f1_path: NormalizedPath, f2_path: NormalizedPath,
                        rel_path: NormalizedPath):
    """Create actions based on the files inside the two folders."""
    f1_actions = []
    f2_actions = []
    f2_file_names = [file["file_name"] for file in files2]
    for f1 in files1:
        try:
            idx = f2_file_names.index(f1["file_name"])
            f2 = files2[idx]
            if f1["modified_timestamp"] > f2["modified_timestamp"]:
                take_1 = True
            else:
                take_1 = False
            f2_file_names.pop(idx)
            files2.pop(idx)
        except ValueError:
            take_1 = True
        if take_1:
            f2_actions.append(c_syc.create_action(normalize_path(f2_path), normalize_path(rel_path, f1["file_name"]),
                                                  gen_json.ACTION_PULL, False,
                                                  remote_abs_path=normalize_path(f1_path, rel_path, f1["file_name"])))
        else:
            f1_actions.append(c_syc.create_action(normalize_path(f1_path), normalize_path(rel_path, f1["file_name"]),
                                                  gen_json.ACTION_PULL, False,
                                                  remote_abs_path=normalize_path(f2_path, rel_path, f1["file_name"])))
    files1.clear()
    if len(files2) > 0:
        new_f2_actions, new_f1_actions = _merge_files_latest(files2, files1, f2_path, f1_path, rel_path)
        f1_actions += new_f1_actions
        f2_actions += new_f2_actions
    return f1_actions, f2_actions
Esempio n. 2
0
 def test_pull_dir_client_to_server(self):
     """Copy dir from client to server"""
     h_register_dummy_user_device_client()
     client_folder = gen_paths.normalize_path(
         client_paths.LOCAL_CLIENT_DATA, "folder1")
     h_fill_dummy_dir(client_folder)
     actions = [
         c_sync.create_action(gen_paths.normalize_path("folder1"),
                              gen_paths.normalize_path(""),
                              gen_json.ACTION_PULL,
                              remote_abs_path=client_folder,
                              is_directory=True)
     ]
     net_interface.server.execute_actions(actions)
     self.assertTrue(
         os.path.exists((path_utils.rel_user_path_to_abs("folder1", 1))))
     self.assertTrue(
         os.path.exists(
             (path_utils.rel_user_path_to_abs("folder1/inner1", 1))))
     self.assertTrue(
         os.path.exists(
             (path_utils.rel_user_path_to_abs("folder1/inner2", 1))))
     self.assertTrue(
         os.path.exists((path_utils.rel_user_path_to_abs(
             "folder1/inner1/test_inner.txt", 1))))
     self.assertTrue(
         os.path.exists(
             (path_utils.rel_user_path_to_abs("folder1/test.txt", 1))))
def _merge_folders_latest(folders1: List[dict], folders2: List[dict], f1_path: NormalizedPath, f2_path: NormalizedPath,
                          rel_path: NormalizedPath):
    """Create actions based on the folders inside the two folders."""
    f1_actions = []
    f2_actions = []
    f2_folder_names = [folder["folder_name"] for folder in folders2]
    for f1 in folders1:
        try:
            idx = f2_folder_names.index(f1["folder_name"])
            f2 = folders2[idx]
            new_rel_path = normalize_path(rel_path, f1["folder_name"])
            new_f1_actions, new_f2_actions = _prioritize_latest_recursive(f1["files"], f2["files"],
                                                                          f1_path, f2_path, new_rel_path)
            f1_actions += new_f1_actions
            f2_actions += new_f2_actions
            f2_folder_names.pop(idx)
            folders2.pop(idx)
        except ValueError:
            rel_file_path = normalize_path(rel_path, f1["folder_name"])
            remote_abs_path = normalize_path(f1_path, rel_path, f1["folder_name"])
            f2_actions.append(c_syc.create_action(normalize_path(f2_path), rel_file_path, gen_json.ACTION_PULL,
                                                  True, remote_abs_path=remote_abs_path))
    if len(folders2) > 0:
        new_f2_actions, new_f1_actions = _merge_folders_latest(folders2, folders1, f2_path, f1_path, rel_path)
        f1_actions += new_f1_actions
        f2_actions += new_f2_actions
    return f1_actions, f2_actions
    def test_merge_changes_move_server(self):
        server_changes = {
            **h_create_folder_entry(
                gen_paths.normalize_path("folder1"), {
                    **h_create_change(gen_paths.NormalizedPath("test.txt"),
                                      gen_json.ACTION_MOVE,
                                      new_file_path=gen_paths.normalize_path("new_test.txt"))
                })
        }

        client_changes = h_create_folder_entry(
            gen_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA,
                                     "folder1"), {},
            gen_paths.normalize_path("folder1"))

        expected_server = []
        expected_conflicts = []
        expected_client = [
            c_sync.create_action(
                gen_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA,
                                         "folder1"),
                gen_paths.normalize_path("new_test.txt"),
                gen_json.ACTION_MOVE,
                rel_old_file_path=gen_paths.normalize_path("test.txt"))
        ]
        self.h_check_merge(server_changes, client_changes, expected_server,
                           expected_client, expected_conflicts)
    def test_merge_changes_create_server(self):
        server_changes = {
            **h_create_folder_entry(
                gen_paths.normalize_path("folder1"), {
                    **h_create_change(gen_paths.normalize_path("test.txt"), gen_json.ACTION_PULL)
                })
        }

        client_changes = h_create_folder_entry(
            gen_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA,
                                     "folder1"), {},
            gen_paths.normalize_path("folder1"))

        src_path = gen_paths.normalize_path("folder1", "test.txt")
        expected_server = []
        expected_conflicts = []
        expected_client = [
            c_sync.create_action(gen_paths.normalize_path(
                client_paths.LOCAL_CLIENT_DATA, "folder1"),
                                 gen_paths.normalize_path("test.txt"),
                                 gen_json.ACTION_PULL,
                                 remote_abs_path=src_path)
        ]
        self.h_check_merge(server_changes, client_changes, expected_server,
                           expected_client, expected_conflicts)
Esempio n. 6
0
def take_1(
        folder_1_content: dict,
        folder_2_content: dict) -> Tuple[List[SyncAction], List[SyncAction]]:
    f1_actions = []
    f2_actions = []
    f1_folder_name = folder_1_content["folder_name"]
    f2_folder_name = folder_2_content["folder_name"]

    f2_actions.append(
        c_syc.create_action(f2_folder_name, NormalizedPath(""),
                            gen_json.ACTION_DELETE, True))
    f2_actions.append(
        c_syc.create_action(f2_folder_name,
                            NormalizedPath(""),
                            gen_json.ACTION_PULL,
                            True,
                            remote_abs_path=f1_folder_name))
    return f1_actions, f2_actions
    def test_execute_server_actions_delete(self):
        self.server_folder = h_setup_execution_env()

        src_file_path = os.path.join(self.server_folder, "test.txt")
        with open(src_file_path, "w") as f:
            f.write("Hello")

        server_actions = [
            c_sync.create_action(gen_paths.normalize_path("folder1"),
                                 gen_paths.normalize_path("test.txt"),
                                 gen_json.ACTION_DELETE)
        ]
        c_sync._execute_server_actions(server_actions)
        self.assertFalse(os.path.isfile(src_file_path))
    def test_execute_client_actions_delete(self):
        h_create_empty(os.path.join(client_paths.LOCAL_CLIENT_DATA, "folder1"))
        client_src_path = os.path.join(client_paths.LOCAL_CLIENT_DATA,
                                       "folder1/test.txt")
        with open(client_src_path, "w") as f:
            f.write("Lorem ipsum " * 10)

        client_actions = [
            c_sync.create_action(
                gen_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA,
                                         "folder1"),
                gen_paths.normalize_path("test.txt"), gen_json.ACTION_DELETE)
        ]
        c_sync.execute_client_actions(client_actions)
        self.assertFalse(os.path.isfile(client_src_path))
    def test_execute_server_actions_pull(self):
        self.server_folder = h_setup_execution_env()
        client_src_path = os.path.join(client_paths.LOCAL_CLIENT_DATA,
                                       "folder1/test.txt")
        with open(client_src_path, "w") as f:
            f.write("Lorem ipsum " * 10)

        server_actions = [
            c_sync.create_action(gen_paths.normalize_path("folder1"),
                                 gen_paths.normalize_path("test.txt"),
                                 gen_json.ACTION_PULL,
                                 remote_abs_path=client_src_path)
        ]
        c_sync._execute_server_actions(server_actions)
        server_dest_path = os.path.join(self.server_folder, "test.txt")
        self.assertTrue(os.path.isfile(server_dest_path))
    def test_execute_server_actions_move(self):
        self.server_folder = h_setup_execution_env()

        src_file_path = os.path.join(self.server_folder, "test.txt")
        with open(src_file_path, "w") as f:
            f.write("Hello")

        server_actions = [
            c_sync.create_action(
                gen_paths.normalize_path("folder1"),
                gen_paths.normalize_path("new_test.txt"),
                gen_json.ACTION_MOVE,
                rel_old_file_path=gen_paths.normalize_path("test.txt"))
        ]
        c_sync._execute_server_actions(server_actions)
        expected_path = os.path.join(self.server_folder, "new_test.txt")
        self.assertTrue(os.path.isfile(expected_path))
    def test_execute_client_actions_pull(self):
        self.server_folder = h_setup_execution_env()
        server_file_path = os.path.join(self.server_folder, 'test.txt')
        with open(server_file_path, "w") as f:
            f.write("Hello" * 10)

        client_actions = [
            c_sync.create_action(
                gen_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA,
                                         "folder1"),
                gen_paths.normalize_path("test.txt"),
                gen_json.ACTION_PULL,
                remote_abs_path=gen_paths.NormalizedPath("folder1/test.txt"))
        ]
        c_sync.execute_client_actions(client_actions)
        client_dest_path = os.path.join(client_paths.LOCAL_CLIENT_DATA,
                                        "folder1/test.txt")
        self.assertTrue(os.path.isfile(client_dest_path))
    def test_merge_changes_delete_client(self):
        client_changes = {
            **h_create_folder_entry(
                gen_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA, "folder1"), {
                    **h_create_change(gen_paths.NormalizedPath("test.txt"), gen_json.ACTION_DELETE)
                }, gen_paths.normalize_path("folder1"))
        }

        server_changes = h_create_folder_entry(
            gen_paths.normalize_path("folder1"), {})

        expected_server = [
            c_sync.create_action(gen_paths.normalize_path("folder1"),
                                 gen_paths.normalize_path("test.txt"),
                                 gen_json.ACTION_DELETE)
        ]
        expected_client = []
        expected_conflicts = []
        self.h_check_merge(server_changes, client_changes, expected_server,
                           expected_client, expected_conflicts)
Esempio n. 13
0
 def test_distribute_action(self):
     server_json.create_changes_file_for_new_device(1, 1)
     server_json.add_folder("folder1")
     src_path = "Dummy_client_path"
     action = c_sync.create_action(gen_paths.normalize_path("folder1"),
                                   gen_paths.normalize_path("test.txt"),
                                   gen_json.ACTION_PULL,
                                   remote_abs_path=src_path)
     server_json.distribute_action(action, [1])
     data = server_json._get_json_data()
     changes: dict = data["folder1"]["changes"]
     changes_data = changes["test.txt"]
     self.assertEqual(
         {
             "test.txt": {
                 "action": "pull",
                 "timestamp": changes_data["timestamp"],
                 "is_directory": False,
                 "rel_old_file_path": None
             }
         }, changes)