def getFtpFileMain(self): while True: ml = MyLog() scanSN = ScanSernum() SN = scanSN.scanMain() snlist = [] if "/" in SN: #Input file with open(SN,'r') as snfile: for asn in snfile.readlines(): if "FDO" in asn: snlist.append(asn.strip()) else: snlist.append(SN) myarea = ScanAreaType() logarea = myarea.askArea() logtype = myarea.asklogType().lower() if "&" in logtype: logtype = "FDO" #Copy mview and mtype else: logtype = logtype[1:] #the logtype finally value is type or view logList = [] for sn in snlist: pcsn = GetPCSN() pcsntuple = pcsn.getMain(sn) childsn = pcsntuple[0] parentsn = pcsntuple[1] self.chDir() getlogpath = GetLogPath() childFolderList = getlogpath.getMain(parentsn,childsn,logarea) input_str = '>>>>Start find SN: %s/%s test log in ftp server'%(childsn, parentsn) ml.info(input_str) print UseStyle(input_str, fore='blue') for childFolder in childFolderList: if "KFCR" in childFolder: KFCRYear = int(childFolder.split('/')[1]) KFCRWeek = int(childFolder.split('/')[3][2:]) #Rename the KFCR log name during copying it to ftp server on 2018/3/2 if KFCRYear <= 2018 and KFCRWeek < 10 or KFCRYear == 2017: logtype = "FDO" #Both mtype and mview will in zip file, eg:FDO2114B0KD_1491454226.zip myftp = LoginFTP() ftp = myftp.ftpserver() ftp.cwd('/') #Enter the FTP top folder ftp.cwd(self.baseFtpFolder) try: ftp.cwd(childFolder) for fileList in ftp.nlst(): if (childsn in fileList or parentsn in fileList) and logtype in fileList: input_str = '--->Copy file:%s to %s'%(fileList,os.getcwd()) print UseStyle(input_str, fore='blue') ml.info(input_str) if not os.path.lexists(fileList): #Log already exist in /usr/auto/testlog mydlfile = DLFtpFile() mydlfile.downloadFile(ftp, fileList) logList.append(fileList) except Exception, err: #The script still running although met error outStr = r"!!!No file in C:/Backup/BU3/%s in FTP server, ERR:%s"%(childFolder,err) print UseStyle(outStr, fore='black', back='yellow') if len(childFolderList) == 1: break finally: pass
def displayItem(self, items): outStr = '||'*30 print UseStyle(outStr, fore='blue') outStr = '||' + 'List as below' print UseStyle(outStr, fore='blue') for item in sorted(items): outStr = '||' + item + '=' + items.get(item) print UseStyle(outStr, fore='blue') outStr = '||'*30 print UseStyle(outStr, fore='blue')
def downloadFile(self, ftp, filename): try: ftp.retrbinary('RETR %s'%filename, open(filename,'wb').write, 1024) except Exception, err: outStr = "!!!Get file failed n_n!: %s" %err print UseStyle(outStr, mode='bold', fore='white', back='red') EXIT()
def ftpserver(self): try: ftp = ftplib.FTP(self.host, self.user, self.passw) except socket.error, socket.gaierror: outStr = "!!!FTP is unaviable, plese check the host, user and passw" print UseStyle(outStr, mode='bold', fore='white', back='red') EXIT()
def getMain(self, sernum): TYPE = "P" if ord(sernum[7]) > 64: TYPE = "C" #Do snpull if TYPE == "P": pp = subprocess.Popen(['agen.exe', 'snpull', 'MAN~', sernum], stdout=subprocess.PIPE) else: pp = subprocess.Popen(['agen.exe', 'snpull', 'MAN~', '-c', sernum], stdout=subprocess.PIPE) output, error = pp.communicate() if pp.returncode != 0: outStr = "snpull failed" print UseStyle(outStr, fore='black', back='yellow') #Capture parenet SN/Children SN p1 = subprocess.Popen(['agend.exe', 'genparent', TYPE, sernum], stdout=subprocess.PIPE) output = p1.communicate()[0] if p1.returncode != 0: outStr = "####Get parent/child fail, ending the routine....\n" print UseStyle(outStr, mode='bold', fore='white', back='red') EXIT() csn = '' psn = '' if TYPE == "C": psn = sernum alst = output.split('\n') for astr in alst: if '->' in astr: #blst = ['->', 'FDO2146KG85,', 'BLWR-RPS2300=', '(ASSY)'] blst = astr.split() if products.get(blst[2][:-3]): csn = blst[1][:-1] else: csn = sernum #alst = ['Top', 'Level', 'S/N', ':', 'FDT2149PG0C,', 'PWR-RPS2300', '(ASSY)'] alst = output.split('\n')[0].split() psn = alst[4][:-1] csn = pcn if csn == "NONE" else csn psn = csn if psn == "NONE" else psn csn = psn if csn == "" else csn psn = csn if psn == "" else psn return (csn, psn)
def scanMain(self): while True: sernum = (raw_input("Enter Sernum[0=EXIT,f=File]==>")).upper() try: if sernum == '0': sys.exit() if sernum == 'F': sernum = raw_input("Enter file path[eg.'/usr/auto/a.txt']==>") outStr = "File Path:[%s]"% sernum print UseStyle(outStr, fore='blue') break if self.sn_sample.match(sernum): outStr = "Scanned sernum[%s]"% sernum print UseStyle(outStr, fore='blue') break else: print "Scanned wrong format-->%s"% sernum except ValueError: print "Scaned wrong format" continue return sernum
def __init__(self): #Compile the agen file #orgPath = os.path.abspath('.') #os.chdir('/home/alexyi/py/getlog') #Change to genparent.agen path #For other user using this script, change to genparent.agen path os.chdir('/home/alexyi/CMN/getlog') p = subprocess.Popen(['/usr/auto/bin/agencmp', 'genparent'], stdout=subprocess.PIPE) output, error = p.communicate() if p.returncode != 0: outStr = "genparent.agen complie fail" print UseStyle(outStr, fore='black', back='yellow')
def getOutput(self,sernum): #Already do snpull in getPCSN.py #p = subprocess.Popen(['agen.exe','snpull','MAN~',sernum], stdout=subprocess.PIPE) #output, error = p.communicate() #if p.returncode != 0: # outStr = "snpull failed" # print UseStyle(outStr, fore='black', back='yellow') p1 = subprocess.Popen(['/usr/auto/bin/snfind', sernum, ' 0'], stdout=subprocess.PIPE) output = p1.communicate()[0] if p1.returncode != 0: outStr = "####snfind failed, ending the routine....\n" print UseStyle(outStr, mode='bold', fore='black', back='red') EXIT() return output
def asklogType(self): self.displayItem(self.logtype) while True: logType = raw_input("Enter find log type[0=EXIT]==>") try: if logType == '0': sys.exit() if self.logtype.get(logType): outStr = "Scanned area[%s]"% self.logtype.get(logType) print UseStyle(outStr, fore='blue') break else: print "Selected wrong option-->%s"% logType except ValueError: print "Input wrong option" continue return self.logtype.get(logType)
def askArea(self): self.displayItem(self.logarea) while True: area = raw_input("Enter AREA[0=EXIT]==>") try: if area == '0': sys.exit() if self.logarea.get(area): outStr = "Scanned area[%s]"% self.logarea.get(area) print UseStyle(outStr, fore='blue') break else: print "Selected wrong option-->%s"% area except ValueError: print "Input wrong option" continue return self.logarea.get(area)
if len(childFolderList) == 1: break finally: pass #ftp.quit() else: #Run after for finished ftp.quit() if len(logList) >0: mymail = MailVerify() mailResult = () mailResult = mymail.mailVf() zfClass = zipFile() zipOriFileName = zfClass.getFilename(logList) zfClass.extraFile(logList) if mailResult[0] == "N" or len(snlist) > 1: #Avoid hurge attachment, option f=File will not send mail outStr = "----------------------------------------------------------------------------" print UseStyle(outStr, fore='blue') outStr = "--->The log saved in local folder:%s, file name as below" % os.getcwd() print UseStyle(outStr, fore='blue') for aFile in zipOriFileName: outStr = "|| {0}".format(aFile) print UseStyle(outStr, fore='blue') outStr = "----------------------------------------------------------------------------" print UseStyle(outStr, fore='blue') else: if "Linux" in platform.system(): #Only send mail in linux outStr = "----------------------------------------------------------------------------" print UseStyle(outStr, fore='blue') mailAdd = mailResult[1] mailAttachment = zfClass.zipFileList(zipOriFileName) mailSubject = "{0} TestLog".format(snlist[0]) pp = subprocess.Popen("echo 'TestLog' | mutt -s '%s' -a %s %s"%(mailSubject,mailAttachment,mailAdd),\