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

Step1:分享背景

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

 

Step2:Python代码实现

  1 import os
  2 from email.mime.multipart import MIMEMultipart
  3 from email.mime.text import MIMEText
  4 import smtplib
  5  
  6 #指定java工程目录
  7 dirpath="D:\java_project\entitystorage"
  8 emails='loadkernel@126.com'
  9  
 10 alldict={}
 11  
 12 #获取所有文件里的key and value
 13 def get_all_property(dirpath,env):
 14     for root, dirs, files in os.walk(dirpath, topdown=False):
 15         for fn in files:
 16             fullpathfile=os.path.join(root, fn)
 17             if(env in fullpathfile and "properties" in fullpathfile):
 18                 with open(fullpathfile,'r',encoding='utf-8') as ff:
 19                     print(ff)
 20                     for line in ff.readlines():
 21                         print(fullpathfile,line)
 22                         if(not line.startswith("#")):
 23                             line=line.replace("\n","")
 24                             if(len(line)>1):
 25                                 if("=" in line):
 26                                     kv=line.split("=")
 27                                     get_all_properties(env, kv[0], kv[1])
 28                                 else:
 29                                     print("the "+str(fullpathfile) +"has an error!!!")
 30  
 31 #将获取的key and value 内容放入字典
 32 def get_all_properties(env,k,v):
 33     if(env not in alldict.keys()):
 34         alldict[env] = {}
 35         alldict[env][k]=v
 36     else:
 37         alldict[env][k]=v
 38  
 39 #在页面上展示不同环境的key 对应的值
 40 def show_data_to_page(betadict,stagingdict,uatdict,proddict):
 41     count=0
 42     str1 = '<!DOCTYPE html> <html> <meta charset="utf-8"> <head> <style type="text/css"> 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; } </style> </head>';
 43     str1 = str1 + '<body>'
 44     str1 = str1 + ' <table class="gridtable">'
 45     str1 = str1 + ' <tr> <th>key</th> <th>beta</th> <th>staging</th> <th>uat</th> <th>prod</th> <tr/> '
 46  
 47     sorted_prod=sorted(proddict['prod'].items(), key=lambda d: d[0])
 48     for pk,pv in sorted_prod:
 49         count+=1
 50         print(count,pk,pv)
 51         if(pk in betadict['betamagic'].keys()):
 52             try:
 53                 if(len(betadict['betamagic'][pk])>0):
 54                     bv=betadict['betamagic'][pk]
 55                     if(str(pv).lower()==str(bv).lower()):
 56                         bv="<p style='color:blue'><B>"+bv+"</B>"
 57                 else:
 58                     bv = "None"
 59             except:
 60                 bv="None"
 61         else:
 62             bv="None"
 63  
 64         if (pk in stagingdict['staging'].keys()):
 65             try:
 66                 if (len(stagingdict['staging'][pk]) > 0):
 67                     sv = stagingdict['staging'][pk]
 68                     if (str(pv).lower() == str(sv).lower()):
 69                         sv = "<p style='color:blue'><B>" + sv + "</B>"
 70                 else:
 71                     sv = "None"
 72             except:
 73                 sv = "None"
 74         else:
 75             sv = "None"
 76  
 77         if (pk in uatdict['uat'].keys()):
 78             try:
 79                 if (len(uatdict['uat'][pk]) > 0):
 80                     uv = uatdict['uat'][pk]
 81                     if (str(pv).lower() == str(uv).lower()):
 82                         uv = "<p style='color:blue'><B>" + uv + "</B>"
 83                 else:
 84                     uv = "None"
 85             except:
 86                 uv = "None"
 87         else:
 88             uv = "None"
 89  
 90         str1+='<tr> <td>' + str(count)+" . "+ pk + '</td> <td>' + bv + '</td> <td>' + sv + '</td> <td>' + uv + '</td> <td>' + pv + '</td> <tr/>'
 91  
 92     str1 = str1 + ' </table>'
 93     str1 = str1 + '</body> </html>'
 94     str1=str1.replace("None","<p style='color:green'><B>None</B>")
 95  
 96     send_mail(emails, 'diff the properties', str1)
 97  
 98 def send_mail(receivers, title, content):
 99     sender = 'qa.notice@126.com'
100     mailto = receivers.split(",")
101     try:
102         msg = MIMEMultipart()
103         msg['Subject'] = title
104         to_user = ",".join(mailto)
105  
106         print("receivers...", to_user)
107         msg['to'] = to_user
108         msg['From'] = sender
109  
110         body = MIMEText(content, _subtype='html', _charset='utf-8')
111         msg.attach(body)
112         smtp = smtplib.SMTP('smtp.officexxx.com', 587)
113         smtp.starttls()
114         print("sending")
115         smtp.login("qa.notice@126.com", "testkyjrsdxz")
116         smtp.sendmail(sender, mailto, msg.as_string())
117         print("send")
118         smtp.quit()
119     except smtplib.SMTPException as e:
120         print(e)
121  
122 if __name__ == '__main__':
123     get_all_property(dirpath,"betamagic")
124     betadict=alldict
125     print(betadict)
126     alldict={}
127     print("***")
128     get_all_property(dirpath,"staging")
129     stagingdict = alldict
130     print(stagingdict)
131     alldict = {}
132     get_all_property(dirpath,"uat")
133     uatdict = alldict
134     print(uatdict)
135     alldict = {}
136     get_all_property(dirpath,"prod")
137     proddict = alldict
138     print(proddict)
139  
140     show_data_to_page(betadict,stagingdict,uatdict,proddict)

 

Step3:邮件效果图展示

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

 

 

备注说明:

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

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

 

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

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

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


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

 

上一篇:数据结构与算法(十六)


下一篇:力扣242 有效的字母异位词