3
from charmhelpers.fetch import (
7
from charmhelpers.payload.archive import (
11
from charmhelpers.core.host import mkdir
14
class ArchiveUrlFetchHandler(BaseFetchHandler):
15
"""Handler for archives via generic URLs"""
16
def can_handle(self, source):
17
url_parts = self.parse_url(source)
18
if url_parts.scheme not in ('http', 'https', 'ftp', 'file'):
19
return "Wrong source type"
20
if get_archive_handler(self.base_url(source)):
24
def download(self, source, dest):
25
# propogate all exceptions
26
# URLError, OSError, etc
27
response = urllib2.urlopen(source)
29
with open(dest, 'w') as dest_file:
30
dest_file.write(response.read())
31
except Exception as e:
32
if os.path.isfile(dest):
36
def install(self, source):
37
url_parts = self.parse_url(source)
38
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), 'fetched')
39
if not os.path.exists(dest_dir):
40
mkdir(dest_dir, perms=0755)
41
dld_file = os.path.join(dest_dir, os.path.basename(url_parts.path))
43
self.download(source, dld_file)
44
except urllib2.URLError as e:
45
raise UnhandledSource(e.reason)
47
raise UnhandledSource(e.strerror)
48
return extract(dld_file)