python 实例

#!/usr/bin/python
#coding=gbk
import re
import sys
import subprocess
import os
class Mytool:
        #根据文件名读取文件内容
        def getFileContent(self,filepath):
                if(os.path.exists(filepath)==False):
                        return -1
                else:
                        content=''
                        #print filepath
                        f=file(filepath,'r')
                        try:
                                while True:
                                        line=f.readline()
                                        content=content+line
                                        if len(line)==0:
                                                break
                        finally:                #print line
                                f.close()
                        #print 'line',line
                        return content

        #根据提供的字符串内容及正则返回需要截取的单个结果
        def getRexReturn(self,rex,content):

                p=re.compile(rex)
                iter=p.finditer(content)
                for m in iter:
                        res=m.group()
                        #print res
                return res
         #根据提供的字符串内容及正则返回需要截取的多个结果
        def getRexReturnList(self,rex,content):
                res=[]
                p=re.compile(rex)
                iter=p.finditer(content)
                for m in iter:
                        res.append(m.group())
                        #print m.group()
                #print res
                return res

        #获得部署说明获取作者UM帐户
        def getWriteUM(self,content):
                #p=re.compile(r'(作者\s([\S]+})\s)')
                #p=re.compile(r'((?:作者\s+)(\S)+)')
                p=re.compile(r'((?<=作者  )(\S)+(?=\s))')
                iter=p.finditer(content)
                writerList=[]
                for m in iter:
                        #print "m" ,m.group(1)
                        writerList.append(m.group(1))
                print writerList
                return writerList

        def getWriteUMEmailList(self,content):
                emailList=''
                p=re.compile(r'((?<=作者  )(\S)+(?=\s))')
                emailList=p.findall(content)
                print emailList


        #根据提供的文件名返回版本信息
        def getRdinfo(self,rdinfo):
                rdinfocontent=''
                content=self.getFileContent(rdinfo)
                print content
                if (content==-1):
                        #/wls/deployop/versions/storage/isv2_db_2.15.0_standard/dist_stg_autod_1
                        path=os.getcwd()
                        rex='(?<=storage/)(.*?)(?=_standard)'
                        p=re.compile(rex)
                        returnlist=p.findall(path)
                        rdinfocontent=returnlist[0].upper()
                        #print rdinfocontent
                else:
                        titlerex=r'((?<=headline=)(\S)+)'
                        title=self.getRexReturn(titlerex,content)

                        rdrex=r'((?<=rdid=)(\S)+)'
                        rd=self.getRexReturn(rdrex,content)

                        testenv2rex=r'((?<=testenv2=)(\S)+)'
                        testenv2=self.getRexReturn(testenv2rex,content)

                        developerrex=r'((?<=developer=)(\S)+)'
                        developer=self.getRexReturn(developerrex,content)

                        rddaterex=r'((?<=rddate=)(\S)+)'
                        rddate=self.getRexReturn(rddaterex,content)

                        prd_planbegindaterex=r'((?<=prd_planbegindate=)(\S)+)'
                        prd_planbegindate=self.getRexReturn(prd_planbegindaterex,content)

                        pwdpath=os.getcwd()
                        rdinfocontent="===========版本信息beigin==================================\n\n"
                        rdinfocontent=rdinfocontent+rd+' '+title+'\n'+'部署环境'+testenv2+'\n'+'开发人员:'+developer+'\n'
                        rdinfocontent=rdinfocontent+'版本移交时间:'+rddate+'\n'+'生产计划发布时间:'+prd_planbegindate+'\n\n'+pwdpath
                        rdinfocontent=rdinfocontent+"\n==========版本信息end======================================="
                return rdinfocontent


        def sendmail(self,sender,receiver,subject,message):

                '''
                dsendmail --help
                Usage: dsendmail -s SUBJECT {-m MESSAGE|-f FILE} --to RECIPCIENTS [OPTION]...
                -s, --subject      指定主题
                -m, --message      指定邮件内容
                -f, --file         从文件中读取邮件内容
                --to           指定收件人,以逗号分隔,下同
                --cc           指定抄送址
                --bcc          
                -a, --attach       指定附件位置,以逗号分隔
                -c, --content-type text/html或text/plain,缺省时根据内容判断
                --from         发件人,
                --smtp         smtp服务器地址,默认为
                --help         显示本信息
                '''
                cmdargs='/wls/deploysys/tools/dsendmail '
                cmdargs=cmdargs+' -s ' +'"'+subject+'"' +' -m '+'"'+ message +'"'+ ' --from ' +'" '+ sender +' "'+ ' --to '+'" '+receiver+' "'
                print cmdargs
                cmdargs=cmdargs+' -a '+' -c '+ 'text/plain'
                print cmdargs
                p=subprocess.call(cmdargs,shell=True)
                if(p==0):
                        print 'sendmail success'
                else:
                        print 'sendmail fail'
        def getDcheckContent(self):
                cmdargs='deploytool dcheck'
                dcheckContent='本次执行结果汇总如下,详见日志\n\n'
                dcheckContent+=subprocess.Popen(cmdargs,shell=True,stdout=subprocess.PIPE).communicate()[0]
                #print dcheckContent
                #dcheckContent=subprocess.call(cmdargs,shell=True)
                where=dcheckContent.find('log个数')
                dcheckContent=dcheckContent[where+12:]
                dcheckContent+="\n 如下为对应的日志\n"
                dcheckContent=dcheckContent.replace('[1m','')
                dcheckContent=dcheckContent.replace('[0m','')
                dcheckContent=dcheckContent.replace('[32m','')
                dcheckContent=dcheckContent.replace('[31m','')
                dcheckContent=dcheckContent.replace('[33m','')
                dcheckContent=dcheckContent.replace('[ ','')
                dcheckContent=dcheckContent.replace('>>','')
                return dcheckContent
        #dcheck内容分析         
        def getErrorSqlLog(self):
                #s=self.getFileContent('ccc.txt')
                s=self.getDcheckContent()
                errorlogContent=''
                str1='.sql'
                str1sqlList =[]
                if(str1 in s):
                        #1: ./dml/gbs/sr_pa02461797_74_gbspkg_dml_guozhengmin961_gbs.sql
                        str1rex=r'([\S]+sql)'
                        str1sqlList=self.getRexReturnList(str1rex,s)
                        #print str1 


                str2='.run'
                str2sqlList=[]
                if(str2 in s):
                        str2rex=r'([\S]+run)'
                        str2sqlList=self.getRexReturnList(str2rex,s)
                        #print str2 +':' 
                        #print str2sqlList  
                        str2Alllogcontent=''
                        for f in str2sqlList:
                                logcontent='==========================================================\n'
                                logcontent=logcontent+f+'日志如下\n'
                                logcontent=logcontent+'==========================================================\n'
                                logcontent=logcontent+self.getFileContent(f)
                                str2Alllogcontent=str2Alllogcontent+"正在执行或因故处于执行状态的脚本日志汇总如下:\n\n"+logcontent
                        #print str2Alllogcontent
                        errorlogContent=errorlogContent+str2Alllogcontent

                str3='.fail'
                str3sqlList=[]
                if (str3 in s):
                        str3rex=r'([\S]+fail)'
                        str3sqlList=self.getRexReturnList(str3rex,s)
                        #print str3+':'
                        str3Alllogcontent=''
                        #print str3sqlList
                        for f in str3sqlList:
                                logcontent='==========================================================\n'
                                logcontent=logcontent+f+'日志如下\n'
                                logcontent=logcontent+'==========================================================\n'
                                logcontent=logcontent+self.getFileContent(f)
                                str3Alllogcontent=str3Alllogcontent+logcontent
                        #print str3Alllogcontent        
                        errorlogContent=errorlogContent+"执行失败的脚本如下:\n\n"+str3Alllogcontent

                str4='.suc'
                str4sqlList=[]
                if (str4 in s):
                        str4rex=r'([\S]+suc)'
                        str4sqlList=self.getRexReturnList(str4rex,s)
                        #print str4+':'
                        str4Alllogcontent=''
                        for f in str4sqlList:
                                logcontent='==========================================================\n'
                                logcontent=logcontent+f+'日志如下\n'
                                logcontent=logcontent+'==========================================================\n'
                                logcontent=logcontent+self.getFileContent(f)
                                str4Alllogcontent=str4Alllogcontent+"有执行成功但报错的脚本汇总日志如下 :\n\n"+logcontent
                        errorlogContent+=str4Alllogcontent
                #print errorlogContent
                return errorlogContent




mytool=Mytool()
print mytool.getFileContent('rdinfos');
#rdinfos=mytool.getRdinfo('rdInfo.txt')
dcheckContent=mytool.getDcheckContent()
ErrorSqlLog=mytool.getErrorSqlLog()
#msg=rdinfos+'\n'+ErrorSqlLog
#content=mytool.getFileContent("egis.html")
#titlerex=r'((?<=headline=)(\S)+)'
#print writer.getRexReturn(titlerex,'headline=第1次移交--常规版本 EIM-RSTP-DB1.0.0')
#print "content:",content
#print mytool.getWriteUM(content)
rdinfos=mytool.getRdinfo('rdInfo.txt')
print rdinfos
msg="'"+rdinfos+"\n"+dcheckContent+ErrorSqlLog+"'"
mytool.sendmail('***@163.com',***@163.com','sendmail test',msg)
#print mytool.getDcheckContent()
#print msg
                                  

你可能感兴趣的:(python 实例)