userland-fetch revision 3533
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
# userland-fetch - a file download utility
#
# A simple program similiar to wget(1), but handles local file copy, ignores
# directories, and verifies file hashes.
#
""" Function to decode and print IOError type exception """
try:
print str(e)
"""Given paths to a file and a detached PGP signature, verify that
the signature is valid for the file. Current configuration allows for
unrecognized keys to be downloaded as necessary."""
# Find the root of the repo so that we can point GnuPG at the right
# configuration and keyring.
if proc.returncode != 0:
return False
# Skip the permissions warning: none of the information here is private,
# so not having to worry about getting mercurial keeping the directory
# unreadable is just simplest.
try:
# If the executable simply couldn't be found, just skip the
# validation.
if e.errno == errno.ENOENT:
return False
if proc.returncode != 0:
# Only print GnuPG's output when there was a problem.
print proc.stdout.read()
return False
return True
"""Given a file-like object and a hash string, verify that the hash
matches the file contents."""
try:
algorithm = "sha256"
# force migration away from sha1
if algorithm == "sha1":
algorithm = "sha256"
try:
m = hashlib.new(algorithm)
return False
while True:
try:
break
if block == '':
break
"""Given a file path and a hash string, verify that the hash matches the
file contents."""
try:
return False
"""Given a file path and a hash string, verify that the hash matches the
payload (uncompressed content) of the file."""
try:
if expr_bz.match(filename):
elif expr_gz.match(filename):
elif expr_tgz.match(filename):
else:
return False
return False
"""Download the content at the given URL to the given filename
(defaulting to the basename of the URL if not given. If 'quiet' is
True, throw away any error messages. Returns the name of the file to
which the content was donloaded."""
try:
return None
# 3xx, 4xx and 5xx (f|ht)tp codes designate unsuccessfull action
return None
try:
return None
while True:
if block == '':
break;
# return the name of the file that we downloaded the data to.
return filename
"""Returns a list of URLs where the file 'filename' might be found,
using 'url', 'search', and $DOWNLOAD_SEARCH_PATH as places to look.
If 'filename' is None, then the list will simply contain 'url'.
"""
if tmp:
# filename should always be first
urls.insert(0, filename)
# command line url is a fallback, so it's last
return urls
"""Attempts to download a file from a number of possible locations.
Generates a list of paths where the file ends up on the local
filesystem. This is a generator because while a download might be
successful, the signature or hash may not validate, and the caller may
want to try again from the next location. The 'link_arg' argument is a
boolean which, when True, specifies that if the source is not a remote
URL and not already found where it should be, to make a symlink to the
source rather than copying it.
"""
print "Source %s..." % url,
print "not found, skipping file copy"
continue
print "\n copying..."
else:
print "\n linking..."
print "\n downloading...",
print "failed"
continue
print "Usage: %s [-f|--file (file)] [-l|--link] [-h|--hash (hash)] " \
"[-s|--search (search-dir)] [-S|--sigurl (signature-url)] --url (url)" % \
sys.exit(1)
# FLUSH STDOUT
try:
["file=", "link", "hash=", "search=", "sigurl=", "url="])
usage()
else:
usage()
print "\n validating signature...",
print "skipping (no signature URL)"
else:
# Put the signature file in the same directory as the
# file we're downloading.
# Validate with the first signature we find.
if sig_file:
if validate_signature(name, sig_file):
print "ok"
else:
print "failed"
break
else:
continue
else:
print "failed (couldn't fetch signature)"
print " validating hash...",
print "skipping (no hash)"
print "hash is: %s" % realhash
print "ok"
else:
if payloadhash == hash_arg:
print "ok"
else:
# If the signature validated, then we assume
# that the expected hash is just a typo, but we
# warn just in case.
if sig_valid:
print "invalid hash!"
else:
print "corruption detected"
print " expected: %s" % hash_arg
print " actual: %s" % realhash
print " payload: %s" % payloadhash
# An invalid hash shouldn't cause us to remove
# the target file if the signature was valid.
try:
continue
sys.exit(0)
sys.exit(1)
if __name__ == "__main__":
main()