~gnuoy/charms/trusty/percona-cluster/bug-1389670

« back to all changes in this revision

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

  • Committer: james.page at ubuntu
  • Date: 2014-06-23 09:47:35 UTC
  • Revision ID: james.page@ubuntu.com-20140623094735-qlvyu1yjvcx7wsl5
[trivial] Resync helpers

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: