def test_copmression(self): compression.compress_recursively( _CASE_PATH, _CASE_SRC, _CASE_DEST, password=None, encryption_method=compression.ENCRYPTION_AES_256 ) src = os.path.join(_CASE_SRC, _CASE_PATH) dest = os.path.join(_CASE_DEST, _CASE_PATH) for path, dirs, files in os.walk(src): rel_path = os.path.relpath(path, src) dest_path = os.path.join(dest, rel_path) for f in files: self.assertTrue( "Compressed file %s is not found for %s" % (os.path.join(dest_path, compression.get_compressed_filename(f)), os.path.join(path, f)), os.path.exists(os.path.join(dest_path, compression.get_compressed_filename(f))), )
def _generate_compressed_dir_changes(self, dir_changes): dir_changes = copy.deepcopy(dir_changes) for c in dir_changes.flat_changes(): if c.cur_info and c.cur_info.tmp_file: compressed_tmp_filename = change_entry.generate_tmp_file(self.tmp_dir) compressed_tmp_file = os.path.join(self.tmp_dir, compressed_tmp_filename) tmp_file_with_realname = os.path.join(self.tmp_dir, os.path.basename(c.path)) # Rename the tmp file so that the archive includes the original # filename. os.rename(c.cur_info.tmp_file, tmp_file_with_realname) compression.compress_file(tmp_file_with_realname, compressed_tmp_file, password=self.password, encryption_method=self.encryption_method, compression_level=self.compression_level) # Rename the tmp filename back. os.rename(tmp_file_with_realname, c.cur_info.tmp_file) tmp_fi = file_info.load_file_info(compressed_tmp_file) compressed_file_info = file_info.FileInfo( # TODO: check conflict of compressed filename? compression.get_compressed_filename(c.cur_info.path), c.cur_info.is_dir, c.cur_info.mode, tmp_fi.size, c.cur_info.last_modified_time) c.cur_info.compressed_file_info = file_info.copy_with_tmp_file( compressed_file_info, compressed_tmp_filename, self.tmp_dir) return dir_changes
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"))
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"))
def testInvalidArchive(self): # Add new file with compressed filename but not a valid compressed file compressed_filename = compression.get_compressed_filename("test_new.txt") f = open(os.path.join(_TEST_CLOUD, compressed_filename), "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, compressed_filename)) self.assertFalse(os.path.exists(os.path.join(_TEST_CLOUD, compressed_filename)))