userland-fetch revision 5242
920N/A# The contents of this file are subject to the terms of the 749N/A# Common Development and Distribution License (the "License"). 749N/A# You may not use this file except in compliance with the License. 919N/A# See the License for the specific language governing permissions 919N/A# and limitations under the License. 919N/A# When distributing Covered Code, include this CDDL HEADER in each 919N/A# If applicable, add the following below this CDDL HEADER, with the 919N/A# fields enclosed by brackets "[]" replaced with your own identifying 919N/A# information: Portions Copyright [yyyy] [name of copyright owner] 919N/A# Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 919N/A# userland-fetch - a file download utility 749N/A# A simple program similiar to wget(1), but handles local file copy, ignores 749N/A# directories, and verifies file hashes. 749N/A """ Function to decode and print IOError type exception """ 749N/A """Given paths to a file and a detached PGP signature, verify that 749N/A the signature is valid for the file. Current configuration allows for 749N/A unrecognized keys to be downloaded as necessary.""" 749N/A # Find the root of the repo so that we can point GnuPG at the right 920N/A # configuration and keyring. # 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. # If the executable simply couldn't be found, just skip the # Only print GnuPG's output when there was a problem. """Given a file-like object and a hash string, verify that the hash matches the file contents.""" # force migration away from sha1 """Given a file path and a hash string, verify that the hash matches the """Given a file path and a hash string, verify that the hash matches the payload (uncompressed content) of the file.""" """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.""" # 3xx, 4xx and 5xx (f|ht)tp codes designate unsuccessfull action # return the name of the file that we downloaded the data to. """Returns a list of URLs where the file 'filename' might be found, If 'filename' is None, then the list will simply contain 'url'. # filename should always be first # command line url is a fallback, so it's last """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" elif scheme in [
'http',
'https',
'ftp' ]:
print "\n downloading...",
print "Usage: %s [-f|--file (file)] [-l|--link] [-h|--hash (hash)] " \
"[-s|--search (search-dir)] [-S|--sigurl (signature-url)] " \
"[-t|--timeout (timeout)] --url (url)" % \
["file=", "link", "hash=", "search=", "sigurl=", if opt in [
"-f",
"--file" ]:
elif opt in [
"-l",
"--link" ]:
elif opt in [
"-h",
"--hash" ]:
elif opt in [
"-s",
"--search" ]:
elif opt in [
"-S",
"--sigurl" ]:
elif opt in [
"-t",
"--timeout" ]:
print "Invalid argument for %s, should be a " \
"number, but is %s" %
(opt, arg) print "Invalid argument for %s, should be a " \
"positive number, but is %s" %
(opt, arg) elif opt in [
"-u",
"--url" ]:
print "\n validating signature...",
print "skipping (no signature URL)" # Put the signature file in the same directory as the # file we're downloading. # Validate with the first signature we find. print "failed (couldn't fetch signature)" print " validating hash...",
print "skipping (no hash)" # If the signature validated, then we assume # that the expected hash is just a typo, but we print "invalid hash! Did you forget " \
print "corruption detected" # If the hash is invalid, but the signature # validation succeeded, rename the archive (so # the user doesn't have to re-download it) and # fail. Otherwise, try to remove the file and print "archive saved as %s; " \
"if it isn't corrupt, " \