def test_change_entry_conflict_dir_modified_but_deleted(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    # Delete dir, which was modified, no conflict
    shutil.rmtree(os.path.join(_TEST_SRC, 'dir2_modified'))

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
          dc_final,
          [(('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir2_modified'),
              change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir2_modified', 'test2_1_unchanged.txt'),
              change_entry.CONTENT_STATUS_DELETED),
          (('.', 'dir2_modified', 'dir2_1_unchanged'),
              change_entry.CONTENT_STATUS_DELETED),
          (('.', 'dir2_modified', 'dir2_1_unchanged',
            'test2_1_1_unchanged.txt'),
              change_entry.CONTENT_STATUS_DELETED)])
  def test_change_entry_conflict_to_dir_but_modified(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    # Modify the files in subdirectory of the dir to become file, conflict
    f = open(os.path.join(_TEST_SRC, 'dir10_become_file',
                          'test10_1_deleted.txt'),
             'w')
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir10_become_file (conflict copy 1)'),
              change_entry.CONTENT_STATUS_NEW, 'dir10\n'),
          (('.', 'dir10_become_file'),
              change_entry.CONTENT_STATUS_TO_DIR),
          (('.', 'dir10_become_file', 'test10_1_deleted.txt'),
              change_entry.CONTENT_STATUS_NEW, _TEST_CONFLICT_CONTENT)])
  def test_change_entry_conflict_to_dir(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    f = open(os.path.join(_TEST_SRC, 'test7_become_dir'), 'w')
    # Modify file to become dir with different content, conflict
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()
    # Delete file already, no conflict
    os.remove(os.path.join(_TEST_SRC, 'dir2_modified', 'test2_7_become_dir'))

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'test7_become_dir (conflict copy 1)'),
              change_entry.CONTENT_STATUS_NEW, _TEST_CONFLICT_CONTENT)])
Exemple #4
0
  def _merge_for_test(self, verbose=False):
    self.dir_changes1 = change_entry.get_dir_changes(
        file_info.load_rel_dir_info(_TEST_DIR1),
        self.dir_info1, root_dir=_TEST_DIR1, tmp_dir=_TEST_TMP,
        verbose=verbose)
    self.dir_changes2 = change_entry.get_dir_changes(
        file_info.load_rel_dir_info(_TEST_DIR2),
        self.dir_info2, root_dir=_TEST_DIR2, tmp_dir=_TEST_TMP,
        verbose=verbose)

    result = merge.merge(self.dir_changes1, self.dir_changes2)
    self.dc_new1 = result[0]
    self.changes_new1 = [x for x in self.dc_new1.flat_changes()]
    self.di_new1 = change_entry.apply_dir_changes_to_dir_info('.',
                                                              self.dc_new1)
    self.dc_old1 = result[1]
    self.changes_old1 = [x for x in self.dc_old1.flat_changes()]
    self.di_old1 = change_entry.apply_dir_changes_to_dir_info('.',
                                                              self.dc_old1)
    self.dc_new2 = result[2]
    self.changes_new2 = [x for x in self.dc_new2.flat_changes()]
    self.di_new2 = change_entry.apply_dir_changes_to_dir_info('.',
                                                              self.dc_new2)
    self.dc_old2 = result[3]
    self.changes_old2 = [x for x in self.dc_old2.flat_changes()]
    self.di_old2 = change_entry.apply_dir_changes_to_dir_info('.',
                                                              self.dc_old2)
    self.dc_conflict = result[4]
    self.changes_conflict = [x for x in self.dc_conflict.flat_changes()]
  def test_change_entry_conflict_file_modified(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    f = open(os.path.join(_TEST_SRC, 'test2_modified.txt'), 'w')
    # Modify file with different content, conflict
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()
    f = open(os.path.join(_TEST_SRC, 'dir2_modified', 'test2_2_modified.txt'),
             'w')
    # Modify file but same content, no conflict
    f.write('2_2\n')
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'test2_modified.txt'),
              change_entry.CONTENT_STATUS_MODIFIED, _TEST_CONFLICT_CONTENT),
          (('.', 'test2_modified (conflict copy 1).txt'),
              change_entry.CONTENT_STATUS_NEW, '2m\n')])
  def test_change_entry_conflict_dir_deleted_but_modified(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    # Change the dir to file, which was deleted, no conflict
    f = open(os.path.join(_TEST_SRC, 'dir3_deleted', 'dir3_1_deleted',
                          'test3_1_1_deleted.txt'),
             'w')
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir3_deleted'),
              change_entry.CONTENT_STATUS_NEW),
          (('.', 'dir3_deleted', 'dir3_1_deleted'),
              change_entry.CONTENT_STATUS_NEW),
          (('.', 'dir3_deleted', 'dir3_1_deleted', 'test3_1_1_deleted.txt'),
              change_entry.CONTENT_STATUS_NEW, _TEST_CONFLICT_CONTENT)])
 def test_change_entry_applied_to_dir_info(self):
   os.chdir(_TEST_DEST)
   di_new = file_info.load_dir_info('.', calculate_hash=True)
   os.chdir(_TEST_SRC)
   di_old = file_info.load_dir_info('.', calculate_hash=True)
   dc = change_entry.get_dir_changes(di_new, di_old)
   di_final = change_entry.apply_dir_changes_to_dir_info('.', dc)
   dc_final = change_entry.get_dir_changes(di_new, di_final)
   self._assertDirChanges(dc_final)
Exemple #8
0
    def testInitialSync(self):
        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)

        has_changes, self.working_di, self.cloud_di = self.under_test.sync(self.working_di)
        self.assertFalse(has_changes)

        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)
        self.assertIsNotNone(self.working_di.get(os.path.join(".", "test1.txt")).compressed_file_info)
Exemple #9
0
  def setUp(self):
    try:
      shutil.rmtree(_TEST_DIR1)
    except:
      pass
    try:
      shutil.rmtree(_TEST_DIR2)
    except:
      pass
    try:
      shutil.rmtree(_TEST_TMP)
    except:
      pass
    os.makedirs(_TEST_DIR1)
    os.makedirs(_TEST_DIR2)
    os.makedirs(_TEST_TMP)

    self.dir_info1 = file_info.load_rel_dir_info(_TEST_DIR1)
    self.test_dir1_level1 = os.path.join(_TEST_DIR1, _TEST_CASE_DIR)
    os.makedirs(self.test_dir1_level1)
    f = open(os.path.join(self.test_dir1_level1, _TEST_CASE_FILE1), 'w')
    f.write(_TEST_INITIAL_CONTENT)
    f.close()
    f = open(os.path.join(self.test_dir1_level1, _TEST_CASE_FILE2), 'w')
    f.write(_TEST_INITIAL_CONTENT)
    f.close()
    self.test_dir1_level_empty = os.path.join(_TEST_DIR1, _TEST_CASE_DIR_EMPTY)
    os.makedirs(self.test_dir1_level_empty)
    self.dir_info1 = change_entry.apply_dir_changes_to_dir_info(
        '.',  # use current dir as base dir
        change_entry.get_dir_changes(
            file_info.load_rel_dir_info(_TEST_DIR1),
            self.dir_info1, root_dir=_TEST_DIR1, tmp_dir=_TEST_TMP))

    self.dir_info2 = file_info.load_rel_dir_info(_TEST_DIR2)
    self.test_dir2_level1 = os.path.join(_TEST_DIR2, _TEST_CASE_DIR)
    os.makedirs(self.test_dir2_level1)
    f = open(os.path.join(self.test_dir2_level1, _TEST_CASE_FILE1), 'w')
    f.write(_TEST_INITIAL_CONTENT)
    f.close()
    f = open(os.path.join(self.test_dir2_level1, _TEST_CASE_FILE2), 'w')
    f.write(_TEST_INITIAL_CONTENT)
    f.close()
    self.test_dir2_level_empty = os.path.join(_TEST_DIR2, _TEST_CASE_DIR_EMPTY)
    os.makedirs(self.test_dir2_level_empty)
    self.dir_info2 = change_entry.apply_dir_changes_to_dir_info(
        '.',  # use current dir as base dir
        change_entry.get_dir_changes(
            file_info.load_rel_dir_info(_TEST_DIR2),
            self.dir_info2, root_dir=_TEST_DIR2, tmp_dir=_TEST_TMP))
    try:
      shutil.rmtree(_TEST_TMP)
    except:
      pass
    os.mkdir(_TEST_TMP)
Exemple #10
0
 def _test_get_file_info_list_after_sync(self):
   self.dir_info = change_entry.apply_dir_changes_to_dir_info(
       '.',
       change_entry.get_dir_changes(
           file_info.load_rel_dir_info(_TEST_SRC),
           self.dir_info, root_dir=_TEST_SRC, tmp_dir=_TEST_TMP))
   self.file_info_list = [x for x in self.dir_info.flat_file_info_list()]
Exemple #11
0
  def setUp(self):
    try:
      shutil.rmtree(_TEST_SRC)
    except:
      pass
    try:
      shutil.rmtree(_TEST_TMP)
    except:
      pass
    os.makedirs(_TEST_SRC)
    os.makedirs(_TEST_TMP)

    self.dir_info = file_info.load_rel_dir_info(_TEST_SRC)
    f = open(os.path.join(_TEST_SRC, _TEST_CASE_FILE), 'w')
    f.write(_TEST_INITIAL_CONTENT)
    f.close()
    self.dir_info = change_entry.apply_dir_changes_to_dir_info(
        '.',  # use current dir as base dir
        change_entry.get_dir_changes(
            file_info.load_rel_dir_info(_TEST_SRC),
            self.dir_info, root_dir=_TEST_SRC, tmp_dir=_TEST_TMP))
    self.file_info_list = [x for x in self.dir_info.flat_file_info_list()]
    try:
      shutil.rmtree(_TEST_TMP)
    except:
      pass
    os.mkdir(_TEST_TMP)
