python 文件对比并发送邮件

#!/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 smtplib
from 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()

猜你喜欢

转载自www.cnblogs.com/jianeng/p/9333499.html