配置比对脚本(python)

时间:2022-04-30 07:56:40
# -*- coding: utf- -*-
from datetime import *
import MySQLdb
import sys
import time
import datetime
import json
import logging

logging.basicConfig(level=logging.INFO,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='tb_test004.log',
                filemode='w')

console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

def process():
    dict1 = {}
    file1 = open("newlogic52.txt")
    :
        line = file1.readline()
        if not line:
           break
        arr_line = line.split( );
        :
            #logging.info(arr_line[])
            #logging.info(arr_line[])
            key = arr_line[]
            value = arr_line[]
            dict1[key] = value
        else:
            continue

    logging.info("-------------------------")

    dict2 = {}
    file2 = open("oldlogic52.txt")
    :
        line = file2.readline()
        if not line:
           break
        arr_line = line.split( );
        :
            #logging.info(arr_line[])
            #logging.info(arr_line[])
            key = arr_line[]
            value = arr_line[]
            dict2[key] = value
        else:
            continue

    f = file("hello3.txt","w+")
    logging.info("************************")
    sume =
    for key1 in dict1:
        #logging.info(key1)
        #logging.info(dict1[key1])
        for key2 in dict2:
            # logging.info(key2)
            # logging.info(dict2[key2])
            if key1.strip() == key2.strip() and dict1[key1].strip() != dict2[key2].strip():
                li1 = "new key:%s , value: %s \n" % (key1,dict1[key1])
                #logging.info(li1)
                li2 = "old key:%s , value: %s \n" % (key2,dict2[key2])
                #logging.info(li2)

            if key1.strip() == key2.strip() and dict1[key1].strip() == dict2[key2].strip():
                sume = sume +
            if key1.strip() == key2.strip() and dict1[key1].strip() == dict2[key2].strip():
                li1 = "%s=%s\n" % (key1.strip(),dict1[key1].strip())
                f.writelines(li1)
    f.close()

    dict3 = {}
    file3 = open("hello3.txt")
    :
        line = file3.readline()
        if not line:
           break
        arr_line = line.split( );
        :
            #logging.info(arr_line[])
            #logging.info(arr_line[])
            key = arr_line[]
            value = arr_line[]
            dict3[key] = value
        else:
            continue

    for key2 in dict2:
        # logging.info(key2)
        # logging.info(dict3[key2])
        if dict3[key2] == None:
            logging.info(key2)

    logging.info("@@@@@@@@@@@@@@@@@@@@@@@@@@")
    logging.info(sume)
    logging.info("new %s" % len(dict1))
    logging.info("old %s" % len(dict2))
    logging.info("same %s" % len(dict3))

if __name__ == "__main__":
   process()