def unzip_single(src_file, dest_dir, password=None): ''' 解压单个文件到目标文件夹。''' LockUtil.acquire() if password: password = password.encode() zf = zipfile.ZipFile(src_file) cwd = os.getcwd() os.chdir(dest_dir) root_path = None for name in zf.namelist(): zinfo = zf.getinfo(name) if zinfo.flag_bits & 0x800: fname_str = name else: fname_str = name.encode('cp437').decode('gbk') try: if fname_str.endswith('/') and fname_str.count( '/') == 1 and fname_str != name: root_path = name if not fname_str.endswith('/'): if not isdir( dirname(fname_str)) and len(dirname(fname_str)) > 0: makedirs(dirname(fname_str)) zf.extract(name, dest_dir, pwd=password) if name != fname_str: os.rename(name, fname_str) elif not isdir(fname_str): makedirs(fname_str) except RuntimeError: logUtils.logException('unzip_single') if root_path and isdir(root_path): rmtree(root_path) zf.close() os.chdir(cwd) LockUtil.release()
def work(thread:LooperThread): msg = 'working start in thread name : {}'.format(thread.getName()) logUtils.info(msg) try: action() except Exception: logUtils.logException('任务出错') msg = 'working end in thread name : {}'.format(thread.getName()) logUtils.info(msg) if not __allWork__.empty(): for work in __WORK_THREADS__: if work.queue.qsize() == 0 and not work.working: msg = 'post working name : {}'.format(work) logUtils.info(msg) work.post(__allWork__.get()) return if __allWork__.empty(): LockUtil.acquireLock(__WORK_THREAD_LOCK__) workCount = 0 for t in __WORK_THREADS__: if t.working: workCount = workCount+1 if workCount == 1: while not __Work_Done__.empty(): callback = __Work_Done__.get() callback() LockUtil.releaseLock(__WORK_THREAD_LOCK__)
def parseZipLog(fileName, resonFile: TextIOWrapper, packageName: str = DEFAULT_PACKAGE, removeDir=True, callbackMsg=None): logUtils.info("parLogZip : fileName={}, packageName={}".format( fileName.replace('\\', '/'), packageName)) callbackMsg('正在解析{}'.format(basename(fileName))) #如果不是pid文件则不解析 if not zipfile.is_zipfile(fileName): exit(-1) #获取文件路径和文件全名 (filepath, tempfilename) = os.path.split(fileName) #获取文件名和文件后缀 (name, extension) = os.path.splitext(tempfilename) #获取解压的文件路径 tempDir = sep.join([dirname(fileName), name]) #解压的文件路径如果存在就删除 if isdir(tempDir): try: rmtree(tempDir) except Exception: logUtils.logException('任务出错') #创建解压路径 makedirs(tempDir) #解压zip文件到指定路径 toolUtils.unzip_single(fileName, tempDir) #解析刚刚解压的文件 globalValues: GlobalValues = parseLogDir(tempDir, resonFile, packageName) #删除刚刚解压的临时文件夹 if removeDir: rmtree(tempDir) return globalValues
def encodeAndDecode(dest_dir: str): for root_path, dir_names, file_names in os.walk(dest_dir): for fn in dir_names: path = os.path.join(root_path, fn) if not zipfile.is_zipfile(path): try: fn = fn.encode('cp437').decode('utf-8') new_path = os.path.join(root_path, fn) os.rename(path, new_path) except Exception: logUtils.logException('encodeAndDecode')
def run(self): if callable(self.action): try: self.action() except Exception: logUtils.logException('任务出错')
def parserAnr(): value = select.get() file_path = entry.get() packageName = packageEntry.get().strip() if not packageName: packageName = DEFAULT_PACKAGE packageEntry.insert('insert', packageName) bar = GressBar() def downCallback(): time.sleep(1) bar.quit() start_file = '' if value == 0 : start_file = sep.join([foldPath, basename(file_path).replace('.zip','.txt') if '.zip' in basename(file_path) else 'reason.txt']) elif value == 1 : start_file = sep.join([file_path, '{}.txt'.format(basename(file_path))]) elif value == 2 : start_file = file_path if exists(start_file): startfile(start_file) addWorkDoneCallback(downCallback) if value == 0 : # tip.config(text='解析单个anr的zip文件(例如:Jira号/版本号/LogId.zip)') if zipfile.is_zipfile(file_path): text_view.delete('1.0', 'end') foldPath = dirname(abspath(file_path)) fileTxt = sep.join([foldPath, basename(file_path).replace('.zip','.txt') if '.zip' in basename(file_path) else 'reason.txt']) resonFile = open(file=fileTxt, mode='w', encoding='utf-8') resonFile.writelines('{}.{}\n\n'.format(str(1), abspath(file_path)[len(dirname(foldPath)) + 1:])) try: def parse(): globalValue = parseZipLog(file_path, resonFile, packageName=packageName, removeDir=True, callbackMsg=bar.updateMsg) resonFile.writelines("\n\n 解析有误或者有建议请邮箱[email protected](肖良)") resonFile.flush() resonFile.close() if len(globalValue.showMessage) > 0: text_view.insert('insert','\n'.join(globalValue.showMessage)) else: text_view.insert('insert','解析完成') postAction(action=parse) except: logUtils.logException("Error: unable to start thread") bar.start() else: messagebox.showwarning(title='错误', message='请选择anr的zip包!') elif value == 1: # tip.config(text='解析解析目录下所有anr文件(例如:项目/Jira号)') if isdir(file_path): text_view.delete('1.0','end') try: def parse(): globalValuesList = parserZipLogDir(file_path, packageName=packageName, removeDir=True, callbackMsg=bar.updateMsg) showMessages = ['\n'.join(globalValues.showMessage) for globalValues in globalValuesList if len(globalValues.showMessage)>0 ] if len(showMessages) > 0: text_view.insert('insert','\n'.join(showMessages)) else: text_view.insert('insert','解析完成') fileTxt = sep.join([file_path,'reason.txt']) postAction(action=parse) except: logUtils.logException("Error: unable to start thread") bar.start() else: messagebox.showwarning(title='错误', message='请选择带anr的zip的目录!') if value == 2: # tip.config(text='解析解析目录下所有anr文件(例如:/项目)') if isdir(file_path): text_view.delete('1.0','end') for foldPath in [sep.join([file_path, child]) for child in listdir(file_path)]: def getAction(path): def action(): if isdir(path): globalValuesList = parserZipLogDir(path, packageName=packageName, removeDir=True, callbackMsg=bar.updateMsg) showMessages = ['\n'.join(globalValues.showMessage) for globalValues in globalValuesList if len(globalValues.showMessage)>0 ] if len(showMessages) > 0: text_view.insert('insert','\n'.join(showMessages)) return action try: postAction(getAction(foldPath)) except: logUtils.logException("Error: unable to start thread") bar.start() else: messagebox.showwarning(title='错误', message='请选择带anr的zip的目录!')