md5sum_check.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import hashlib
00004 import os
00005 from operator import add
00006 from pprint import pprint
00007 
00008 
00009 def md5sum_check_dir_to_name(full_dir):
00010   name = full_dir.replace("/", "_")
00011   name = name.replace("-", "_")
00012   if name[0] == "_":
00013     name = name[1:]
00014   return name
00015 
00016 
00017 def md5sum_check_filename(full_dir):
00018   return "md5sum_" + md5sum_check_dir_to_name(full_dir) + ".py"
00019 
00020 
00021 def md5sum_file(filename):
00022   if os.path.exists(filename):
00023     file_md5 = hashlib.md5(open(filename, 'rb').read()).hexdigest()
00024   else:
00025     file_md5 = 'No such file or directory'
00026     print "  ** ", filename, "\t\t", file_md5
00027   return file_md5
00028 
00029 
00030 def md5sum_check_dir(dir_info, output_dir=''):
00031   ret = True
00032   info = {}
00033 
00034   full_dir = dir_info[0]
00035   dir_md5 = 0
00036   print "  Check ", full_dir,
00037   for root, dirs, files in os.walk(full_dir):
00038     for file in files:
00039       filename = os.path.join(root, file)
00040       file_md5 = md5sum_file(filename)
00041       info[filename] = file_md5
00042       dir_md5 = dir_md5 ^ int(file_md5, 16)
00043   print "   \t(", hex(dir_md5), ")\t",
00044   if dir_md5 != dir_info[1]:
00045     print "False"
00046     ret = False
00047   else:
00048     print "Ok"
00049 
00050   filename = md5sum_check_filename(full_dir)
00051 
00052   print "  Writing results to ... ", os.path.join(output_dir, filename)
00053   f = open(os.path.join(output_dir, filename), 'wb')
00054   print >> f, 'info=',
00055   pprint(info, f, width=240)
00056   f.close()
00057 
00058   return ret
00059 
00060 
00061 def md5sum_check_files(full_dir, info):
00062   ret = True
00063   for root, dirs, files in os.walk(full_dir):
00064     for file in files:
00065       filename = os.path.join(root, file)
00066       file_md5 = md5sum_file(filename)
00067       if filename in info.keys():
00068         if info[filename] != file_md5:
00069           print " **", filename, "\t\t has changed"
00070           ret = False
00071         del info[filename]
00072       else:
00073         print " **", filename, "\t\t is possibly newly added (not found on database)."
00074         ret = False
00075   for fname in info.keys():
00076     print " **", fname, "\t\t is missing."
00077   return ret
00078 
00079 # for file in files
00080 #   print [hashlib.md5(open(fname, 'rb').read()).hexdigest() for fname in reduce(add, [[os.path.join(root, file) for file in files] for root, dirs, files in os.walk('$dir')])]"
00081 # done


hironx_ros_bridge
Author(s): Kei Okada
autogenerated on Mon Oct 6 2014 07:19:42