您好,登錄后才能下訂單哦!
應用場景:ini類型配置文件由于升級改動了,我想看看升級后的配置文件相對于之前的改動了哪些配置項
ini類型的配置文件的特點:
就像這樣子:
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myity]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
每個配置塊內容前有一個[xxx]的section
現在我們要對比的是升級后改動的配置文件相對于升級前哪些section中改了哪些配置,是
新增的,還是值更新了
這里面用Python解決的難點,一個是每個section要和下面的配置項進行綁定,第二個問題是對比配置的不同,可以提取舊文件的key存起來,再通過遍歷新文件中的鍵是不是在舊文件的key中
以下是實現的代碼:
import re
import sys
def data_block(file_stream):
flag = False
for line in file_stream:
line = line.strip()
if line.startswith('[') and line.endswith("]"):
if flag:
yield var
line_list = []
line_list.append(("tag",line))
elif line == "":
continue
else:
line = line.split("=",1)
line = tuple(line)
line_list.append(line)
var = dict(line_list)
flag = True
yield var
def line_count(keywords, filename):
"""
:param keywords: 對比舊文件,在新文件中改變值的鍵名或新增的鍵
:return: 返回鍵所在的行號
:filename 文件名稱
"""
with open(filename) as fp:
for index, line in enumerate(fp):
if keywords in line:
return index+1
def compare_config(dict1, dict2):
"""
遍歷新文件中的每個鍵是否在舊文件中存在,如果存在,則比較值是否相同,不相同則打印配置更新,和所在的位置
否則視為在新文件中新增的項
:return:
"""
for k2 in dict2.keys():
k1 = list(dict1.keys())
if k2 in k1:
if dict2[k2] != dict1[k2]:
count = line_count(dict2['tag'], file2)
print("%s中配置項值更新:%s=%s-->%s=%s,位置在第%s行" %(dict2['tag'], k2, dict1[k2],k2, dict2[k2], count))
else:
count = line_count(k2,file2)
print("%s中新增配置項:%s=%s,位置在第%s行" %(dict2['tag'], k2, dict2[k2], count))
# 新文件中刪除了哪些項,在舊文件中有,在新文件中沒有的項
set1 = set(dict1.keys())
set2 = set(dict2.keys())
deleteKeys = set1 - set2
for k1 in deleteKeys:
count = line_count(dict1['tag'], file1)
print("新文件%s中刪除了配置:%s=%s,位置在舊文件中的第%s行" %(dict2['tag'], k1, dict1[k1],count))
def delete_config(dict1_tag, dict2_tag, data_list1):
delete_section = set(dict1_tag) - set(dict2_tag)
for dict1 in data_list1:
if dict1['tag'] in delete_section:
count = line_count(dict1['tag'], file1)
print("新文件中已經刪除了配置%s,位置在舊文件中的第%s行" %(dict1['tag'],count))
dict1.pop("tag")
for k1,v1 in dict1.items():
print(k1 + " = " + v1)
if __name__ == '__main__':
try:
file1 = sys.argv[1]
file2 = sys.argv[2]
except:
print("userage:xxx.py oldfile newfile")
sys.exit(1)
fp1 = open(file1, 'r')
fp2 = open(file2, 'r')
data_list1 = []
a1 = data_block(fp1)
for i in a1:
data_list1.append(i)
data_list2 = []
a2 = data_block(fp2)
for i in a2:
data_list2.append(i)
dict1_tag = []
for dict1 in data_list1:
dict1_tag.append(dict1['tag'])
dict2_tag = []
for dict2 in data_list2:
dict2_tag.append(dict2['tag'])
for dict2 in data_list2:
if dict2['tag'] in dict1_tag:
for dict1 in data_list1:
if dict1['tag'] == dict2['tag']:
"""dict1,dict2是鍵值對字典"""
compare_config(dict1, dict2)
else:
print("新文件中新增%s配置,配置項如下:" % dict2["tag"])
dict2.pop("tag")
for k2,v2 in dict2.items():
print(k2 + " = " + v2)
delete_config(dict1_tag, dict2_tag, data_list1)
fp1.close()
fp2.close()
測試:
1.準備兩個配置文件,新文件中有的配置刪了,有的是新增的
config1.ini:
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[sfsdamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myity]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
config2.ini:
[isamchk]
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
hello = world
[sfsdamchk]
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myity]
key_buffer = 1288M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myity2]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
python compare-config2.py config1.ini config2.ini
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。