tools/userland-fetch
changeset 135 5c6feb0253fa
parent 58 d8024c042a00
child 136 6a7dd3a5aaf9
equal deleted inserted replaced
134:08912320a4ca 135:5c6feb0253fa
    30 
    30 
    31 import os
    31 import os
    32 import sys
    32 import sys
    33 import shutil
    33 import shutil
    34 from urllib import splittype, urlopen
    34 from urllib import splittype, urlopen
    35 
    35 import hashlib
    36 def validate(filename, hash):
    36 
    37 	import hashlib
    37 def validate(file, hash):
    38 
       
    39 	try:
       
    40 		file = open(filename, 'r')
       
    41 	except IOError:
       
    42 		return False
       
    43 
       
    44 	algorithm, hashvalue = hash.split(':')
    38 	algorithm, hashvalue = hash.split(':')
    45 	try:
    39 	try:
    46 		m = hashlib.new(algorithm)
    40 		m = hashlib.new(algorithm)
    47 	except ValueError:
    41 	except ValueError:
    48 		return False
    42 		return False
    52 		m.update(block)
    46 		m.update(block)
    53 		if block == '':
    47 		if block == '':
    54 			break
    48 			break
    55 
    49 
    56 	return "%s:%s" % (algorithm, m.hexdigest())
    50 	return "%s:%s" % (algorithm, m.hexdigest())
       
    51 
       
    52 def validate_container(filename, hash):
       
    53 	try:
       
    54 		file = open(filename, 'r')
       
    55 	except IOError:
       
    56 		return False
       
    57 	return validate(file, hash)
       
    58 
       
    59 
       
    60 def validate_payload(filename, hash):
       
    61 	import re
       
    62 	import gzip
       
    63 	import bz2
       
    64 
       
    65 	expr_bz = re.compile('.+\.bz2$', re.IGNORECASE)
       
    66 	expr_gz = re.compile('.+\.gz$', re.IGNORECASE)
       
    67 
       
    68 	try:
       
    69 		if expr_bz.match(filename):
       
    70 			file = bz2.BZFile(filename, 'r')
       
    71 		elif expr_gz.match(filename):
       
    72 			file = gzip.GzipFile(filename, 'r')
       
    73 		else:
       
    74 			return False
       
    75 	except IOError:
       
    76 		return False
       
    77 	return validate(file, hash)
       
    78 
    57 
    79 
    58 def download(url, filename = None):
    80 def download(url, filename = None):
    59 	src = None
    81 	src = None
    60 
    82 
    61 	try:
    83 	try:
   176 		print "\n    validating...",
   198 		print "\n    validating...",
   177 		if hash_arg == None:
   199 		if hash_arg == None:
   178 			print "skipping (no hash)"
   200 			print "skipping (no hash)"
   179 			sys.exit(0)
   201 			sys.exit(0)
   180 			
   202 			
   181 		realhash = validate(name, hash_arg)
   203 		realhash = validate_container(name, hash_arg)
   182 		if realhash == hash_arg:
   204 		if realhash == hash_arg:
   183 			print "ok"
   205 			print "ok"
   184 			sys.exit(0)
   206 			sys.exit(0)
   185 		else:
   207 		else:
       
   208 			payloadhash = validate_payload(name, hash_arg)
       
   209 			if payloadhash == hash_arg:
       
   210 				print "ok"
       
   211 				sys.exit(0)
   186 			print "corruption detected"
   212 			print "corruption detected"
   187 			print "    expected: %s" % hash_arg
   213 			print "    expected: %s" % hash_arg
   188 			print "    actual:   %s" % realhash
   214 			print "    actual:   %s" % realhash
       
   215 			print "    payload:  %s" % payloadhash
   189 
   216 
   190 		try:
   217 		try:
   191 			os.remove(name)
   218 			os.remove(name)
   192 		except OSError:
   219 		except OSError:
   193 			pass
   220 			pass