Example #1
0
    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
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
    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