tools/userland-fetch
changeset 58 d8024c042a00
parent 42 566ce4d2ff99
child 135 5c6feb0253fa
equal deleted inserted replaced
57:91dc164c5b46 58:d8024c042a00
    17 # fields enclosed by brackets "[]" replaced with your own identifying
    17 # fields enclosed by brackets "[]" replaced with your own identifying
    18 # information: Portions Copyright [yyyy] [name of copyright owner]
    18 # information: Portions Copyright [yyyy] [name of copyright owner]
    19 #
    19 #
    20 # CDDL HEADER END
    20 # CDDL HEADER END
    21 #
    21 #
    22 # Copyright (c) 2010, Oracle and/or it's affiliates.  All rights reserved.
    22 # Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
    23 #
    23 #
    24 #
    24 #
    25 # fetch.py - a file download utility
    25 # fetch.py - a file download utility
    26 #
    26 #
    27 #  A simple program similiar to wget(1), but handles local file copy, ignores
    27 #  A simple program similiar to wget(1), but handles local file copy, ignores
    39 	try:
    39 	try:
    40 		file = open(filename, 'r')
    40 		file = open(filename, 'r')
    41 	except IOError:
    41 	except IOError:
    42 		return False
    42 		return False
    43 
    43 
    44 	if (hash == None):
    44 	algorithm, hashvalue = hash.split(':')
    45 		return True
       
    46 
       
    47 	algorithm, value = hash.split(':')
       
    48 	try:
    45 	try:
    49 		m = hashlib.new(algorithm)
    46 		m = hashlib.new(algorithm)
    50 	except ValueError:
    47 	except ValueError:
    51 		return False
    48 		return False
    52 
    49 
    54 		block = file.read()
    51 		block = file.read()
    55 		m.update(block)
    52 		m.update(block)
    56 		if block == '':
    53 		if block == '':
    57 			break
    54 			break
    58 
    55 
    59 	return m.hexdigest() == value
    56 	return "%s:%s" % (algorithm, m.hexdigest())
    60 
    57 
    61 def download(url, filename = None):
    58 def download(url, filename = None):
    62 	src = None
    59 	src = None
    63 
    60 
    64 	try:
    61 	try:
   156 		scheme, path = splittype(url)
   153 		scheme, path = splittype(url)
   157 		name = file_arg
   154 		name = file_arg
   158 
   155 
   159 		if scheme in [ None, 'file' ]:
   156 		if scheme in [ None, 'file' ]:
   160 			if os.path.exists(path) == False:
   157 			if os.path.exists(path) == False:
   161 				print "not found, skipping"
   158 				print "not found, skipping file copy"
   162 				continue
   159 				continue
   163 			elif name != path:
   160 			elif name != path:
   164 				if link_arg == False:
   161 				if link_arg == False:
   165 					print "copying...",
   162 					print "\n    copying..."
   166 					shutil.copy2(path, name)
   163 					shutil.copy2(path, name)
   167 				else:
   164 				else:
   168 					print "linking...",
   165 					print "\n    linking..."
   169 					os.symlink(path, name)
   166 					os.symlink(path, name)
   170 			else:
   167 			else:
   171 				pass
   168 				pass
   172 		elif scheme in [ 'http', 'https', 'ftp' ]:
   169 		elif scheme in [ 'http', 'https', 'ftp' ]:
   173 			print "downloading...",
   170 			print "\n    downloading...",
   174 			name = download(url, file_arg)
   171 			name = download(url, file_arg)
   175 			if name == None:
   172 			if name == None:
   176 				print "failed"
   173 				print "failed"
   177 				continue
   174 				continue
   178 
   175 
   179 		print "validating...",
   176 		print "\n    validating...",
   180 		if validate(name, hash_arg):
   177 		if hash_arg == None:
       
   178 			print "skipping (no hash)"
       
   179 			sys.exit(0)
       
   180 			
       
   181 		realhash = validate(name, hash_arg)
       
   182 		if realhash == hash_arg:
   181 			print "ok"
   183 			print "ok"
   182 			sys.exit(0)
   184 			sys.exit(0)
   183 		else:
   185 		else:
   184 			print "corrupt"
   186 			print "corruption detected"
       
   187 			print "    expected: %s" % hash_arg
       
   188 			print "    actual:   %s" % realhash
   185 
   189 
   186 		try:
   190 		try:
   187 			os.remove(name)
   191 			os.remove(name)
   188 		except OSError:
   192 		except OSError:
   189 			pass
   193 			pass