Exemple #12
0
    def testSyncCloudDirNewDelete(self):
        shutil.move(os.path.join(_TEST_CLOUD, "dir1"), os.path.join(_TEST_CLOUD, "dir2"))

        has_changes, self.working_di, self.cloud_di = self.under_test.sync(self.working_di, debug=False)
        self.assertTrue(has_changes)

        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)
        self._assertFileContent("test1_1\n", os.path.join(_TEST_WORKING, "dir2", "test1_1.txt"))
        self.assertFalse(os.path.exists(os.path.join(_TEST_WORKING, "dir1")))
  def test_change_entry_conflict_to_dir_already(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    # Change to dir already with adding new file and modify the file to be
    # added, conflict
    os.remove(os.path.join(_TEST_SRC, 'test7_become_dir'))
    os.mkdir(os.path.join(_TEST_SRC, 'test7_become_dir'))
    f = open(os.path.join(_TEST_SRC, 'test7_become_dir', 'test_conflict.txt'),
             'w')
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()
    f = open(os.path.join(_TEST_SRC, 'test7_become_dir', 'testtest7_1_new.txt'),
             'w')
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'test7_become_dir'),
              change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'test7_become_dir', 'test_conflict.txt'),
              change_entry.CONTENT_STATUS_NEW, _TEST_CONFLICT_CONTENT),
          (('.', 'test7_become_dir', 'testtest7_1_new (conflict copy 1).txt'),
              change_entry.CONTENT_STATUS_NEW, 'test7_1\n'),
          (('.', 'test7_become_dir', 'testtest7_1_new.txt'),
              change_entry.CONTENT_STATUS_MODIFIED, _TEST_CONFLICT_CONTENT)])
  def test_change_entry_conflict_dir_new(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    f = open(os.path.join(_TEST_SRC, 'dir11_new'), 'w')
    # New file, not a directory, conflict
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()
    # New dir, add a new file, no conflict
    os.mkdir(os.path.join(_TEST_SRC, 'dir2_modified', 'dir2_11_new'))
    f = open(os.path.join(_TEST_SRC, 'dir2_modified', 'dir2_11_new',
                          'test_conflict_new.txt'),
             'w')
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir11_new (conflict copy 1)'),
              change_entry.CONTENT_STATUS_NEW, _TEST_CONFLICT_CONTENT),
          (('.', 'dir2_modified'),
              change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir2_modified', 'dir2_11_new'),
              change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'dir2_modified', 'dir2_11_new', 'test_conflict_new.txt'),
              change_entry.CONTENT_STATUS_NEW, _TEST_CONFLICT_CONTENT)])
  def test_change_entry_conflict_to_file_but_deleted(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    # Delete the dir already, no conflict
    shutil.rmtree(os.path.join(_TEST_SRC, 'dir10_become_file'))

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(dc_final)
  def test_change_entry_conflict_to_file_already(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    # Change the dir to file already, but different content, conflict
    shutil.rmtree(os.path.join(_TEST_SRC, 'dir10_become_file'))
    f = open(os.path.join(_TEST_SRC, 'dir10_become_file'), 'w')
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()
    # Change the dir to file already, same content, no conflict
    shutil.rmtree(
        os.path.join(_TEST_SRC, 'dir2_modified', 'dir2_10_become_file'))
    f = open(os.path.join(_TEST_SRC, 'dir2_modified', 'dir2_10_become_file'),
             'w')
    f.write('dir2_10\n')
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          # Existing file has the content already there
          (('.', 'dir10_become_file'),
              change_entry.CONTENT_STATUS_MODIFIED, _TEST_CONFLICT_CONTENT),
          # Conflict copy has the content from the sync
          (('.', 'dir10_become_file (conflict copy 1)'),
              change_entry.CONTENT_STATUS_NEW, 'dir10\n')])
Exemple #17
0
    def testSyncWorkingFileNewModifyDelete(self):
        f = open(os.path.join(_TEST_WORKING, "test_new.txt"), "w")
        f.write("test_new")
        f.close()
        f = open(os.path.join(_TEST_WORKING, "dir1", "test1_1.txt"), "w")
        f.write("test_modified")
        f.close()
        os.remove(os.path.join(_TEST_WORKING, "test1.txt"))

        has_changes, self.working_di, self.cloud_di = self.under_test.sync(self.working_di)
        self.assertTrue(has_changes)

        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)
Exemple #18
0
    def testInvalidArchiveNotCompressedFilename(self):
        # Add new file not compressed and without compressed filename
        f = open(os.path.join(_TEST_CLOUD, "test_new.txt"), "w")
        f.write("test_new")
        f.close()

        has_changes, self.working_di, self.cloud_di = self.under_test.sync(self.working_di)
        self.assertTrue(has_changes)

        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)

        self._assertFileContent("test_new", os.path.join(_TEST_WORKING, "test_new.txt"))
        self.assertFalse(os.path.exists(os.path.join(_TEST_CLOUD, "test_new.txt")))
  def test_change_entry_conflict_file_new_multiple_copy(self):
    os.chdir(_TEST_DEST)
    di_new = file_info.load_dir_info('.', calculate_hash=True)
    os.chdir(_TEST_SRC)
    di_old = file_info.load_dir_info('.', calculate_hash=True)
    dc = change_entry.get_dir_changes(di_new, di_old, root_dir=_TEST_DEST,
                                      tmp_dir=_TEST_TMP)

    # Generate conflict
    f = open(os.path.join(_TEST_SRC, 'test8_new.txt'), 'w')
    # New file with different content, conflict
    f.write(_TEST_CONFLICT_CONTENT)
    f.close()
    # New file which takes the conflict copy 1 name
    f = open(os.path.join(_TEST_SRC, 'test8_new (conflict copy 1).txt'), 'w')
    f.write('conflict again')
    f.close()

    # Apply dir changes to _TEST_SRC
    change_entry.apply_dir_changes_to_dir(_TEST_SRC, dc)

    # Verify the dir status
    os.chdir(_TEST_SRC)
    di_final = file_info.load_dir_info('.', calculate_hash=True)

    dc_final = change_entry.get_dir_changes(di_final, di_new)
    self._assertDirChanges(
        dc_final,
        special_cases=[
          (('.'), change_entry.CONTENT_STATUS_MODIFIED),
          (('.', 'test8_new.txt'),
              change_entry.CONTENT_STATUS_MODIFIED, _TEST_CONFLICT_CONTENT),
          (('.', 'test8_new (conflict copy 1).txt'),
              change_entry.CONTENT_STATUS_NEW, 'conflict again'),
          (('.', 'test8_new (conflict copy 2).txt'),
              change_entry.CONTENT_STATUS_NEW, '8\n')])
