~james-page/charms/trusty/nova-compute/hook-optimize

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/fetch/archiveurl.py

  • Committer: james.page at ubuntu
  • Date: 2014-10-01 21:17:11 UTC
  • mfrom: (79.2.21 ipv6)
  • Revision ID: james.page@ubuntu.com-20141001211711-eatkfiaqlaed6t79
[xianghui,dosaboy,r=james-page,t=gema] Add IPv6 support using prefer-ipv6 flag

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import urllib2
 
3
from urllib import urlretrieve
3
4
import urlparse
 
5
import hashlib
4
6
 
5
7
from charmhelpers.fetch import (
6
8
    BaseFetchHandler,
10
12
    get_archive_handler,
11
13
    extract,
12
14
)
13
 
from charmhelpers.core.host import mkdir
 
15
from charmhelpers.core.host import mkdir, check_hash
14
16
 
15
17
 
16
18
class ArchiveUrlFetchHandler(BaseFetchHandler):
17
 
    """Handler for archives via generic URLs"""
 
19
    """
 
20
    Handler to download archive files from arbitrary URLs.
 
21
 
 
22
    Can fetch from http, https, ftp, and file URLs.
 
23
 
 
24
    Can install either tarballs (.tar, .tgz, .tbz2, etc) or zip files.
 
25
 
 
26
    Installs the contents of the archive in $CHARM_DIR/fetched/.
 
27
    """
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'):
24
34
        return False
25
35
 
26
36
    def download(self, source, dest):
 
37
        """
 
38
        Download an archive file.
 
39
 
 
40
        :param str source: URL pointing to an archive file.
 
41
        :param str dest: Local path location to download archive file to.
 
42
        """
27
43
        # propogate all exceptions
28
44
        # URLError, OSError, etc
29
45
        proto, netloc, path, params, query, fragment = urlparse.urlparse(source)
48
64
                os.unlink(dest)
49
65
            raise e
50
66
 
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)
 
71
        return tempfile
 
72
 
 
73
    def install(self, source, dest=None, checksum=None, hash_type='sha1'):
 
74
        """
 
75
        Download and install an archive file, with optional checksum validation.
 
76
 
 
77
        The checksum can also be given on the `source` URL's fragment.
 
78
        For example::
 
79
 
 
80
            handler.install('http://example.com/file.tgz#sha1=deadbeef')
 
81
 
 
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.
 
89
 
 
90
        """
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)
 
106
        if checksum:
 
107
            check_hash(dld_file, checksum, hash_type)
 
108
        return extract(dld_file, dest)