一、准备数据,如图
二、分析不同版本csv文件数据的差异,生成报告
# 比较不同时间的2个版本服务的接口是否有变化
import os
import csv
import time
os.chdir(os.path.dirname(__file__)) # 执行目录跳转到本目录
csv_path = '../csv' # 存放 swagger csv
current_time = time.strftime('%Y%m%d%H%M%S', time.localtime()) # 格式化当前时间
def get_csv_list():
"""获取csv目录下的文件列表"""
csv_list = []
for path in os.listdir(csv_path):
name, suf = os.path.splitext(path)
# 筛选出csv文件
if suf == '.csv':
csv_list.append(path)
# print(csv_list)
return csv_list
def parse_list(csv_list):
"""把同一服务的不同时间的csv文件放在一个小列表里"""
csv_list_sort = []
csv_list_set = set(map(lambda x: x[:4], csv_list))
# print(csv_list_set)
for a in csv_list_set:
a_list = []
for b in csv_list:
if b[:4] == a:
a_list.append(b)
csv_list_sort.append(a_list)
# print(csv_list_sort)
return csv_list_sort
def read_csv_file(csv_name):
"""读csv文件,返回一个列表"""
csv_list = []
csvfile_path = os.path.join(csv_path, csv_name)
with open(csvfile_path) as f:
reader = csv.reader(f)
for x in reader:
csv_list.append(x)
return csv_list
def diff_file(file1, file2):
"""比较2个文件的不同,读文件,生成列表,变成集合,求并集,分别减并集就是结果"""
list1 = read_csv_file(file1)
# print(list1)
list2 = read_csv_file(file2)
# 将列表转化为集合
set1 = set([str(x) for x in list1])
# print(set1)
set2 = set([str(x) for x in list2])
# set_more_1 = ()
# set_more_2 = ()
# 集合运算
set_1_2 = set1 & set2
set_more_1 = set1 - set_1_2
list_more_1 = [eval(x) for x in set_more_1]
# print(list_more_1)
set_more_2 = set2 - set_1_2
list_more_2 = [eval(x) for x in set_more_2]
f = open('../report/接口更改检测报告-{}.txt'.format(current_time), 'a', encoding='utf-8')
if not list_more_1 and not list_more_2 :
print("{}接口没有更改".format(file1))
f.write("{}接口没有更改\n".format(file1))
if list_more_1 or list_more_2:
print('#' * 100)
f.write('#' * 100 + '\n')
print('服务 {} 的接口有更改'.format(file1.split('-')[0]))
f.write('服务 {} 的接口有更改'.format(file1.split('-')[0]) + '\n')
print('*'*25, '标准版', '*'*25)
f.write('*'*25 + '标准版' + '*'*25 + '\n')
print(file1)
f.write(file1 + '\n')
str_list = ['地址', '方法', '注释', '参数'] # 1245
num1 = 1
for x1 in list_more_1:
if len(x1) == 4:
x1.append('没参数')
x1_list = [x1[1], x1[2], x1[4], x1[5]]
print('-'*20, num1, '-'*20)
f.write('-'*20 + str(num1) + '-'*20 + '\n')
for index in range(4):
print('{:<25}: {:<10}'.format(str_list[index], x1_list[index]))
f.write('{:<25}: {:<10}'.format(str_list[index], x1_list[index]) + '\n')
num1 += 1
print('*'*25, '更改后', '*'*25)
f.write('*'*25 + '更改后' + '*'*25 + '\n')
print(file2)
f.write(file2 + '\n')
num2 = 1
for x2 in list_more_2:
if len(x2) == 4:
x2.append('没参数')
print('-'*20, num2, '-'*20)
f.write('-'*20 + str(num2) + '-'*20 + '\n')
x2_list = [x2[1], x2[2], x2[4], x2[5]]
for index in range(4):
print('{:<25}: {:<10}'.format(str_list[index], x2_list[index]))
f.write('{:<25}: {:<10}'.format(str_list[index], x2_list[index]) + '\n')
num2 += 1
f.close()
def diff_file2(file1, file2):
"""比较2个文件的不同,读文件,生成列表,变成集合,求并集,分别减并集就是结果"""
list1 = read_csv_file(file1)
list2 = read_csv_file(file2)
# 将列表转化为集合
set1 = set([str(x) for x in list1])
# print(set1)
set2 = set([str(x) for x in list2])
# 集合运算
set_1_2 = set1 & set2
set_more_1 = set1 - set_1_2
set_more_2 = set2 - set_1_2
# 将集合重新转化为列表
list_more_1 = [eval(x) for x in set_more_1]
list_more_2 = [eval(x) for x in set_more_2]
f = open('../report/接口更改检测报告-{}.txt'.format(current_time), 'a', encoding='utf-8')
print("#" * 100)
f.write("#" * 100 + "\n")
if list_more_1 or list_more_2:
# print(list_more_1)
# print(list_more_2)
# 都存在说明是 之前接口相比现在有变化
print("服务{}的接口变化如下".format(file1[:4]))
f.write("服务{}的接口变化如下".format(file1[:4]) + "\n")
for x2 in list_more_2:
print("-" * 40, "接口参数变化", "-" * 40)
f.write("-" * 40 + "接口参数变化" + "-" * 40 + "\n")
for x1 in list_more_1:
# 路径和方法相同就是同一个接口
# 接口变化,好像只是参数变化,那么直接看参数
if x2[1] == x1[1] and x2[2] == x1[2]:
x2_list = [x2[0],x2[4],x2[1],x2[2]]
x2_str_list = ["接口标识", "接口名称", "接口路径", "请求方法"]
# 如果是字典转化为集合,保留的只是键,不能判断值有什么不同
# 把字典转换为 列表-元祖的形式
set_tuple_x2 = set()
set_tuple_x1 = set()
for key, value in eval(x2[5]).items():
set_tuple_x2.add((key, value))
for key, value in eval(x1[5]).items():
set_tuple_x1.add((key, value))
# print(set_tuple_x2)
# print(set_tuple_x1)
# 集合运算
set_tuple_x2_x1 = set_tuple_x2 & set_tuple_x1
set_tuple_x2_more = set_tuple_x2 - set_tuple_x2_x1
set_tuple_x1_more = set_tuple_x1 - set_tuple_x2_x1
if not set_tuple_x1_more and set_tuple_x2_more:
# x1 中没有参数,而x2中有参数,说明是接口新增参数
x2_str_list.append("新增参数")
x2_list.append(str(set_tuple_x2_more))
for index in range(5):
print('{:<25}: {:<10}'.format(x2_str_list[index], x2_list[index]))
f.write('{:<25}: {:<10}'.format(x2_str_list[index], x2_list[index]) + "\n")
if set_tuple_x1_more and set_tuple_x2_more:
# 如果都存在的话,说明之前已经存在的接口参数有变化
# 猜测可能是参数类型变化。
x2_str_list.append("之前参数")
x2_str_list.append("现在参数")
x2_list.append(str(set_tuple_x1_more))
x2_list.append(str(set_tuple_x2_more))
for index in range(6):
print('{:<25}: {:<10}'.format(x2_str_list[index], x2_list[index]))
f.write('{:<25}: {:<10}'.format(x2_str_list[index], x2_list[index]) + "\n")
# x2子集 在x1 中遍历,找到退出,找不到,说明新增了一个接口
break
else:
# x2 在 x1 中找不到,就是新增了一个接口
print("-" * 40, "新增接口", "-" * 40)
f.write("-" * 40 + "新增接口" + "-" * 40 + "\n")
x2_str_list = ["接口标识", "接口名称", "接口路径", "请求方法"]
x2_list = [x2[0], x2[4], x2[1], x2[2]]
for index in range(4):
print('{:<25}: {:<10}'.format(x2_str_list[index], x2_list[index]))
f.write('{:<25}: {:<10}'.format(x2_str_list[index], x2_list[index]) + "\n")
# 还有一种可能是x1 中有,x2中没有,说明是删除了一个接口
for x1 in list_more_1:
for x2 in list_more_2:
# 路径和请求方法相同,说明是一个接口
if x2[1] == x1[1] and x2[2] == x1[2]:
break
else:
# x1 里有,x2里没有,说明是删除了一个接口
print("-" * 40, "被删除的接口", "-" * 40)
f.write("-" * 40 + "被删除的接口" + "-" * 40 + "\n")
x1_str_list = ["接口标识", "接口名称", "接口路径", "请求方法"]
x1_list = [x1[0], x1[4], x1[1], x1[2]]
for index in range(4):
print('{:<25}: {:<10}'.format(x1_str_list[index], x1_list[index]))
f.write('{:<25}: {:<10}'.format(x1_str_list[index], x1_list[index]) + "\n")
else:
# 说明接口没有变化
print("服务{}的接口没有变化".format(file1[:4]))
f.write("服务{}的接口没有变化".format(file1[:4]) + "\n")
f.close()
def diff_all():
"""对比所有的文件,根据一个2维列表"""
csv_list = get_csv_list()
csv_list_sort = parse_list(csv_list)
# print(csv_list_sort)
for x in csv_list_sort:
# todo 修改这里
diff_file2(x[-1], x[-2])
if __name__ == '__main__':
csv_path = '../csv' # 存放 swagger csv
diff_all()
三、生成报告
版本一
版本二
版本一需要肉眼对比版本的变化,版本二把变化总结成结论展示。