def getCheckListResult(self, packId, root, taskId,resultId,judgeTaskResult): fname = os.path.abspath(str.format('../result/{}/{}/{}.xml',taskId, resultId,packId)) print "fname:",fname if os.path.exists(fname): ET.SubElement(root, "ResultId").text = str(resultId) listCount = TaskInfo.select().where(TaskInfo.taskResultId == judgeTaskResult).count() ET.SubElement(root, "ListCount").text = str(listCount) # 获取指定包编号的检查结果信息 checklistResult = ET.ElementTree(file=fname) checklist = ET.SubElement(root, "CheckList") counter = 0 for elem in checklistResult.iter(tag="CheckItem"): checklist.append(elem) counter+=1 fileCheckListString = ET.tostring(checklist,encoding="UTF-8").replace("<?xml version='1.0' encoding='UTF-8'?>","") strHash = calcHash(fileCheckListString) ET.SubElement(root, "PackCount").text = str(counter) ET.SubElement(root, "PackId").text = str(packId) ET.SubElement(root, "Md5").text = str(strHash) ET.SubElement(root, "State").text = Codes.SUCCESS else: ET.SubElement(root, "State").text = Codes.INNERERROR
def sendCheckList(self, userName, password, taskId, checkListData, packId, packCount, listCount, md5Hash): # taskId:任务编号 # userName:用户名 # password:密码 # checkListData: 需要抓取的任务--xml格式 # packId:此xml文件的packageId # packCount:需要发送的总包数 # listCount:本包中包含多少条记录 # md5Hash:接收成功后XML字符串的md5 hash # taskCount:需要发送的任务总数 root = ET.Element("ReturnInfo") try: user_result = validateuser(userName, password) if user_result is not None: taskEnity = Task.getOne(Task.taskId == taskId) if taskEnity is not None: if checkListData == "": ET.SubElement(root, "State").text = Codes.PARAMSERROR else: if packId > 0 and packCount > 0 and listCount > 0: #传递的hash print 'md5Hash:',md5Hash #print "checkListData:",checkListData.encode("UTF-8") # 校验hash strHash = calcHash(checkListData.encode("UTF-8")) print "strHash:",strHash if strHash==md5Hash: ET.SubElement(root, "TaskId").text = taskId dirPath = os.path.abspath( str.format(os.path.join('../temp/{}/'), taskId)) if not os.path.exists(dirPath): os.makedirs(dirPath) filePath = dirPath+"/"+ str(packId) + '.xml' saveResullt = writeXML(filePath, checkListData) if saveResullt is None: ET.SubElement(root, "State").text = Codes.COMPANYFILEISEXISTS elif saveResullt is True: # 根据taskId目录下的文件个数判断是否接收完整 fileCount = len(os.listdir('../temp/%s' % taskId)) # 当所有的包完整接收之后进行数据校验再向数据库中添加任务记录 if fileCount == packCount: # 主任务结果 taskResult = TaskResult() taskResult.taskId = taskEnity taskResult.state = '1' q = taskResult.save(force_insert=True) if q == 1: checkAllLz.delay(os.path.abspath(dirPath), taskResult.taskResultId) ET.SubElement(root, "State").text = Codes.SUCCESS else: ET.SubElement(root, "State").text = Codes.INTERIORERROR else: ET.SubElement(root, "State").text = Codes.SUCCESS else: # 记录不完整的packid pId = taskEnity.missPackId if pId != "" and pId.find(str(packId))==-1: packIds = pId + "," + str(packId) else: packIds = str(packId) q = Task.update(missPackId=packIds).where(Task.taskId == taskId) q.execute() ET.SubElement(root, "State").text = Codes.VALIDERROR else: ET.SubElement(root, "State").text = Codes.PARAMSERROR else: ET.SubElement(root, "State").text = Codes.TASKIDERROR else: ET.SubElement(root, "State").text = Codes.USERERROR except Exception, e: ET.SubElement(root, "State").text = Codes.INNERERROR print e