Python 3 implementation profile difference comparison script

Keywords: Python

Application scenario: the configuration file has been changed due to the upgrade. We want to see what configuration items have been changed in the upgraded configuration file compared with the previous changes
Note: the configuration file that this script can only detect is in the form of key value pair, that is, in the form of key=value
I've been searching the Internet for a long time, but I haven't found this case. Most of them are visual comparison with some difflib libraries, so I tried to write one myself

# The script implements the output function of configuration items in two configuration files, i.e. adding, deleting and modifying new files relative to old ones
# The configuration file must be in the form of key = value

import re
import sys

def data2list(file_stream):
    """
    //generator
    :param file_stream:Receive open file objects
    :return:
    """
    for line in file_stream:
        line = line.strip()
        if line == "": #Filtered blank lines
            continue
        line = line.split(" = ") #Turn each row into a list
        line2tuple = tuple(line) #List tuple
        yield line2tuple #One tuple at a time

def line_count(keywords, filename):
    """
    :param keywords: Compared with the old file, change the key name of the value or add a new key in the new file
    :return: Return the line number of the key
    : filename File name
    """
    count = 1
    with open(filename) as fp:
        for line in fp:
            line = line.strip()
            if re.search(keywords, line):
                return count
            count += 1

#Compare the changed and new configuration in the new file
def compare_config():
    """
    //Traverse whether each key in the new file exists in the old file, if so, whether the comparison value is the same, if not, print the configuration update, and the location
    //Otherwise, it is regarded as the new item in the new file
    :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("Configuration item value update:%s=%s-->%s=%s,Location is in the first place.%s That's ok" %(k2, dict1[k2],k2, dict2[k2], count))
        else:
            count = line_count(k2,file2)
            print("New configuration item:%s=%s,Location is in the first place.%s That's ok" %(k2, dict2[k2], count))

    # What items have been deleted in the new file, in the old file, and not in the new file
    set1 = set(dict1.keys())
    set2 = set(dict2.keys())
    deleteKeys = set1 - set2
    for k1 in deleteKeys:
        count = line_count(k1, file1)
        print("The following configuration was removed from the new file:%s=%s,Location in old file%s That's ok" %(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)

    #Through the iterator feature of the generator, a list is always generated. Arrays are nested in the list, and each array contains the data of each row
    gen1 = data2list(fp1)
    list1 = []
    for i in gen1:
        list1.append(i)
    dict1 = dict(list1) # The dict function can turn a tuple nested in a list into a dictionary

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

    fp1.close()
    fp2.close()

    compare_config()

The test results are as follows:
Prepare two files
file1

file2:

Run: config.py file1 File2
Output comparison results:
Configuration item value update: age = 19 -- > age = 20, position in line 3
Configuration item value update: gender = man -- > gender = male, line 4
Configuration item value update: Apple = 5 -- > Apple = 6, position in line 6
New configuration item: peach=2, position in line 9
New configuration item: hello=world, location on line 11
New configuration item: language=english, position in line 12
The following configuration was removed from the new file: banana=3, on line 8 of the old file
The following configuration was removed from the new file: name=wangtao, in line 2 of the old file

Posted by zenabi on Sun, 17 Nov 2019 08:43:05 -0800