download_checkmd5.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 NAME="download_checkmd5.py"
00004 
00005 import urllib, hashlib, os, sys
00006 from optparse import OptionParser
00007 
00008 def main():
00009   parser = OptionParser(usage="usage: %prog URI dest [md5sum]", prog=NAME)
00010   options, args = parser.parse_args()
00011   md5sum = None
00012   if len(args) == 2:
00013     uri, dest = args
00014   elif len(args) == 3:
00015     uri, dest, md5sum = args
00016   else:
00017     parser.error("wrong number of arguments")
00018 
00019   # Create intermediate directories as necessary, #2970
00020   d = os.path.dirname(dest)
00021   if len(d) and not os.path.exists(d):
00022     os.makedirs(d)
00023 
00024   fresh = False
00025   if not os.path.exists(dest):
00026     sys.stdout.write('[rosbuild] Downloading %s to %s...'%(uri, dest))
00027     sys.stdout.flush()
00028     urllib.urlretrieve(uri, dest)
00029     sys.stdout.write('Done\n')
00030     fresh = True
00031 
00032   if md5sum:
00033     m = hashlib.md5(open(dest).read())
00034     d = m.hexdigest()
00035 
00036     print '[rosbuild] Checking md5sum on %s'%(dest)
00037   
00038     if d != md5sum:
00039       if not fresh:
00040         print '[rosbuild] WARNING: md5sum mismatch (%s != %s); re-downloading file %s'%(d, md5sum, dest)
00041         os.remove(dest)
00042 
00043         # Try one more time
00044         urllib.urlretrieve(uri, dest)
00045         m = hashlib.md5(open(dest).read())
00046         d = m.hexdigest()
00047     
00048       if d != md5sum:
00049         print '[rosbuild] ERROR: md5sum mismatch (%s != %s) on %s; aborting'%(d, md5sum, dest)
00050         return 1
00051 
00052   return 0
00053 
00054 
00055 if __name__ == '__main__':
00056   sys.exit(main())


rosbuild
Author(s): Brian Gerkey, Troy Straszheim, Morgan Quigley
autogenerated on Thu Jan 2 2014 11:52:45