import getopt
import hashlib
import sys
import os
import time
_FILE_SLIM = 100 * 1024 * 1024
fileInPath = "/webapp07/nfs/sec_lqlog/69_file_hash_.log"
filePaths = ['/webapp01/www/ydb3', '/webapp02/qhwebapps/qhmcc_client9681', '/webapp02/qhwebapps/qhmcc_client9682',
'/webapp02/qhwebapps/qhmcc_manager', '/webapp02/qhwebapps/qhmcc_manager9882',
'/webapp02/qhwebapps/qhmcc_wap9781', '/webapp02/qhwebapps/qhmcc_wap9782', '/webapp02/webapps']
nowTime = time.strftime("%Y%m%d_%H%M%S", time.localtime())
fileOutPath = "/webapp07/nfs/sec_lqlog/69_check_hash_" + nowTime + ".log"
def File_md5(filename):
calltimes = 0
hmd5 = hashlib.md5()
fp = open(filename, "rb")
f_size = os.stat(filename).st_size
result = ""
if f_size > _FILE_SLIM:
while (f_size > _FILE_SLIM):
hmd5.update(fp.read(_FILE_SLIM))
f_size /= _FILE_SLIM
calltimes += 1
if (f_size > 0) and (f_size <= _FILE_SLIM):
hmd5.update(fp.read())
else:
hmd5.update(fp.read())
return str(hmd5.hexdigest())
def wirter(file_and_hash):
with open(fileOutPath, 'a') as f:
f.write(file_and_hash + '\n')
wirter("")
one_list = []
two_list = []
with open(fileInPath) as f:
for line in f:
one_list.append(line.replace("\n", ""))
for dir in filePaths:
print "开始计算=========>" + dir
for fpathe, dirs, fs in os.walk(dir):
for f in fs:
try:
file_hash = File_md5(os.path.join(fpathe, f))
file_is_hash = os.path.join(fpathe, f) + "|" + file_hash
two_list.append(file_is_hash)
except Exception as e:
pass
difference_one = [v for v in one_list if v not in two_list]
difference_two = [v for v in two_list if v not in one_list]
for one in difference_one:
nowTime = time.strftime("%Y%m%d_%H%M%S", time.localtime())
wirter(one + "|" + nowTime+"|文件被删除")
for two in difference_two:
nowTime = time.strftime("%Y%m%d_%H%M%S", time.localtime())
wirter(two + "|" + nowTime + "|新增文件")
print "69_SUCCESS"