def process_wrapper(fname, chunkStart, chunkSize, keyword="reportCallFailLD ="): # print '----- process_wrapper PID:%s , chunkSize:%d ' % (os.getpid(), chunkSize) with open(fname) as f: f.seek(chunkStart) lines = f.read(chunkSize).splitlines() analyLogBeanList = [] for line in lines: # print '---> line: %s, keyword: %s ' % (line, keyword) textLine = str(_translateUtf8(line)) # print '--> textLine: ', textLine textLineLower = textLine.lower() keywordIndex = textLineLower.find(str(_translateUtf8(keyword)).lower()) if keywordIndex != -1: # 匹配以时间开头,除换行符"\n"之外的任意字符 reLogStr = r'(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3}.*)' # 匹配时间 reTimeStr = r'(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3})' # 一个文件中可能有多个异常Log打印信息 searchedLogList = re.findall(reLogStr, textLine) if not searchedLogList: continue # print 'searchedLogList: %s --> filePath: %s ' % (searchedLogList, file_path) for searchedLog in searchedLogList: if not searchedLog: continue # print 'searchedLog: %s --> filePath: %s ' % (searchedLog, file_path) logTime = re.search(reTimeStr, str(searchedLog)).group(1) analyLogBean = AnalyticsLogBean() analyLogBean.keyword = keyword analyLogBean.logTxt = searchedLog analyLogBean.filePath = fname analyLogBean.logTime = logTime analyLogBeanList.append(analyLogBean) return analyLogBeanList
def process_wrapper(fname, chunkStart, chunkSize, keyword="base attribute info"): # print '----- process_wrapper PID:%s , chunkSize:%d ' % (os.getpid(), chunkSize) with open(fname) as f: f.seek(chunkStart) lines = f.read(chunkSize).splitlines() baseAttrBeanList = [] for line in lines: # print '---> line: %s, keyword: %s ' % (line, keyword) textLine = str(_translateUtf8(line)) # print '--> textLine: ', textLine textLineLower = textLine.lower() keywordIndex = textLineLower.find( str(_translateUtf8(keyword)).lower()) if keywordIndex != -1: baseAttrJson = filterBaseAttr2Json(textLine) # print '==> baseAttrJsonDict: ', baseAttrJson if baseAttrJson: baseAttr = BaseAttrBean() baseAttr.binderNumber = baseAttrJson['mId'] baseAttr.machineMode = baseAttrJson['devName'] baseAttr.osVersion = baseAttrJson['osVer'] # print 'baseAttr: ', baseAttr baseAttrBeanList.append(baseAttr) return baseAttrBeanList
def process_wrapper(fname, chunkStart, chunkSize, keyword): # print '----- process_wrapper PID:%s , chunkSize:%d ' % (os.getpid(), chunkSize) with open(fname) as f: f.seek(chunkStart) lines = f.read(chunkSize).splitlines() filterLines = "" for line in lines: # print '---> line: %s, keyword: %s ' % (line, keyword) textLine = str(_translateUtf8(line)) # print '--> textLine: ', textLine textLineLower = textLine.lower() keywordIndex = textLineLower.find( str(_translateUtf8(keyword)).lower()) if keywordIndex != -1: filterLines += textLine + "\n" if filterLines: return filterLines
def process(line, fname): filterLines = "" textLine = str(_translateUtf8(line)) print '--> textLine: ', textLine textLineLower = textLine.lower() keywordIndex = textLineLower.find("reportCallFailLD =") if keywordIndex != -1: filterLines += textLine return filterLines
def searchWordInFile(self, keyword, file_path, log_call_back=None): if not file_path or not keyword.strip(): return filePath = _translate('', file_path, None) file = QFile(filePath) if not file.open(QtCore.QIODevice.ReadOnly): logMsg = u'无法打开文件:' + _translateUtf8(file_path) self.dologCallBack(log_call_back, logMsg) file.close() return logMsg = u'正在分析文件:' + _translateUtf8(file_path) self.dologCallBack(log_call_back, logMsg) # stream = QtCore.QTextStream(file) # stream.setCodec('UTF-8') # data = stream.readAll() # file.close() # stream.flush() # dataTmp = StringIO.StringIO(data) searchedText = '' # while True: with open(filePath) as f: print 'len(file): ', len(f.readlines()) for line in f: textLine = str(_translateUtf8(line)) if textLine == '': # logMsg = u'已分析完文件:' + _translateUtf8(file_path) # log_call_back(logMsg) # print logMsg break textLineLower = textLine.lower() keywordIndex = textLineLower.find(keyword.lower()) if keywordIndex != -1: searchedText += textLine # release StringIO memory # dataTmp.close() return searchedText
def searchKeywordCallBack(self, status, file_path, searchedLogJobs): # print '-0---searchKeywordCallBack keyword:%s == lines: %s, status: %s' % (self.analyKeyword, searchedLogList, status) if not self.analyKeyword: return if status == SearchByMultiProcess.STATUS_PROCESSING: logMsg = u'正在分析文件:' + _translateUtf8(file_path) self.dologCallBack(self.emitAppendLogSignal, logMsg) return if not searchedLogJobs: return for searchedLogJob in searchedLogJobs: if not searchedLogJob: continue for searchedLog in searchedLogJob: if not searchedLog: continue self.filterAnalyLog2List(self.analyticsLogList, searchedLog)
def recursiveUnZipFile(self, src_dir): if not src_dir: self.doCallBack(u'请输入需要解压缩的路径') return dest_dir = None zip_ref = None tar_ref = None try: allZipFileList = FileUtil.getAllFilesByExt(src_dir, 'zip') allGzFileList = FileUtil.getAllFilesByExt(src_dir, 'gz') # print '--->src_dir:%s allGzFileList:%s ' % (src_dir, allGzFileList) if not allZipFileList and not allGzFileList: logStr = u'在目录 ' + _translateUtf8( src_dir) + u' 及子目录下未找到 zip 文件' self.doCallBack(logStr) return for file_path in allZipFileList: log_txt = u'正在解压文件: ' + _translateUtf8(file_path) self.doCallBack(log_txt) # print str(_translateUtf8(file_path)) dest_dir = _translateUtf8( FileUtil.getFilePathWithName(file_path)) zip_ref = zipfile.ZipFile(unicode(file_path), 'r') # FileUtil.mkdirNotExist(str(dest_dir)) # zipfile 会自动创建 zip_ref.extractall(unicode(dest_dir)) zip_ref.close() self.recursiveUnZipFile(dest_dir) for file_path in allGzFileList: log_txt = u'正在解压文件: ' + _translateUtf8(file_path) self.doCallBack(log_txt) dest_dir = str( _translateUtf8(FileUtil.getFilePathWithName(file_path))) if FileUtil.getFileExt(dest_dir).find('tar') != -1: dest_dir = str( _translateUtf8(FileUtil.getFilePathWithName(dest_dir))) # print '----dest_dir: ', dest_dir tar_ref = tarfile.open(unicode(file_path)) tar_ref.extractall(unicode(dest_dir)) tar_ref.close() self.recursiveUnZipFile(dest_dir) except: if dest_dir: self.recursiveUnZipFile(dest_dir) finally: if zip_ref: zip_ref.close() if tar_ref: tar_ref.close()
def appendLog(self, logTxt): self.LogTextEdit.append(_translateUtf8(logTxt))
def doGenDocFile(self, log_call_back): docTitle = u'# 问题分析' docSecondTitle = u'## 上层分析' docTempleteBinder = u'绑定号: ' docTempleteMachine = u'+ 机型 : ' docTempleteTime = u'+ 时间点: ' docTempleteNetworkType = u'+ 通话类型:' docTempleteDialMode = u'+ 通话方向:' docTempleteCause = u'+ 掉话code: ' docTempleteDetail = u'+ 详情:' print 'callFailList len: ', len(self.callFailList) for callFail in self.callFailList: docFilePath = callFail.logFilePath binderNumber = callFail.binderNumber docFilePathTmp = re.findall(r'([A-Za-z0-9_.]+)', docFilePath) docFilePath.rfind(binderNumber) fileDirPath = '' for fileDirPathTmp in docFilePathTmp: if fileDirPathTmp == binderNumber: fileDirPathIndex = docFilePath.find(binderNumber) fileDirPath = docFilePath[:fileDirPathIndex + len(binderNumber) + 1] break elif fileDirPathTmp.find(binderNumber) != -1: fileDirPathIndex = docFilePath.find(binderNumber) fileDirPath = docFilePath[:fileDirPathIndex] else: fileDirPath = os.path.join( str(self.selectDirectoryLineEdit.text()), binderNumber) FileUtil.mkdirNotExist(fileDirPath) print 'docFilePath: ', fileDirPath fileName = binderNumber + u'_问题分析.txt' filePath = os.path.join(fileDirPath, fileName) hasFileExists = os.path.exists(filePath) docFile = open(filePath, 'a+') docContentBinderNumber = str(docTempleteBinder + binderNumber).encode('utf-8') docContentMachine = str(docTempleteMachine + callFail.machineMode + "\t" + callFail.osVersion).encode('utf-8') docContentTime = str(docTempleteTime + callFail.failTime).encode('utf-8') docContentNetworkType = str(docTempleteNetworkType + callFail.voiceNetworkType).encode( 'utf-8') docContentDialMode = str(docTempleteDialMode + callFail.dialMode).encode('utf-8') docContentCause = str(docTempleteCause + callFail.vendorCauseCode).encode('utf-8') docContentDetail = str(docTempleteDetail + callFail.logText).encode('utf-8') if not hasFileExists: docFile.write('\n' + docTitle + '\n') docFile.write('\n' + docContentBinderNumber + '\n') docFile.write('\n' + docSecondTitle + '\n') docFile.write('\n' + docContentMachine + '\n') else: docFile.write('\n\n\n') docFile.write(docContentTime + '\n') docFile.write(docContentNetworkType + '\n') docFile.write(docContentDialMode + '\n') docFile.write(docContentCause + '\n') docFile.write(docContentDetail + '\n') docFile.flush() docFile.close() logMsg = u'已生成文档:' + _translateUtf8(filePath) log_call_back(logMsg) print logMsg self.release() logMsg = u'---------- 文档生成完毕 -----------' log_call_back(logMsg) self.emitTrayMsgSignal(u'文档生成完毕')