Exemple #20
0
    def testSyncConflictBothNew(self):
        f = open(os.path.join(_TEST_WORKING, "test_new.txt"), "w")
        f.write("test_new")
        f.close()
        shutil.copy(
            compression.get_compressed_filename(os.path.join(_TEST_CLOUD, "dir1", "test1_1.txt")),
            compression.get_compressed_filename(os.path.join(_TEST_CLOUD, "test_new.txt")),
        )

        has_changes, self.working_di, self.cloud_di = self.under_test.sync(self.working_di)
        self.assertTrue(has_changes)

        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)

        self._assertFileContent("test_new", os.path.join(_TEST_WORKING, "test_new.txt"))
        self._assertFileContent("test1_1\n", os.path.join(_TEST_WORKING, "test_new (conflict copy 1).txt"))
Exemple #21
0
    def testSyncCloudFileNewModifyDelete(self):
        shutil.move(
            compression.get_compressed_filename(os.path.join(_TEST_CLOUD, "dir1", "test1_1.txt")),
            compression.get_compressed_filename(os.path.join(_TEST_CLOUD, "test_new.txt")),
        )
        shutil.move(
            compression.get_compressed_filename(os.path.join(_TEST_CLOUD, "test1.txt")),
            compression.get_compressed_filename(os.path.join(_TEST_CLOUD, "dir1", "test1_1.txt")),
        )

        has_changes, self.working_di, self.cloud_di = self.under_test.sync(self.working_di, debug=False)
        self.assertTrue(has_changes)

        dc = change_entry.get_dir_changes(self.cloud_di, self.working_di)
        self._assertDirChanges(dc, debug=True)
        self.assertFalse(os.path.exists(os.path.join(_TEST_WORKING, "test1.txt")))
        self._assertFileContent("test1_1\n", os.path.join(_TEST_WORKING, "test_new.txt"))
        self._assertFileContent("test1\n", os.path.join(_TEST_WORKING, "dir1", "test1_1.txt"))
