1# encoding=utf-8 2 3""" 4====================================================================================== 5copyright (C) 2018-2019, Huawei Technologies Co. 6======================================================================================== 7@文件名称: Download.py 8@描述: 文件下载相关动作 9@作者: lwx587017 10@生成日期: 20181019 11====================================================================================== 12""" 13import os 14import tarfile 15from aw.Common.Common import copyFile, copyDirectory 16from util.log_info import logger 17 18def extractZipFile(zip_file, extract_dir): 19 ''' 20 #=================================================================================== 21 # @Method: extractZipFile(zip_file, download_dir) 22 # @Precondition: none 23 # @Func: 把压缩文件解压到指定目录 24 # @PostStatus: none 25 # @Param: zip_file:压缩文件路径 26 # extract_dir:解压的目标文件夹 27 # params:已获取需要参数的字典 28 # os_method:绑定操作系统用到公共方法的对象 29 # @eg: excutedown("xxxx", "D:\\local\\image", Flase, {"xxx":"xxx"}, os_method) 30 # @return: True or Flase 31 #=================================================================================== 32 ''' 33 if not os.path.isfile(zip_file): 34 logger.error("Decompress package failed.") 35 return False 36 import zipfile 37 if not os.path.isdir(extract_dir): 38 os.mkdir(extract_dir) 39 40 f = zipfile.ZipFile(zip_file) 41 f.extractall(extract_dir) 42 f.close() 43 zfile = None 44 try: 45 zfile = zipfile.ZipFile(zip_file) 46 for file_name in zfile.namelist(): 47 if file_name.endswith("filelist.xml") or file_name.endswith(".zip"): 48 file = zfile.extract(file_name, extract_dir) 49 if not os.path.isfile(os.path.join(extract_dir, os.path.basename(file_name))): 50 copyFile(file, extract_dir) 51 return True 52 except Exception as e: 53 logger.error(e) 54 return False 55 finally: 56 if zfile: 57 try: 58 zfile.close() 59 except Exception as e: 60 logger.error(e) 61 62def unTarFile(tar_file, dest_dir): 63 ''' 64 #=================================================================================== 65 # @Method: unTarFile(tar_file, dest_dir) 66 # @Precondition: none 67 # @Func: 把压缩文件解压到指定目录 68 # @PostStatus: none 69 # @Param: tar_file:压缩文件路径 70 # dest_dir:解压的目标文件夹 71 # @eg: excutedown("xxxx", "D:\\local\\image") 72 # @return: True or Flase 73 #=================================================================================== 74 ''' 75 import platform 76 if platform.system() == "Windows": 77 dest_dir = "\\\\?\\%s" % dest_dir 78 try: 79 logger.info("untar %s to %s " % (tar_file, dest_dir)) 80 utar = tarfile.open(tar_file) 81 utar.extractall(path = dest_dir) 82 utar.close() 83 img_path = os.path.join(dest_dir, "resource.img") 84 if not os.path.exists(img_path): 85 logger.error("no img") 86 return False 87 return True 88 except Exception as e: 89 logger.error(e) 90 return False 91 92def backupFile(file): 93 ''' 94 #=================================================================================== 95 # @Method: backupFile(file) 96 # @Precondition: none 97 # @Func: 备份文件 98 # @PostStatus: none 99 # @Param: file:文件名 100 # @eg: backupFile("xxx") 101 # @return: True or False 102 #=================================================================================== 103 ''' 104 logger.info("file is %s" % file) 105 if not os.path.isfile(file): 106 logger.error("%s file is not exist" % file) 107 return False 108 109 backup_file = file + ".bak" 110 if os.path.exists(backup_file): 111 os.remove(backup_file) 112 try: 113 logger.info("start to backup file and backup_file is %s" % backup_file) 114 os.rename(file, backup_file) 115 except: 116 logger.info("the file may access by another process") 117 time.sleep(10) 118 return False 119 120 if os.path.isfile(file): 121 os.remove(file) 122 return True 123 124 125