~openstack-charmers/charms/precise/cinder/active-active

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2014-03-27 12:26:49 UTC
  • mfrom: (27.2.5 cinder)
  • Revision ID: edward.hope-morley@canonical.com-20140327122649-n02r4o447bcbwapd
[hopem] updated from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import urllib2
 
3
import urlparse
 
4
 
3
5
from charmhelpers.fetch import (
4
6
    BaseFetchHandler,
5
7
    UnhandledSource
24
26
    def download(self, source, dest):
25
27
        # propogate all exceptions
26
28
        # URLError, OSError, etc
 
29
        proto, netloc, path, params, query, fragment = urlparse.urlparse(source)
 
30
        if proto in ('http', 'https'):
 
31
            auth, barehost = urllib2.splituser(netloc)
 
32
            if auth is not None:
 
33
                source = urlparse.urlunparse((proto, barehost, path, params, query, fragment))
 
34
                username, password = urllib2.splitpasswd(auth)
 
35
                passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
 
36
                # Realm is set to None in add_password to force the username and password
 
37
                # to be used whatever the realm
 
38
                passman.add_password(None, source, username, password)
 
39
                authhandler = urllib2.HTTPBasicAuthHandler(passman)
 
40
                opener = urllib2.build_opener(authhandler)
 
41
                urllib2.install_opener(opener)
27
42
        response = urllib2.urlopen(source)
28
43
        try:
29
44
            with open(dest, 'w') as dest_file: