def testCopyFoldersConflict(self): """ Test copytree duplicate error management when two folders from two different drives have the same name. :return: """ folders = [self.fin_path, self.bank_path, self.other_fin_path] for folder in folders: file_path = os.path.join(folder, "testfile.doc") with open(file_path, "w") as fh: fh.write("test") make = MakeArchive(None) final_dest_p = Path(self.base_path, "final_dest") final_dest_p.mkdir() final_dest_path = str(final_dest_p) make.create_with_dirs( [self.fin_path, self.bank_path, self.other_fin_path], self.base_path, final_dest_path) dir_list = os.listdir(final_dest_path) dir_found_dict = {"Fin": False, "Bank": False, "unittest_Fin": False} for file in dir_list: ext = os.path.splitext(file)[1] if ext == ".7z": unzip_path = os.path.join(self.base_path, "unzip") zip_file_path = os.path.join(final_dest_path, file) s_zip = SevenZipFile(zip_file_path) ret = s_zip.extract_all(zip_file_path, unzip_path) self.assertTrue(ret) another_list = os.listdir(unzip_path) self.assertEqual(1 + len(folders), len(another_list)) for root, dirs, files in os.walk(unzip_path): dirname = os.path.split(root)[1] if dirname == "Fin" or dirname == "Bank" or dirname == "unittest_Fin": dir_found_dict[dirname] = True self.assertEqual(1, len(files)) for doc_file in files: self.assertEqual("testfile.doc", doc_file) else: self.assertFalse(True) self.assertTrue(dir_found_dict["Fin"]) self.assertTrue(dir_found_dict["Bank"]) self.assertTrue(dir_found_dict["unittest_Fin"])
def testCopyFoldersConflict(self): """ Test copytree duplicate error management when two folders from two different drives have the same name. :return: """ folders = [self.fin_path, self.bank_path, self.other_fin_path] for folder in folders: file_path = os.path.join(folder, "testfile.doc") with open(file_path, "w") as fh: fh.write("test") make = MakeArchive(None) final_dest_p = Path(self.base_path, "final_dest") final_dest_p.mkdir() final_dest_path = str(final_dest_p) make.create_with_dirs([self.fin_path, self.bank_path, self.other_fin_path], self.base_path, final_dest_path) dir_list = os.listdir(final_dest_path) dir_found_dict = {"Fin" : False, "Bank" : False, "unittest_Fin": False} for file in dir_list: ext = os.path.splitext(file)[1] if ext == ".7z": unzip_path = os.path.join(self.base_path, "unzip") zip_file_path = os.path.join(final_dest_path, file) s_zip = SevenZipFile(zip_file_path) ret = s_zip.extract_all(zip_file_path, unzip_path) self.assertTrue(ret) another_list = os.listdir(unzip_path) self.assertEqual(1+len(folders), len(another_list)) for root, dirs, files in os.walk(unzip_path): dirname = os.path.split(root)[1] if dirname == "Fin" or dirname == "Bank" or dirname == "unittest_Fin": dir_found_dict[dirname] = True self.assertEqual(1, len(files)) for doc_file in files: self.assertEqual("testfile.doc", doc_file) else: self.assertFalse(True) self.assertTrue(dir_found_dict["Fin"]) self.assertTrue(dir_found_dict["Bank"]) self.assertTrue(dir_found_dict["unittest_Fin"])
def testFullMake(self): folders = [self.fin_path, self.bank_path] for folder in folders: file_path = os.path.join(folder, "testfile.doc") with open(file_path, "w") as fh: fh.write("test") make = MakeArchive(None) final_dest_p = Path(self.base_path, "final_dest") final_dest_p.mkdir() final_dest_path = str(final_dest_p) make.create_with_dirs([self.fin_path, self.bank_path], self.base_path, final_dest_path) dir_list = os.listdir(final_dest_path) dir_found_dict = {"Fin": False, "Bank": False} for file in dir_list: ext = os.path.splitext(file)[1] if ext == ".7z": unzip_path = os.path.join(self.base_path, "unzip") zip_file_path = os.path.join(final_dest_path, file) s_zip = SevenZipFile(zip_file_path) ret = s_zip.extract_all(zip_file_path, unzip_path) self.assertTrue(ret) another_list = os.listdir(unzip_path) self.assertEqual(1 + len(folders), len(another_list)) for root, dirs, files in os.walk(unzip_path): dirname = os.path.split(root)[1] if dirname == "Fin" or dirname == "Bank": dir_found_dict[dirname] = True self.assertEqual(1, len(files)) for doc_file in files: self.assertEqual("testfile.doc", doc_file) else: self.assertFalse(True) self.assertTrue(dir_found_dict["Fin"]) self.assertTrue(dir_found_dict["Bank"])
def testFullMake(self): folders = [self.fin_path, self.bank_path] for folder in folders: file_path = os.path.join(folder, "testfile.doc") with open(file_path, "w") as fh: fh.write("test") make = MakeArchive(None) final_dest_p = Path(self.base_path, "final_dest") final_dest_p.mkdir() final_dest_path = str(final_dest_p) make.create_with_dirs([self.fin_path, self.bank_path], self.base_path, final_dest_path) dir_list = os.listdir(final_dest_path) dir_found_dict = {"Fin" : False, "Bank" : False} for file in dir_list: ext = os.path.splitext(file)[1] if ext == ".7z": unzip_path = os.path.join(self.base_path, "unzip") zip_file_path = os.path.join(final_dest_path, file) s_zip = SevenZipFile(zip_file_path) ret = s_zip.extract_all(zip_file_path, unzip_path) self.assertTrue(ret) another_list = os.listdir(unzip_path) self.assertEqual(1+len(folders), len(another_list)) for root, dirs, files in os.walk(unzip_path): dirname = os.path.split(root)[1] if dirname == "Fin" or dirname == "Bank": dir_found_dict[dirname] = True self.assertEqual(1, len(files)) for doc_file in files: self.assertEqual("testfile.doc", doc_file) else: self.assertFalse(True) self.assertTrue(dir_found_dict["Fin"]) self.assertTrue(dir_found_dict["Bank"])
def testMakeSimpleArchiveZipDir(self): file_path = os.path.join(self.fin_path, "testfile.txt") with open(file_path, "w") as fh: lines = ["toto", "test", "852.90$", "Truc"] fh.writelines(lines) s_zip = SevenZipFile(self.base_path) zip_file_path = os.path.join(self.base_path, "test.7z") ret = s_zip.archive_dirs([self.fin_path], "test") self.assertTrue(ret) self.assertTrue(os.path.exists(zip_file_path)) unzip_path = os.path.join(self.base_path, "unzip") s_zip.extract_all(zip_file_path, unzip_path) another_list = os.listdir(unzip_path) self.assertEqual(2, len(another_list)) fin_dir_found = False for root, dirs, files in os.walk(unzip_path): dirname = os.path.split(root)[1] if dirname == "Fin": fin_dir_found = True self.assertEqual(1, len(files)) self.assertEqual(files[0], "testfile.txt") self.assertTrue(fin_dir_found)
def create_with_dirs(self, dir_list, work_folder_path, copy_zip_to_path): if not os.path.exists(work_folder_path): raise DirectoryNotFoundError("work folder doesn't exist, it needs to exist so we can copy files.") if not os.path.exists(copy_zip_to_path): raise DirectoryNotFoundError("Final folder path doesn't exist.") if not os.path.isdir(copy_zip_to_path): raise NotADirectoryError("copy to path is not a directory") dest_root_path = os.path.join(work_folder_path, "work_temp") # be careful with that line, a rmtree will be done on it. dest_dir_p = Path(dest_root_path) dest_dir_p.mkdir() dest_dir_list = [] for folder in dir_list: split_path = os.path.split(folder) dirname = split_path[1] to_copy_dir_dest_p = dest_dir_p.joinpath(dirname) if os.path.exists(str(to_copy_dir_dest_p)): parent_dirname = os.path.split(split_path[0])[1] to_copy_dir_dest_p = dest_dir_p.joinpath(parent_dirname + "_" + dirname) if os.path.exists(str(to_copy_dir_dest_p)): raise DirectoryConflictError("Cannot find a proper name for {folder}".format(folder=dirname)) try: shutil.copytree(folder, str(to_copy_dir_dest_p)) # can throw Error except shutil.Error: raise DirectoryConflictError("Error during copy for folder: {dirname}".format( dirname=dirname )) dest_dir_list.append(str(to_copy_dir_dest_p)) timestamp = datetime.datetime.now().strftime("%Y_%m_%d__%H_%M_%S") archive_name = "backup_{stamp}".format(stamp=timestamp) zip_file_path = os.path.join(dest_root_path, archive_name + ".7z") s_zip = SevenZipFile(dest_root_path) is_use_pwd = False if self._password is not None: is_use_pwd = True s_zip.set_pwd(self._password) s_zip.archive_dirs(dest_dir_list, archive_name, is_use_pwd) shutil.copy(zip_file_path, copy_zip_to_path) files = 0 folders = 0 try: files, folders = s_zip.archive_info(zip_file_path, is_use_pwd) except Exception: files = folders = -1 try: shutil.rmtree(dest_root_path) except PermissionError as ex: raise CleanUpError("Couldn't remove work folder.") from ex return files, folders
def create_with_dirs(self, dir_list, work_folder_path, copy_zip_to_path): if not os.path.exists(work_folder_path): raise DirectoryNotFoundError( "work folder doesn't exist, it needs to exist so we can copy files." ) if not os.path.exists(copy_zip_to_path): raise DirectoryNotFoundError("Final folder path doesn't exist.") if not os.path.isdir(copy_zip_to_path): raise NotADirectoryError("copy to path is not a directory") dest_root_path = os.path.join( work_folder_path, "work_temp" ) # be careful with that line, a rmtree will be done on it. dest_dir_p = Path(dest_root_path) dest_dir_p.mkdir() dest_dir_list = [] for folder in dir_list: split_path = os.path.split(folder) dirname = split_path[1] to_copy_dir_dest_p = dest_dir_p.joinpath(dirname) if os.path.exists(str(to_copy_dir_dest_p)): parent_dirname = os.path.split(split_path[0])[1] to_copy_dir_dest_p = dest_dir_p.joinpath(parent_dirname + "_" + dirname) if os.path.exists(str(to_copy_dir_dest_p)): raise DirectoryConflictError( "Cannot find a proper name for {folder}".format( folder=dirname)) try: shutil.copytree(folder, str(to_copy_dir_dest_p)) # can throw Error except shutil.Error: raise DirectoryConflictError( "Error during copy for folder: {dirname}".format( dirname=dirname)) dest_dir_list.append(str(to_copy_dir_dest_p)) timestamp = datetime.datetime.now().strftime("%Y_%m_%d__%H_%M_%S") archive_name = "backup_{stamp}".format(stamp=timestamp) zip_file_path = os.path.join(dest_root_path, archive_name + ".7z") s_zip = SevenZipFile(dest_root_path) is_use_pwd = False if self._password is not None: is_use_pwd = True s_zip.set_pwd(self._password) s_zip.archive_dirs(dest_dir_list, archive_name, is_use_pwd) shutil.copy(zip_file_path, copy_zip_to_path) files = 0 folders = 0 try: files, folders = s_zip.archive_info(zip_file_path, is_use_pwd) except Exception: files = folders = -1 try: shutil.rmtree(dest_root_path) except PermissionError as ex: raise CleanUpError("Couldn't remove work folder.") from ex return files, folders