def test_uncompress_rar(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") typbytes = bytes fold = get_temp_folder(__file__, "temp_compress_rar") rz = os.path.join(fold, "..", "data", "rar5-blake.rar") res = unrar_files(rz, where_to=fold, fLOG=fLOG) self.assertTrue(isinstance(res, list)) res.sort() self.assertEqual(len(res), 2) if not isinstance(res[0], (typbytes, str)): raise TypeError(type(res[0])) res[0] = res[0].replace("\\", "/") if not res[0].endswith("ut_filehelper/temp_compress_rar/stest1.txt"): raise Exception(res[0])
def unzip_files(self, group): """ Unzips files and convert notebooks into :epkg:`HTML`. @param group group name @return list of new filess """ def fvalid(zip_name, local_name): if "__pycache__" in zip_name: return False if zip_name.endswith(".pyc"): return False return True names = list(self.enumerate_group_files(group)) files = [] for name in names: if "attachments" not in name: continue ext = os.path.splitext(name)[-1] if ext == ".zip": folder = os.path.splitext(name)[0] + "_zip" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG( "[ProjectsRepository.unzip_files] unzip '{0}'".format( name)) self.fLOG( "[ProjectsRepository.unzip_files] creating '{0}'". format(folder)) os.mkdir(folder) try: lf = unzip_files(name, folder, fLOG=self.fLOG, fvalid=fvalid, fail_if_error=False) except zipfile.BadZipFile as e: self.fLOG( "[ProjectsRepository.unzip_files] ERROR: unable to unzip '{0}' because of '{1}']" .format(name, e)) lf = [] files.extend(lf) else: # already done, we do not do it again pass elif ext == ".7z": folder = os.path.splitext(name)[0] + "_7z" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG("[ProjectsRepository.un7zip_files] un7zip '{0}'". format(name)) self.fLOG( "[ProjectsRepository.un7zip_files] creating '{0}'". format(folder)) os.mkdir(folder) lf = un7zip_files(name, folder, fLOG=self.fLOG, fvalid=fvalid) files.extend(lf) else: # already done, we do not do it again pass elif ext == ".rar": folder = os.path.splitext(name)[0] + "_rar" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG( "[ProjectsRepository.unrar_files] unrar '{0}'".format( name)) self.fLOG( "[ProjectsRepository.unrar_files] creating '{0}'". format(folder)) os.mkdir(folder) lf = unrar_files(name, folder, fLOG=self.fLOG, fvalid=fvalid) files.extend(lf) else: # already done, we do not do it again pass elif ext == ".gz": folder = os.path.splitext(name)[0] + "_gz" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG("[ProjectsRepository.ungzip_files] ungzip '{0}'". format(name)) self.fLOG( "[ProjectsRepository.ungzip_files] creating '{0}'". format(folder)) os.mkdir(folder) unzip = "pkl.gz" not in name lf = ungzip_files(name, folder, fLOG=self.fLOG, fvalid=fvalid, unzip=unzip) files.extend(lf) else: # already done, we do not do it again pass elif ext == ".tar.gz": raise Exception("unable to process such a file: " + name) return files
def unzip_files(self, group): """ Unzips files and convert notebooks into :epkg:`HTML`. @param group group name @return list of new filess """ def fvalid(zip_name, local_name): if "__pycache__" in zip_name: return False if zip_name.endswith(".pyc"): return False return True names = list(self.enumerate_group_files(group)) files = [] for name in names: if "attachments" not in name: continue ext = os.path.splitext(name)[-1] if ext == ".zip": folder = os.path.splitext(name)[0] + "_zip" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG( "[ProjectsRepository.unzip_files] unzip '{0}'".format(name)) self.fLOG( "[ProjectsRepository.unzip_files] creating '{0}'".format(folder)) os.mkdir(folder) try: lf = unzip_files( name, folder, fLOG=self.fLOG, fvalid=fvalid, fail_if_error=False) except zipfile.BadZipFile as e: self.fLOG( "[ProjectsRepository.unzip_files] ERROR: unable to unzip '{0}' because of '{1}']".format(name, e)) lf = [] files.extend(lf) else: # already done, we do not do it again pass elif ext == ".7z": folder = os.path.splitext(name)[0] + "_7z" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG( "[ProjectsRepository.un7zip_files] un7zip '{0}'".format(name)) self.fLOG( "[ProjectsRepository.un7zip_files] creating '{0}'".format(folder)) os.mkdir(folder) lf = un7zip_files( name, folder, fLOG=self.fLOG, fvalid=fvalid) files.extend(lf) else: # already done, we do not do it again pass elif ext == ".rar": folder = os.path.splitext(name)[0] + "_rar" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG( "[ProjectsRepository.unrar_files] unrar '{0}'".format(name)) self.fLOG( "[ProjectsRepository.unrar_files] creating '{0}'".format(folder)) os.mkdir(folder) lf = unrar_files( name, folder, fLOG=self.fLOG, fvalid=fvalid) files.extend(lf) else: # already done, we do not do it again pass elif ext == ".gz": folder = os.path.splitext(name)[0] + "_gz" folder = folder.replace(" ", "_").replace(",", "_") if not os.path.exists(folder): self.fLOG( "[ProjectsRepository.ungzip_files] ungzip '{0}'".format(name)) self.fLOG( "[ProjectsRepository.ungzip_files] creating '{0}'".format(folder)) os.mkdir(folder) unzip = "pkl.gz" not in name lf = ungzip_files( name, folder, fLOG=self.fLOG, fvalid=fvalid, unzip=unzip) files.extend(lf) else: # already done, we do not do it again pass elif ext == ".tar.gz": raise Exception("unable to process such a file: " + name) return files