栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 其他

不同环境相同配置项的内容如何diff差异?

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

不同环境相同配置项的内容如何diff差异?

Step1:分享背景

在工作中,我们用到的环境经常会有很多套,由于不同的环境使用的配置是不一样的,那么怎么能够对比所有不同环境的相同配置项各是什么内容呢?

Step2:Python代码实现

import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

#指定java工程目录
dirpath="D:java_projectentitystorage"
emails='loadkernel@126.com'

alldict={}

#获取所有文件里的key and value
def get_all_property(dirpath,env):
    for root, dirs, files in os.walk(dirpath, topdown=False):
        for fn in files:
            fullpathfile=os.path.join(root, fn)
            if(env in fullpathfile and "properties" in fullpathfile):
                with open(fullpathfile,'r',encoding='utf-8') as ff:
                    print(ff)
                    for line in ff.readlines():
                        print(fullpathfile,line)
                        if(not line.startswith("#")):
                            line=line.replace("n","")
                            if(len(line)>1):
                                if("=" in line):
                                    kv=line.split("=")
                                    get_all_properties(env, kv[0], kv[1])
                                else:
                                    print("the "+str(fullpathfile) +"has an error!!!")

#将获取的key and value 内容放入字典
def get_all_properties(env,k,v):
    if(env not in alldict.keys()):
        alldict[env] = {}
        alldict[env][k]=v
    else:
        alldict[env][k]=v

#在页面上展示不同环境的key 对应的值
def show_data_to_page(betadict,stagingdict,uatdict,proddict):
    count=0
    str1 = '     table.gridtable {font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #666666; border-collapse: collapse; } table.gridtable th {border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; } table.gridtable td {border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; }  ';
    str1 = str1 + ''
    str1 = str1 + ' '
    str1 = str1 + '  key beta staging uat prod  '

    sorted_prod=sorted(proddict['prod'].items(), key=lambda d: d[0])
    for pk,pv in sorted_prod:
        count+=1
        print(count,pk,pv)
        if(pk in betadict['betamagic'].keys()):
            try:
                if(len(betadict['betamagic'][pk])>0):
                    bv=betadict['betamagic'][pk]
                    if(str(pv).lower()==str(bv).lower()):
                        bv=""+bv+""
                else:
                    bv = "None"
            except:
                bv="None"
        else:
            bv="None"

        if (pk in stagingdict['staging'].keys()):
            try:
                if (len(stagingdict['staging'][pk]) > 0):
                    sv = stagingdict['staging'][pk]
                    if (str(pv).lower() == str(sv).lower()):
                        sv = "" + sv + ""
                else:
                    sv = "None"
            except:
                sv = "None"
        else:
            sv = "None"

        if (pk in uatdict['uat'].keys()):
            try:
                if (len(uatdict['uat'][pk]) > 0):
                    uv = uatdict['uat'][pk]
                    if (str(pv).lower() == str(uv).lower()):
                        uv = "" + uv + ""
                else:
                    uv = "None"
            except:
                uv = "None"
        else:
            uv = "None"

        str1+=' ' + str(count)+" . "+ pk + ' ' + bv + ' ' + sv + ' ' + uv + ' ' + pv + ' '

    str1 = str1 + ' '
    str1 = str1 + ' '
    str1=str1.replace("None","None")

    send_mail(emails, 'diff the properties', str1)

def send_mail(receivers, title, content):
    sender = 'qa.notice@126.com'
    mailto = receivers.split(",")
    try:
        msg = MIMEMultipart()
        msg['Subject'] = title
        to_user = ",".join(mailto)

        print("receivers...", to_user)
        msg['to'] = to_user
        msg['From'] = sender

        body = MIMEText(content, _subtype='html', _charset='utf-8')
        msg.attach(body)
        smtp = smtplib.SMTP('smtp.officexxx.com', 587)
        smtp.starttls()
        print("sending")
        smtp.login("qa.notice@126.com", "testkyjrsdxz")
        smtp.sendmail(sender, mailto, msg.as_string())
        print("send")
        smtp.quit()
    except smtplib.SMTPException as e:
        print(e)

if __name__ == '__main__':
    get_all_property(dirpath,"betamagic")
    betadict=alldict
    print(betadict)
    alldict={}
    print("***")
    get_all_property(dirpath,"staging")
    stagingdict = alldict
    print(stagingdict)
    alldict = {}
    get_all_property(dirpath,"uat")
    uatdict = alldict
    print(uatdict)
    alldict = {}
    get_all_property(dirpath,"prod")
    proddict = alldict
    print(proddict)

    show_data_to_page(betadict,stagingdict,uatdict,proddict)

Step3:邮件效果图展示

备注说明:

1. 图中红色表示live环境有对应项,但是其他环境没有的。

2. 图中蓝色表示非live环境与live配置相同的。

欢迎关注【无量测试之道】公众号,回复【领取资源】
Python编程学习资源干货、
Python+Appium框架APP的UI自动化、
Python+Selenium框架Web的UI自动化、
Python+Unittest框架API自动化、
资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。

备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:


添加关注,让我们一起共同成长! 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/279597.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号