10
12
get_archive_handler,
13
from charmhelpers.core.host import mkdir
15
from charmhelpers.core.host import mkdir, check_hash
16
18
class ArchiveUrlFetchHandler(BaseFetchHandler):
17
"""Handler for archives via generic URLs"""
20
Handler to download archive files from arbitrary URLs.
22
Can fetch from http, https, ftp, and file URLs.
24
Can install either tarballs (.tar, .tgz, .tbz2, etc) or zip files.
26
Installs the contents of the archive in $CHARM_DIR/fetched/.
18
28
def can_handle(self, source):
19
29
url_parts = self.parse_url(source)
20
30
if url_parts.scheme not in ('http', 'https', 'ftp', 'file'):
26
36
def download(self, source, dest):
38
Download an archive file.
40
:param str source: URL pointing to an archive file.
41
:param str dest: Local path location to download archive file to.
27
43
# propogate all exceptions
28
44
# URLError, OSError, etc
29
45
proto, netloc, path, params, query, fragment = urlparse.urlparse(source)
51
def install(self, source):
67
# Mandatory file validation via Sha1 or MD5 hashing.
68
def download_and_validate(self, url, hashsum, validate="sha1"):
69
tempfile, headers = urlretrieve(url)
70
check_hash(tempfile, hashsum, validate)
73
def install(self, source, dest=None, checksum=None, hash_type='sha1'):
75
Download and install an archive file, with optional checksum validation.
77
The checksum can also be given on the `source` URL's fragment.
80
handler.install('http://example.com/file.tgz#sha1=deadbeef')
82
:param str source: URL pointing to an archive file.
83
:param str dest: Local destination path to install to. If not given,
84
installs to `$CHARM_DIR/archives/archive_file_name`.
85
:param str checksum: If given, validate the archive file after download.
86
:param str hash_type: Algorithm used to generate `checksum`.
87
Can be any hash alrgorithm supported by :mod:`hashlib`,
88
such as md5, sha1, sha256, sha512, etc.
52
91
url_parts = self.parse_url(source)
53
92
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), 'fetched')
54
93
if not os.path.exists(dest_dir):
60
99
raise UnhandledSource(e.reason)
61
100
except OSError as e:
62
101
raise UnhandledSource(e.strerror)
63
return extract(dld_file)
102
options = urlparse.parse_qs(url_parts.fragment)
103
for key, value in options.items():
104
if key in hashlib.algorithms:
105
check_hash(dld_file, value, key)
107
check_hash(dld_file, checksum, hash_type)
108
return extract(dld_file, dest)