diff 更新 检查脚本

工作中常需要对某些数据文件进行更新检查, 特写了个小脚本来完成.


#!/usr/bin/env python
# encoding: utf-8

"""
@filename : diff_check.py
@Author : idup2x@gmail.com
"""

import sys, getopt

_a_nums = 0
_c_nums = 0
_d_nums = 0

def _handle_a(line):
global _a_nums
_l = line.split('a')
if len(_l) > 1:
_ll = _l[1].split(',')
if len(_ll) > 1:
_a_nums = _a_nums + int(_ll[1]) - int(_ll[0]) + 1
else:
_a_nums = _a_nums + 1
else:
print 'bad a is catched'

def _handle_d(line):
global _d_nums
_l = line.split('d')
if len(_l) > 1:
_ll = _l[0].split(',')
if len(_ll) > 1:
_d_nums = _d_nums + int(_ll[1]) - int(_ll[0]) + 1
else:
_d_nums = _d_nums + 1
else:
print 'bad d is catched'

def _handle_c_c(line):
_l = line.split(',')
if len(_l) > 1:
_cnt = int(_l[1]) - int(_l[0]) + 1
return _cnt
else:
return 1

def _handle_c(line):
global _c_nums
_l = line.split('c')
if len(_l) > 1:
_d0 = _handle_c_c(_l[0])
_d1 = _handle_c_c(_l[1])
if _d0 >= _d1:
_c_nums = _c_nums + _d0
else:
_c_nums = _c_nums + _d1
else:
print 'bad c is catched'

def _main():
global input_file_name
_rf = open(input_file_name, "r")
while 1:
line = _rf.readline()
if not line:
break
if -1 != line.find('a'):
_handle_a(line)
elif -1 != line.find('d'):
_handle_d(line)
elif -1 != line.find('c'):
_handle_c(line)
_rf.close()

def _usage():
print "./check_change.py -i inputfile.diff"

def _check_opt():
global input_file_name
opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
if ( len(sys.argv) < 2 ):
_usage()
sys.exit()
for op, value in opts:
if op == "-i":
input_file_name = value
elif op == "-h":
_usage()
sys.exit()
else:
_usage()
sys.exit()


if __name__ == "__main__":
input_file_name = "file.diff"
_check_opt()
_main()
print '增加:', _a_nums
print '删除:', _d_nums
print '更新:', _c_nums
print '共计:', _a_nums + _d_nums + _c_nums
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值