標籤:檔案中 伺服器 send os.path sha 發送 localtime log form
#!/usr/bin/python#_*_ coding:utf-8 _*_import timefrom email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBasefrom email import Utils, Encodersfrom email.header import Headerimport mimetypesimport sysimport smtplibimport getoptimport osimport reimport shutilimport time, datetime‘‘‘指令碼功能: (1)檢查所有的builting_setting.h是否能夠編譯通過,並將編譯結果寫入 buildLog檔案中。 (2)將編譯結果通過郵箱發送給負責人。 (3)系統定期執行任務,檢查build_setting。 注意:SRC的絕對路徑更新:gPathSrc‘‘‘gPathSrc = u"/home/kk/share/bak/512_new/code"gPathBuildList = gPathSrc + u"/kitking/buildList.h"gPathBuildLog = gPathSrc + u"/kitking/buildLog.h"def SearchbinFile(curBuildName): global gPathSrc binFilepath = gPathSrc + "/mergedir" IsFindfile = 0 for filename in os.listdir(binFilepath): if (re.search(".*RR.*\.bin.*",filename)): IsFindfile = 1 break else: IsFindfile = 0 if IsFindfile == 0: if (os.path.isfile(gPathBuildList)): open(gPathBuildLog, "a").writelines(curBuildName) else: open(gPathBuildLog, "w").writelines(curBuildName)def Compile(): buildList = [] strCurBuildName = "" #查看資訊清單檔 if(os.path.isfile(gPathBuildList)): buildList = open(gPathBuildList, "r").readlines() #無清單任務,需要重新產生 if(len(buildList) == 0): for dirPath, dirNames, fileNames in os.walk(gPathSrc + "/../BUILD_RECORD"): for strCurBuildName in dirNames: buildList.append(strCurBuildName+"\n") #每次移除一個任務後,更新資訊清單檔 if(len(buildList)): strCurBuildName = buildList.pop() open(gPathSrc + "/../BUILD_RECORD/BUILD_NOW.h", "w").write("#define BUILD_NAME " + strCurBuildName) os.system(gPathSrc + "/build_history.sh") SearchbinFile(strCurBuildName) if (len(buildList)): open(gPathBuildList, "w").writelines(buildList) else: # 注意:附件的路徑字串應為unicode編碼 # 寄件者郵箱 接收者郵箱 郵箱密碼 主題 內容 附件名 SendMail(‘[email protected]‘, ‘[email protected]‘, ‘xxx‘,‘編譯結果‘,‘請查看Log!‘,gPathBuildLog) os.remove(gPathBuildLog) os.remove(gPathBuildList)def SendMail(fromAddress, toAddress, usepassword,subject=None, content=None, attfile=None, subtype=‘plain‘, charset=‘utf-8‘): username = fromAddress #建立一個帶附件的執行個體 msg = MIMEMultipart() msg[‘From‘] = fromAddress msg[‘To‘] = toAddress if subject: #標題 msg[‘Subject‘] = subject msg[‘Date‘] = Utils.formatdate(localtime=1) if content: #添加郵件內容 txt = MIMEText(content, subtype, charset) msg.attach(txt) if attfile: #構造附件 #注意:傳入的參數attfile為unicode,否則帶中文的目錄或名稱的檔案讀不出來 # basename 為檔案名稱,由於傳入的參數attfile為unicode編碼,此處的basename也為unicode編碼 basename = os.path.basename(attfile) #print basename #注意:指定att的編碼方式為gb2312 att = MIMEText(open(attfile, ‘rb‘).read(), ‘base64‘, ‘gb2312‘) att["Content-Type"] = ‘application/octet-stream‘ #注意:此處basename要轉換為gb2312編碼,否則中文會有亂碼。 # 特別,此處的basename為unicode編碼,所以可以用basename.encode(‘gb2312‘) # 如果basename為utf-8編碼,要用basename.decode(‘utf-8‘).encode(‘gb2312‘) att["Content-Disposition"] = ‘attachment; filename=%s‘ % basename.encode(‘gb2312‘) msg.attach(att) try: #smtp = smtplib smtp = smtplib.SMTP() #串連伺服器 smtp.connect(‘smtp.163.com‘, ‘25‘) #登入 smtp.login(username, usepassword) #發送郵件 smtp.sendmail(fromAddress, toAddress, msg.as_string()) #退出 smtp.quit() print(‘郵件發送成功email has send out !‘) except Exception as e: print str(e) if __name__ == "__main__": #擷取指令碼所在的絕對路徑 AbsolutePath = sys.path[0] #切換目錄進行編譯 os.chdir(gPathSrc) #編譯 Compile() #sys.exit(1)
Python 定期檢查Build_setting的編譯情況