python3 achieve contrast differences script configuration file

Scenario: Due to the configuration file upgrade changes, we would like to see the upgraded configuration file changes with respect to the previous which configuration items
Note: This script can only detect configuration file is key-value pairs, is of key = value form
I find the internet a long time could not find a case of this one, most of them are using a visual contrast to some of the difflib library to do, so he tries to write a

# 该脚本实现两个配置文件中,新文件相对于旧文件的增删改的配置项输出功能
# 配置文件必须是key = value的形式

import re
import sys

def data2list(file_stream):
    """
    生成器
    :param file_stream:接收打开的文件对象
    :return:
    """
    for line in file_stream:
        line = line.strip()
        if line == "": #过滤空行
            continue
        line = line.split(" = ") #每行转为列表
        line2tuple = tuple(line) #列表转元组
        yield line2tuple #每次返回一个元组

def line_count(keywords, filename):
    """
    :param keywords: 对比旧文件,在新文件中改变值的键名或新增的键
    :return: 返回键所在的行号
    :filename 文件名称
    """
    count = 1
    with open(filename) as fp:
        for line in fp:
            line = line.strip()
            if re.search(keywords, line):
                return count
            count += 1

#比较新文件中配置型值改变的和新增的配置
def compare_config():
    """
    遍历新文件中的每个键是否在旧文件中存在,如果存在,则比较值是否相同,不相同则打印配置更新,和所在的位置
    否则视为在新文件中新增的项
    :return:
    """
    global dict1,dict2
    for k2 in dict2.keys():
        k1 = list(dict1.keys())
        if k2 in k1:
            if dict2[k2] != dict1[k2]:
                count = line_count(k2, file2)
                print("配置项值更新:%s=%s-->%s=%s,位置在第%s行" %(k2, dict1[k2],k2, dict2[k2], count))
        else:
            count = line_count(k2,file2)
            print("新增配置项:%s=%s,位置在第%s行" %(k2, dict2[k2], count))

    # 新文件中删除了哪些项,在旧文件中有,在新文件中没有的项
    set1 = set(dict1.keys())
    set2 = set(dict2.keys())
    deleteKeys = set1 - set2
    for k1 in deleteKeys:
        count = line_count(k1, file1)
        print("新文件中删除了以下配置:%s=%s,位置在旧文件中的第%s行" %(k1, dict1[k1],count))

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)
    fp2 = open(file2)

    #通过生成器的具有迭代器特点,遍历来生成一个列表,列表中嵌套了数组,每个数组里面村了每行的数据
    gen1 = data2list(fp1)
    list1 = []
    for i in gen1:
        list1.append(i)
    dict1 = dict(list1) # dict函数可以把列表中嵌套的元组转为字典

    gen2 = data2list(fp2)
    list2 = []
    for i in gen2:
        list2.append(i)
    dict2 = dict(list2)

    fp1.close()
    fp2.close()

    compare_config()

Test results are as follows:
prepare two file
file1
python3 achieve contrast differences script configuration file
file2:
python3 achieve contrast differences script configuration file
Run: file1 file2 config_compare.py
outputs the comparison result:
CI value updating: age = 19 -> age = 20, the third position in the row
update CI value: gender = man -> gender = female, a position in line 4
CI value updating: apple = 5 -> apple = 6, the position of the sixth row
new configuration items: peach = 2, the position of the line 9
the new configuration item : hello = world, a position in line 11
new configuration item: language = english, position in row 12
to delete the following new configuration file: banana = 3, line 8 position in the old file
new file is deleted the following configuration: name = wangtao, the position of the second row in the old file

Guess you like

Origin blog.51cto.com/13560219/2450949