#!/usr/local/python3#-*-coding:utf-8-*-#@Date: 2018-04-15 09:00:00#@Author: Canon#@Link: https://www.python.org#@Version: 3.6.1"""python sys.argv[0] sys.argv[1]sys.argv[0]: script path sys.argv[1]: project name Execution Script example: Python conf_warn.py project name"""ImportOSImportSYSImportDifflibImportSmtplib fromEmail.mime.textImportMimetext#Project NameProgram_name = sys.argv[1].lower ()#Project CollectionProj_dict = {}#----------Send message-related parameters------#Outgoing ServerSmtp_server ="smtp.exmail.qq.com"#PortPort = 465#Account NumberSender ="[email protected]"#PasswordPSW ="Bjf792sfx4fyjego"#Receiving PersonReceiver = ["[email protected]","[email protected]"]#----------Check if there is a configuration for the following file, and if so, send the message------#Project Check FileProj_as = ['Web. XML','Ehcache.xml','Log4j.xml','Struts.xml']#All ProjectsProj_list = [ { "Proj_name":"Project Name", "Patch_file":"Mirrored package Path", "Target_path": [Target Path 1,Target Path 2], "Conf_file": Proj_as,"Compare_path":""}] forIteminchproj_list:#clear the files in the comparison directoryOs.system ("RM-RF {0}/*". Format (item["Compare_path"])) #Get project Information ifitem["Proj_name"].lower () = =program_name:proj_dict=Item#Unzip the image package fileOs.system ("tar-zxf {0}-c {1}". Format ("'"+proj_dict["Patch_file"]+"'", proj_dict["Compare_path"]))#get all the files under the comparison pathCompare_dir = {} forRoot, dirs, filesinchOs.walk (proj_dict["Compare_path"], topdown=False): forNameinchFiles:ifNameinchproj_dict["Conf_file"]: Print(name) File_path=Os.path.join (root, name) Compare_dir[name]=File_path#List of profiles present in the mirrored packageConf_list = [key forKeyinchcompare_dir]compare_dict= {}ifconf_list:#get all the files under the project pathInit_dir = {} forRoot, dirs, filesinchOs.walk (proj_dict["Target_path"][0], topdown=False): forNameinchFiles:ifNameinchConf_list:file_path=Os.path.join (root, name) Init_dir[name]=File_path#configuration file Differences comparisonHD =Difflib. Htmldiff () Init_strs="'with open (Init_dir[name],'R') as Init:init_strs=init.readlines () init.close () Compare_strs="'with open (Compare_dir[name],'R') as Compare:compare_strs=compare.readlines () compare.close () content=hd.make_file (init_strs,compare_strs) compare_dict[name]=Contentemail_body=" "<div> Image Update package The following files appear, requiring testers to manually check:</div> <div> project name: {0}</div> <div> configuration file: {1}&L T;/div>" ". Format (proj_dict["Proj_name"],", ". Join (conf_list)) forHtml_keyinchCompare_dict:email_body= Email_body +"". Format (Html_key, Compare_dict[html_key])#clear the files in the comparison directory forIteminchProj_list:os.system ("RM-RF {0}/*". Format (item["Compare_path"]))#----------Edit Message content------#Message ContentBODY =Email_body#define message body as HTML formatmsg = Mimetext (Body,"HTML","Utf-8")#Sending Personmsg[' from'] =Sender#Receiving Personmsg[' to'] =";". Join (receiver)#message HeaderSubject ="mirroring environment, profile checking"msg['subject'] =subject#----------Send a message------ifconf_list:Try: SMTP=Smtplib. SMTP ()#connecting to a serverSmtp.connect (smtp_server)#Logon Serversmtp.login (sender, PSW)except: #connecting to a serverSMTP =Smtplib. Smtp_ssl (SmtpServer, Port)#Logon Serversmtp.login (sender, PSW)#Send mailSmtp.sendmail (sender, receiver, msg.as_string ())#shutting down the serverSmtp.quit ()
Python files compare and send messages