两套代码需要大量diff,遂顺手写了这么个小玩意儿。能对文件diff,也能对相同目录结构的目录进行迭代diff,将diff结果保存为html文件。
参数说明:
o 指定输出的目录名称(也就没管其他路径吧啦吧啦啥的,临时写的,默认当前目录)
f 指定整个文件夹进行diff,默认diff结果也是按照文件目录的结构进行存放的,指定-f放在一起
d 指定对文件夹下所有文件进行遍历diff
上代码:
# coding=utf-8
# Date : 2019/1/29
import difflib, os, optparse
def diff(LEFT_FILE:'file_abs_path', RIGHT_FILE:'file_abs_path', dir='diff'):
OUT_ROOT = os.path.abspath(os.path.join(os.curdir,dir))
TARGET_NAME = ''.join(['diff_', os.path.basename(LEFT_FILE).rsplit('.', 1)[0],
'#', os.path.basename(RIGHT_FILE).rsplit('.', 1)[0]])
if os.path.exists(OUT_ROOT):
index = 0
for i in os.listdir(OUT_ROOT):
if TARGET_NAME == i.rsplit('[', 1)[0]:
index += 1
else:
os.system('mkdir {}'.format(OUT_ROOT))
index = 0
TARGET_FILE = os.path.join(OUT_ROOT, ''.join([TARGET_NAME, '[', str(index), '].html']))
with open(LEFT_FILE) as left, open(RIGHT_FILE) as right, open(TARGET_FILE, 'w') as target:
target.write(difflib.HtmlDiff().make_file(left, right))
print('Generating ...{left} : {right}'.format(left=LEFT_FILE,right=RIGHT_FILE))
def check(dirL:'abs_path', dirR:'abs_path', dir='diff', flat=False):
left = os.listdir(dirL)
right = os.listdir(dirR)
for i in left:
if i in right:
fileL = os.path.join(dirL, i)
fileR = os.path.join(dirR, i)
if os.path.isfile(fileL):
diff(fileL, fileR, dir)
else:
L = os.path.join(dirL, i)
R = os.path.join(dirR, i)
if not flat:
check(L, R, os.path.join(dir,i), True)
else:
check(L, R, dir, True)
if __name__ == '__main__':
usage = 'usage: %prog [options] arg1 arg2'
parser = optparse.OptionParser(usage)
parser.add_option('-d','--dir',
help='diff the same files in two directories',
action='store_true',dest='isDir',default=False
)
parser.add_option('-f','--flat',
help='diff dir\'s output be flat',
action='store_true',dest='flat',default=False
)
parser.add_option('-o','--output',
help='designate the output name',
dest='dir',default='diff')
opts, args = parser.parse_args()
if len(args) == 2:
left, right = args
if opts.isDir is False:
if os.path.isfile(left) and os.path.isfile(right):
diff(left, right, opts.dir)
else:
parser.error('specify file')
else:
if os.path.isdir(left) and os.path.isdir(right):
check(left, right, opts.dir, opts.flat)
else:
parser.error('specify directory')
else:
parser.print_usage()
效果如下:
对Ilib跟Klib两个同组织结构的文件夹下所有文件进行diff,将所有diff结果html保存在hahaha文件夹下。
~>python dirRun.py -d Ilib Klib -o hahaha
Generating ...Ilib\helpers\infra_helper\config_file.rb : Klib\helpers\infra_helper\config_file.rb
Generating ...Ilib\helpers\infra_helper\executor\ssh_with_password.rb : Klib\helpers\infra_helper\executor\ssh_with_password.rb
Generating ...Ilib\helpers\infra_helper\executor.rb : Klib\helpers\infra_helper\executor.rb
Generating ...Ilib\helpers\infra_helper\iptables.rb : Klib\helpers\infra_helper\iptables.rb
Generating ...Ilib\helpers\infra_helper\linux_node.rb : Klib\helpers\infra_helper\linux_node.rb
Generating ...Ilib\helpers\infra_helper\log.rb : Klib\helpers\infra_helper\log.rb
Generating ...Ilib\helpers\infra_helper\neutron_server.rb : Klib\helpers\infra_helper\neutron_server.rb
Generating ...Ilib\helpers\infra_helper\nova_instance.rb : Klib\helpers\infra_helper\nova_instance.rb
Generating ...Ilib\helpers\infra_helper\process.rb : Klib\helpers\infra_helper\process.rb
Generating ...Ilib\helpers\infra_helper.rb : Klib\helpers\infra_helper.rb
Generating ...Ilib\service\base_helper.rb : Klib\service\base_helper.rb
Generating ...Ilib\service\neutron_helper.rb : Klib\service\neutron_helper.rb
Generating ...Ilib\tasks\setup.rb : Klib\tasks\setup.rb
本来还是有愿望日后慢慢完善成一个更好用的cli工具的,不过去生产环境上试了一下diff工具,emmmmm,算了吧,linux上原生工具更加强大。就不再准备继续添加更多华丽的功能里。日后改成windows用GUI程序好了。毕竟Windows上用自用工具还是更舒服些。