5
from charmhelpers.core import (
10
class ArchiveError(Exception):
13
def get_archive_handler(archive_name):
14
if os.path.isfile(archive_name):
15
if tarfile.is_tarfile(archive_name):
16
return extract_tarfile
17
elif zipfile.is_zipfile(archive_name):
18
return extract_zipfile
20
# look at the file name
21
for ext in ('.tar.gz', '.tgz', 'tar.bz2', '.tbz2', '.tbz'):
22
if archive_name.endswith(ext):
23
return extract_tarfile
24
for ext in ('.zip','.jar'):
25
if archive_name.endswith(ext):
26
return extract_zipfile
28
def archive_dest_default(archive_name):
29
return os.path.join(hookenv.charm_dir(), "archives", archive_name)
32
def extract(archive_name, destpath=None):
33
handler = get_archive_handler(archive_name)
36
destpath = archive_dest_default(archive_name)
37
if not os.path.isdir(destpath):
39
get_archive_handler(archive_name)(archive_name, destpath)
41
raise ArchiveError("No handler for archive")
44
def extract_archive(archive_class, archive_name, destpath):
45
archive = archive_class(archive_name)
46
archive.extract_all(destpath)
49
def extract_tarfile(archive_name, destpath):
50
"Unpack a tar archive, optionally compressed"
51
extract_archive(tarfile.TarFile, archive_name, destpath)
54
def extract_zipfile(archive_name, destpath):
56
extract_archive(zipfile.ZipFile, archive_name, destpath)