python 文件對比並發送郵件
阿新 • • 發佈:2018-07-19
compare os.walk 服務器 odi 3.6 with read org pre
#!/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]: 腳本路徑 sys.argv[1]: 項目名稱 執行腳本示例: python conf_warn.py 項目名稱 """ import os import sys import difflib import smtplibfrom email.mime.text import MIMEText # 項目名稱 program_name = sys.argv[1].lower() # 項目集合 proj_dict = {} # ---------- 發送郵件相關參數------ # 發件服務器 smtp_server = "smtp.exmail.qq.com" # 端口 port = 465 # 賬號 sender = "[email protected]" # 密碼 psw = "BjF792SFx4FyJEgo" # 接收人 receiver = ["[email protected]", "[email protected]"] # ---------- 檢查是否存在配置以下文件, 若存在, 則發送郵件 ------ # 項目檢查文件 proj_as = [‘web.xml‘, ‘ehcache.xml‘, ‘log4j.xml‘, ‘struts.xml‘]# 所有項目 proj_list = [ { "proj_name": "項目名稱", "patch_file": "鏡像包路徑", "target_path": [目標路徑1, 目標路徑2], "conf_file": proj_as, "compare_path": ""} ] for item in proj_list: # 清除對比目錄下的文件 os.system("rm -rf {0}/*".format(item["compare_path"])) # 獲取項目信息 if item["proj_name"].lower() == program_name: proj_dict = item # 解壓鏡像包文件 os.system("tar -zxf {0} -C {1}".format("‘"+proj_dict["patch_file"]+"‘", proj_dict["compare_path"])) # 獲取對比路徑下所有的文件 compare_dir = {} for root, dirs, files in os.walk(proj_dict["compare_path"], topdown=False): for name in files: if name in proj_dict["conf_file"]: print(name) file_path = os.path.join(root, name) compare_dir[name] = file_path # 鏡像包中存在的配置文件列表 conf_list = [key for key in compare_dir] compare_dict = {} if conf_list: # 獲取項目路徑下所有的文件 init_dir = {} for root, dirs, files in os.walk(proj_dict["target_path"][0], topdown=False): for name in files: if name in conf_list: file_path = os.path.join(root, name) init_dir[name] = file_path # 配置文件差異對比 hd = 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] = content email_body = ‘‘‘<div>鏡像更新包出現以下文件,需要測試人員進行人工檢查:</div> <div>項目名稱: {0}</div> <div>配置文件: {1}</div>‘‘‘.format(proj_dict["proj_name"], ", ".join(conf_list)) for html_key in compare_dict: email_body = email_body + "<h1>{0}</h1><span>{1}</span>".format(html_key, compare_dict[html_key]) # 清除對比目錄下的文件 for item in proj_list: os.system("rm -rf {0}/*".format(item["compare_path"])) # ---------- 編輯郵件內容 ------ # 郵件內容 body = email_body # 定義郵件正文為 html 格式 msg = MIMEText(body, "html", "utf-8") # 發送人 msg[‘from‘] = sender # 接收人 msg[‘to‘] = ";".join(receiver) # 郵件標題 subject = "鏡像環境, 配置文件檢查" msg[‘subject‘] = subject # ---------- 發送郵件 ------ if conf_list: try: smtp = smtplib.SMTP() # 連接服務器 smtp.connect(smtp_server) # 登錄服務器 smtp.login(sender, psw) except: # 連接服務器 smtp = smtplib.SMTP_SSL(smtpserver, port) # 登錄服務器 smtp.login(sender, psw) # 發送郵件 smtp.sendmail(sender, receiver, msg.as_string()) # 關閉服務器 smtp.quit()
python 文件對比並發送郵件