~james-page/charms/trusty/neutron-gateway/mitaka-pt-2

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/payload/archive.py

  • Committer: Corey Bryant
  • Date: 2016-01-11 20:29:46 UTC
  • Revision ID: corey.bryant@canonical.com-20160111202946-y7q2bt62okzg10wv
[corey.bryant,r=trivial] Add hooks/charmhelpers/payload/archive.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import os
 
18
import tarfile
 
19
import zipfile
 
20
from charmhelpers.core import (
 
21
    host,
 
22
    hookenv,
 
23
)
 
24
 
 
25
 
 
26
class ArchiveError(Exception):
 
27
    pass
 
28
 
 
29
 
 
30
def get_archive_handler(archive_name):
 
31
    if os.path.isfile(archive_name):
 
32
        if tarfile.is_tarfile(archive_name):
 
33
            return extract_tarfile
 
34
        elif zipfile.is_zipfile(archive_name):
 
35
            return extract_zipfile
 
36
    else:
 
37
        # look at the file name
 
38
        for ext in ('.tar', '.tar.gz', '.tgz', 'tar.bz2', '.tbz2', '.tbz'):
 
39
            if archive_name.endswith(ext):
 
40
                return extract_tarfile
 
41
        for ext in ('.zip', '.jar'):
 
42
            if archive_name.endswith(ext):
 
43
                return extract_zipfile
 
44
 
 
45
 
 
46
def archive_dest_default(archive_name):
 
47
    archive_file = os.path.basename(archive_name)
 
48
    return os.path.join(hookenv.charm_dir(), "archives", archive_file)
 
49
 
 
50
 
 
51
def extract(archive_name, destpath=None):
 
52
    handler = get_archive_handler(archive_name)
 
53
    if handler:
 
54
        if not destpath:
 
55
            destpath = archive_dest_default(archive_name)
 
56
        if not os.path.isdir(destpath):
 
57
            host.mkdir(destpath)
 
58
        handler(archive_name, destpath)
 
59
        return destpath
 
60
    else:
 
61
        raise ArchiveError("No handler for archive")
 
62
 
 
63
 
 
64
def extract_tarfile(archive_name, destpath):
 
65
    "Unpack a tar archive, optionally compressed"
 
66
    archive = tarfile.open(archive_name)
 
67
    archive.extractall(destpath)
 
68
 
 
69
 
 
70
def extract_zipfile(archive_name, destpath):
 
71
    "Unpack a zip file"
 
72
    archive = zipfile.ZipFile(archive_name)
 
73
    archive.extractall(destpath)