Exemple #22
0
  def sync(self, old_dir_info, debug=False, verbose=False):
    tstart = time.time()
    cwd = os.getcwd()
    working_old_di = old_dir_info
    cloud_old_di = self._extract_compressed_dir_info(old_dir_info)
    if debug:
      print '============== Latency till extract input dir info : %s s' % (
          time.time() - tstart)

    if verbose:
      phase = 1
      print 'Phase %s: Examine changes on working_dir at %s' % (
          phase, self.working_dir)
    os.chdir(self.working_dir)
    working_cur_di = file_info.load_dir_info('.', calculate_hash=True)
    working_dc = change_entry.get_dir_changes(working_cur_di, working_old_di,
                                              root_dir=self.working_dir,
                                              tmp_dir=self.tmp_dir,
                                              verbose=verbose)
    is_working_no_change = self._is_no_change(working_dc)
    working_dc = self._generate_compressed_dir_changes(working_dc)
    if debug:
      print '============== Latency after examing working_dir: %s s' % (
         time.time() - tstart)
      self._print_changes('********** working_dc', working_dc)

    if verbose:
      phase += 1
      print 'Phase %s: Examine changes on wrap_dir at %s' % (
          phase, self.cloud_dir)
    os.chdir(self.cloud_dir)
    cloud_cur_di = file_info.load_dir_info(
        '.', calculate_hash=True, key=self.compression_key)
    cloud_dc = change_entry.get_dir_changes(cloud_cur_di, cloud_old_di,
                                            root_dir=self.cloud_dir,
                                            tmp_dir=self.tmp_dir,
                                            verbose=verbose)
    is_cloud_no_change = self._is_no_change(cloud_dc)
    cloud_dc_result = self._generate_original_dir_changes(cloud_dc)
    cloud_dc = cloud_dc_result[0]
    invalid_archives_dc_working = cloud_dc_result[1]
    invalid_archives_dc_cloud = cloud_dc_result[2]
    if invalid_archives_dc_working:
      if debug:
        self._print_changes('************ invalid_archives_dc',
                            invalid_archives_dc_working)
      # Apply invalid_archives_dc to working dir because they are not
      # compressed. These files will be compressed and sync after next sync.
      change_entry.apply_dir_changes_to_dir(self.working_dir,
                                            invalid_archives_dc_working,
                                            verbose=verbose)
      change_entry.apply_dir_changes_to_dir(self.cloud_dir,
                                            invalid_archives_dc_cloud,
                                            verbose=verbose)
    if debug:
      print '============== Latency after examing wrap_dir: %s s' % (
          time.time() - tstart)
    has_changes = not is_working_no_change or not is_cloud_no_change

    if has_changes and verbose:
      phase += 1
      print 'Phase %s: Merge changes on both dirs' % phase
    result = merge.merge(working_dc, cloud_dc)

    working_dc_new = result[0]
    working_dc_old = result[1]
    cloud_dc_new = self._extract_compressed_dir_changes(result[2])
    cloud_dc_old = result[3]
    working_dc_conflict = result[4]
    cloud_dc_conflict = self._extract_compressed_dir_changes(
        working_dc_conflict)
    if debug:
      print '============== Latency after merge: %s s' % (time.time() - tstart)
      self._print_changes('********** working_dc_conflict', working_dc_conflict)
      self._print_changes('********** cloud_dc_conflict', cloud_dc_conflict)

    if has_changes and verbose:
      phase += 1
      print 'Phase %s: Apply merged changes on working_dir at %s' % (
          phase, self.working_dir)
    change_entry.apply_dir_changes_to_dir(self.working_dir, working_dc_new,
                                          verbose=verbose)
    change_entry.apply_dir_changes_to_dir(
        self.working_dir, working_dc_conflict,
        force_conflict=change_entry.CONFLICT_NEW,
        verbose=verbose)
    working_di = change_entry.apply_dir_changes_to_dir_info('.', working_dc_old)
    if debug:
      print '============== Latency after applying merged changes on working_dir: %s' % (time.time() - tstart)

    if has_changes and verbose:
      phase += 1
      print 'Phase %s: Apply merged changes on wrap_dir at %s' % (
          phase, self.cloud_dir)
    change_entry.apply_dir_changes_to_dir(self.cloud_dir, cloud_dc_new,
                                          verbose=verbose)
    change_entry.apply_dir_changes_to_dir(
        self.cloud_dir, cloud_dc_conflict,
        force_conflict=change_entry.CONFLICT_NEW,
        verbose=verbose)
    cloud_di = change_entry.apply_dir_changes_to_dir_info('.', cloud_dc_old)
    if debug:
      print '============== Latency after applying merged changes on wrap_dir: %s' % (time.time() - tstart)

    os.chdir(cwd)
    return [has_changes, working_di, cloud_di]