python进行文件拷贝及compare,并html形式输出差异

python脚本:

#coding:utf-8
__author__ = '**'
import difflib
import webbrowser
import configparser
from bs4 import BeautifulSoup
# import pandas
# import numpy
import os
from shutil import copyfile

def check_diff(file1,file2,filename):
        # file1="D:/Python35/project/WebTest/files/med_node_1_svc.xml"
        # file2="D:/Python35/project/WebTest/files/med_node_1_svc2.xml"
        # file1="files/med_node_1_svc.xml"
        # file2="files/med_node_1_svc2.xml"
        resultPath="result"
        isExists=os.path.exists(resultPath)
        if not isExists:
        # # 如果不存在则创建目录
        #  # 创建目录操作函数
          os.makedirs(resultPath)
          print(resultPath+' 创建成功')
        print("file1:"+file1)
        print("file2:"+file2)

        with open(file1, 'r') as f:
            content1 = f.read().splitlines()
        with open(file2, 'r') as f:
            content2 = f.read().splitlines()
        htmlDiff = difflib.HtmlDiff(tabsize=2,wrapcolumn=100)
        tmp=resultPath+"/"+filename+'.html'    #比对结果文件以"被比对文件的名称"命名
        with open(tmp, 'w') as f:
             f.write(htmlDiff.make_file(content1, content2))
            # diff_tmp=htmlDiff.make_file(content1, content2)
            # f.write(htmlDiff.make_file(content1, content2))
        with open(tmp, 'r') as f2:
            soup = BeautifulSoup(f2.read(), 'html.parser')
            diff_add =soup.find_all(attrs={"class":"diff_add"})
            diff_chg =soup.find_all(attrs={"class":"diff_chg"})
            diff_sub =soup.find_all(attrs={"class":"diff_sub"})
            if(len(diff_add)>1 or len(diff_chg)>1 or len(diff_sub)>1):
                copyfile(tmp, filename+'.html')
                webbrowser.open(filename+'.html')
        # print(htmlDiff.make_file(content1, content2))

def compareFile():
    config=configparser.ConfigParser()
    config.read("files/filespath.conf")
    iemp_root=config.get("iEPM","iEPM_ROOT")
    bakfile=config.get("iEPM","bakfile")

    # #med_node_1_svc文件比较
    # med_node_1_svc_path=iemp_root+config.get("path","med_node_1_svc")  #获取要比对的文件在网管里的路径
    # med_node_1_svc_name=getfilename(med_node_1_svc_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
    # check_diff("files/"+med_node_1_svc_name,med_node_1_svc_path,med_node_1_svc_name)
    #
    #  #fm.xml文件比较
    # fm_path=iemp_root+config.get("path","fm")  #获取要比对的文件在网管里的路径
    # fm_name=getfilename(fm_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
    # check_diff("files/"+fm_name,fm_path,fm_name)

    #比对fm的配置文件
    if(config.has_section("FM") ):
      for key,value in config.items("FM"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)

    #比对Frame的配置文件
    if(config.has_section("Frame") ):
      for key,value in config.items("Frame"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)
          file1=open(bakfile+"/"+item_name, 'r')
          file2=open(item_path,'r')
          # diff file1 file2

    #比对nbi的配置文件
    if(config.has_section("nbi") ):
      for key,value in config.items("nbi"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)

    #比对sm的配置文件
    if(config.has_section("SM") ):
      for key,value in config.items("SM"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)

    #比对EAM的配置文件
    if(config.has_section("EAM") ):
      for key,value in config.items("EAM"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)

    #比对NM的配置文件
    if(config.has_section("NM") ):
      for key,value in config.items("NM"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)

    #比对auto的配置文件
    if(config.has_section("auto") ):
      for key,value in config.items("auto"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)

    #比对auto的配置文件
    if(config.has_section("Search") ):
      for key,value in config.items("Search"):
          item_path = iemp_root+value       #获取要比对的文件在网管里的路径
          item_name=getfilename(item_path)  #获取要比对的文件的名称,方便查找备份目录里对应哪个文件
          check_diff(bakfile+"/"+item_name,item_path,item_name)
    # ops=config.items("fm")
    # for item in ops:
    #     item_path=ops.index(item)


#根据文件路径截取最后面的文件名称
def getfilename(filepath):
    # n = filepath.ReverseFind('\\')
    strFileName =filepath.rsplit('/',1)[1]
    # filepath = filepath.Left(n);
    # strFileName = filepath.Right( filepath.GetLength()-(n+1) );
    return strFileName

#备份各特性需要关注的文件到files目录下
def bakfiles():
     print()
if __name__ == '__main__':
    bakfiles()
    compareFile()

对应filepath文件:

[iEPM]
iEPM_ROOT=D:\iEPM
bakfile=D:/Python35/project/WebTest/update/files


[Frame]
hornetq-configuration-cluster = /etc/iepm.framework/hornetq-configuration-cluster.xml
messaging-client-config = /etc/iepm.framework/messaging-client-config.xml
oms = /etc/oms.xml


[FM]
fm_config_1=/app/com.oms.app.fm/config/fm_config_1.xml
emailrule=/etc/oms.fm/emailrule.xml

html格式展现差异:

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值