How to use uncompress method in avocado

Best Python code snippet using avocado_python

DecompressionTool.py

Source:DecompressionTool.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-23"""4@Author : LIAN5@Project : ToolComponents6@File : DecompressionTool.py7@Time : 2022/03/038@License : (C) Copyright 2022, RoarPanda Corporation.9"""1011"""12解压模块,支持多种解压方法(tar,gz,xz,bz2,zip,rar,7z),支持多级解压13前提添加: sudo apt-get install unar / python3.5 rarfile 3.0 14"""1516import datetime17import zipfile18import subprocess19import rarfile20import hashlib21import os22import magic232425class My7z(object):26 file_path = None27 uncompress_password = None2829 def __init__(self, file_path):30 self.file_path = file_path31 if not os.path.exists(self.file_path):32 raise FileNotFoundError3334 def setpassword(self, uncompress_password=None):35 self.uncompress_password = uncompress_password3637 def extractall(self, path=None):38 if path is None:39 path = "CompressFile"40 # uncompress_command = "7z x -o{} -p{} {}".format(path, self.uncompress_password, self.file_path)41 if self.uncompress_password is None or self.uncompress_password == "":42 uncompress_command = "unar -f {} -o {}".format(self.file_path, path)43 else:44 self.uncompress_password = str(self.uncompress_password)45 uncompress_command = "unar -f -p {} {} -o {}".format(self.uncompress_password, self.file_path, path)46 p = subprocess.Popen(uncompress_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)47 print(p.stdout.read())48 print(p.stderr.read())49 p.communicate()50 print("uncompress status code: ")51 print(p.returncode)52 return p.returncode53 # if p.returncode != 0:54 # raise Exception("password wrong")555657class TarArchive(object):58 file_path = None5960 def __init__(self, file_path):61 self.file_path = file_path62 if not os.path.exists(self.file_path):63 raise FileNotFoundError6465 def extractall(self, path=None):66 if path is None:67 path = "CompressFile"68 p = subprocess.Popen("tar -xf {} -C {}".format(self.file_path, path),69 shell=True,70 stdout=subprocess.PIPE,71 stderr=subprocess.PIPE)72 p.communicate()73 if p.returncode != 0:74 raise Exception("Extract Error")757677# 为文件分配对应的解压缩对象78class UncompressDispatch(object):79 uncompress_obj = None8081 def __init__(self, file_path=None, file_type=None, uncompress_password=None):82 self.fileType = file_type83 self.filePath = file_path84 self.uncompressPassword = uncompress_password85 self.init_uncompress_obj(file_path)8687 def init_uncompress_obj(self, file_path):88 if "zip" == self.fileType.split("/")[-1].strip():89 try:90 self.uncompress_obj = zipfile.ZipFile(file_path)91 except Exception:92 self.fileType = "Unrecognized file type"93 return94 if "AndroidManifest.xml" in self.uncompress_obj.namelist():95 self.fileType = "APK file, Android"96 return97 elif "[Content_Types].xml" in self.uncompress_obj.namelist():98 self.fileType = "Microsoft Office file"99 return100 # Use 7z uncompress method to deal101 self.uncompress_obj = My7z(file_path)102 if self.uncompressPassword is not None:103 self.uncompress_obj.setpassword(self.uncompressPassword)104105 elif self.fileType.split("/")[-1].strip() in ["x-tar", "gzip", "x-xz", "x-bzip2"]:106 # self.uncompress_obj = TarArchive(file_path)107 self.uncompress_obj = My7z(file_path)108109 elif "rar" in self.fileType:110 # self.uncompress_obj = rarfile.RarFile(file_path)111 # if self.uncompressPassword is not None:112 # self.uncompress_obj.extractall(path= ,pwd=self.uncompressPassword)113 self.uncompress_obj = My7z(file_path)114 if self.uncompressPassword is not None:115 self.uncompress_obj.setpassword(self.uncompressPassword)116117 elif "7z" in self.fileType:118 self.uncompress_obj = My7z(file_path)119 if self.uncompressPassword is not None:120 self.uncompress_obj.setpassword(self.uncompressPassword)121122 else:123 pass124125 def extract(self, path=None):126 if "zip" == self.fileType.split("/")[-1].strip():127 return self.uncompress_obj.extractall(path=path)128 elif self.fileType.split("/")[-1].strip() in ["x-tar", "gzip", "x-xz", "x-bzip2"]:129 return self.uncompress_obj.extractall(path=path)130 elif "rar" in self.fileType:131 return self.uncompress_obj.extractall(path=path)132 elif "7z" in self.fileType:133 return self.uncompress_obj.extractall(path=path)134 else:135 pass136137 def rar_extract(self, path=None, extract_dir=None):138 rar = rarfile.RarFile(path)139 if self.uncompressPassword is not None:140 rep = rar.extractall(path=extract_dir, pwd=self.uncompressPassword)141 else:142 rep = rar.extractall(path=extract_dir)143 return rep144145146class UncompressMod(object):147 fileMD5 = None148 zipObj = None149 uncompressObj = None150151 def __init__(self, file_path=None, uncompress_password=None):152153 if os.path.exists(file_path):154 self.filePath = file_path155 self.fileType = self.file_distinguish()156 self.uncompress_password = uncompress_password157 self.uncompressObj = UncompressDispatch(self.filePath,158 self.fileType,159 self.uncompress_password)160 with open(file_path, "rb") as fp:161 md5 = hashlib.md5()162 md5.update(fp.read())163 self.fileMD5 = md5.hexdigest()164 fp.close()165166 def file_distinguish(self):167 magic_obj = magic.Magic(mime=True)168 with open(self.filePath, "rb") as f:169 msg = magic_obj.from_buffer(f.read())170 del magic_obj171 return msg172173 def extract_file(self, extract_path=None):174175 if self.uncompressObj.uncompress_obj is None:176 return None, 1177178 if extract_path is None:179 _, file_name = os.path.split(self.filePath)180 extract_path, _ = os.path.splitext(file_name)181182 tmp_extract_path = extract_path183 if not os.path.exists(tmp_extract_path):184 os.mkdir(tmp_extract_path)185186 try:187 if "rar" in self.fileType:188 uncom_code = self.uncompressObj.rar_extract(path=self.filePath, extract_dir=tmp_extract_path)189 if uncom_code is None:190 return tmp_extract_path, 0191 else:192 return uncom_code193 else:194 uncom_code = self.uncompressObj.extract(tmp_extract_path)195 if uncom_code != 0:196 return None, uncom_code197 else:198 return tmp_extract_path, 0199 except Exception as e:200 print(str(e))201 if 'The specified password is incorrect' in str(e) and self.uncompress_password is not None:202 # Password Error203 return None, 1204 # Need Password205 return None, 2206207208class RecursionUncompress(object):209 """210 Main Uncompress Process211 """212213 @staticmethod214 def extract_with_level(file_path, extract_level, uncompress_password=None, extract_dir="tmp_{}_{}"):215 """216 按“extract_level”等级的解压力度解压路径在“file_path”下的文件217 :param file_path: 要解压文件的路径218 :param extract_level: 解压等级219 :param uncompress_password: 解压密码 Bytes220 :return:221 (解压到的主文件夹,解压完成的文件集合)222 """223 compress_file_queue_list = list()224 detect_sample_set = set()225 # Init compressed file list226 compress_file_queue_list.append(file_path)227 is_first = 1228 first_dir = ""229 # Use Queue to deal with breadth-first traversal230 while len(compress_file_queue_list) != 0:231 if extract_level == 0:232 break233 if is_first == 1:234 with open(file_path, "rb") as fp:235 file_md5 = hashlib.md5(fp.read()).hexdigest()236 fp.close()237 now_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S")238 base_extract_path = extract_dir.format(now_time, file_md5)239 first_dir = base_extract_path240 else:241 base_extract_path, _ = os.path.split(compress_file_queue_list[-1])242 # Extract compressed file243 extract_file_path = compress_file_queue_list.pop()244 tmp_object = UncompressMod(file_path=extract_file_path, uncompress_password=uncompress_password)245 if None in [tmp_object.filePath, tmp_object.uncompressObj]:246 continue247 tmp_extract_path, e_flag = tmp_object.extract_file(base_extract_path)248 del tmp_object249250 # Can't Extract perhaps have password251 if tmp_extract_path is None:252 # Return Root file253 if is_first == 1:254 if e_flag == 2:255 return first_dir, 2256 detect_sample_set.add(extract_file_path)257 continue258259 # Remove compressed file (do not delete the root file)260 if is_first == 0:261 os.remove(extract_file_path)262 else:263 is_first = 0264265 # Record compressed files into queue266 for base_dir, _, files in os.walk(tmp_extract_path):267 for f in files:268 file_abspath = os.path.join(base_dir, f)269 tmp_object = UncompressMod(file_abspath)270 if tmp_object.uncompressObj is None:271 detect_sample_set.add(file_abspath)272 del tmp_object273 continue274 if tmp_object.uncompressObj.uncompress_obj is not None:275 compress_file_queue_list.append(file_abspath)276 if file_abspath != extract_file_path:277 detect_sample_set.add(file_abspath)278 else:279 # Record simple files into detect list280 detect_sample_set.add(file_abspath)281 del tmp_object282 extract_level -= 1283 return first_dir, detect_sample_set284285286class CompressedHandler(object):287 """288 功能:压缩文件的解压289 流程:1. 校验是否是压缩文件 2. 提取子文件 3. 提取成功,父文件子文件推进检测并入库 4. 提取失败,压缩文件入库290 """291292 def __init__(self, file_path=None, unzip_path=None, extract_level=1, uncompress_password=None, recheck=0):293 """294 :param file_path: 解压文件路径295 :param unzip_path: 解压路径296 :param extract_level: 解压等级297 :param uncompress_password: 解压密码298 :param recheck: 是否要对已经检测过的解压缩文件重新进行检测299 :return 0 正常 -1 异常 2 需要密码300 """301 self.file_path = file_path302 self.extract_level = extract_level303 self.uncompress_password = uncompress_password304 self.recheck = recheck305 self.uncompress_obj = RecursionUncompress306 self.unzip_path = unzip_path307308 def compressed_handler(self):309310 # 进行解包操作311 extract = self.compressed_extract()312 if extract == -1:313 print("compressed file extract fail !!!")314 return -1315316 if extract == 2:317 # 保存主文件 需要密码318 return 2319320 return extract321322 def compressed_extract(self):323 # 解压文件,获取解压文件的根目录和其子文件路径324 try:325 # extract_path 解压文件路径 sample_file_set 子文件326 extract_path, sample_file_set = self.uncompress_obj.extract_with_level(file_path=self.file_path,327 extract_level=self.extract_level,328 uncompress_password=self.uncompress_password,329 extract_dir=self.unzip_path)330 except Exception as e:331 print(str(e))332 return -1333334 if type(sample_file_set) is int:335 print("Need Password")336 return 2337338 return extract_path, sample_file_set339340341if __name__ == '__main__':342 # 使用示例343 # file_path: 解压文件路径344 # unzip_path: 解压路径345 # extract_level: 解压等级346 # uncompress_password: 解压密码347 # recheck: 是否要对已经检测过的解压缩文件重新进行检测348 # return -1----解压失败 2--需要密码 其它-- extract_path 解压文件路径 sample_file_set 子文件349 file_path = "/home/rpadmin/test/testmim4.rar"350 unzip_path = "/home/rpadmin/test/"351 extract_level = 1352 uncompress_password = "123456"353354 compressed_result = CompressedHandler(file_path=file_path, unzip_path=unzip_path, extract_level=extract_level,355 uncompress_password=uncompress_password, recheck=0).compressed_handler() ...

Full Screen

Full Screen

test_hw6.py

Source:test_hw6.py Github

copy

Full Screen

...10 def test01(self):11 sequence = '0' * 6412 compress = hw6.compress(sequence)13 self.assertEqual(compress, '1111100000111110000000010')14 uncompress = hw6.uncompress(compress)15 self.assertEqual(uncompress, sequence)16 self.assertAlmostEqual(hw6.compression(sequence), 0.390625, 4)17 def test02(self):18 sequence = '01' * 3219 compress = hw6.compress(sequence)20 self.assertEqual(compress, '00001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001')21 uncompress = hw6.uncompress(compress)22 self.assertEqual(uncompress, sequence)23 self.assertAlmostEqual(hw6.compression(sequence), 5.0, 4)24 def test03(self):25 sequence = '10' * 3226 compress = hw6.compress(sequence)27 self.assertEqual(compress, '0000000001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001000010000100001')28 uncompress = hw6.uncompress(compress)29 self.assertEqual(uncompress, sequence)30 self.assertAlmostEqual(hw6.compression(sequence), 5.078125, 4)31 def test04(self):32 sequence = '0' * hw6.MAX_RUN_LENGTH + '1' * hw6.MAX_RUN_LENGTH + '0' * (64 - 2 * hw6.MAX_RUN_LENGTH)33 compress = hw6.compress(sequence)34 self.assertEqual(compress, '111111111100010')35 uncompress = hw6.uncompress(compress)36 self.assertEqual(uncompress, sequence)37 self.assertAlmostEqual(hw6.compression(sequence), 0.234375, 4)38 def test05(self):39 sequence = '0' * (hw6.MAX_RUN_LENGTH + 1) + '1' * (hw6.MAX_RUN_LENGTH + 1) + '0' * (64 - 2 * hw6.MAX_RUN_LENGTH - 2)40 compress = hw6.compress(sequence)41 self.assertEqual(compress, '111110000000001111110000000001')42 uncompress = hw6.uncompress(compress)43 self.assertEqual(uncompress, sequence)44 self.assertAlmostEqual(hw6.compression(sequence), 0.46875, 4)45 def test06(self):46 sequence = '1' * hw6.MAX_RUN_LENGTH + '0' * hw6.MAX_RUN_LENGTH + '1' * (64 - 2 * hw6.MAX_RUN_LENGTH)47 compress = hw6.compress(sequence)48 self.assertEqual(compress, '00000111111111100010')49 uncompress = hw6.uncompress(compress)50 self.assertEqual(uncompress, sequence)51 self.assertAlmostEqual(hw6.compression(sequence), 0.3125, 4)52 53 def test07(self):54 sequence = '1' * 6455 compress = hw6.compress(sequence)56 self.assertEqual(compress, '000001111100000111110000000010')57 uncompress = hw6.uncompress(compress)58 self.assertEqual(uncompress, sequence)59 self.assertAlmostEqual(hw6.compression(sequence), 0.46875, 4)60 def test08(self):61 sequence = '11' * 3262 compress = hw6.compress(sequence)63 self.assertEqual(compress, '000001111100000111110000000010')64 uncompress = hw6.uncompress(compress)65 self.assertEqual(uncompress, sequence)66 self.assertAlmostEqual(hw6.compression(sequence), 0.46875, 4) 67 68if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful