~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

Viewing changes to hooks/charmhelpers-pl/contrib/python/packages.py

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# coding: utf-8
3
 
 
4
 
# Copyright 2014-2015 Canonical Limited.
5
 
#
6
 
# This file is part of charm-helpers.
7
 
#
8
 
# charm-helpers is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU Lesser General Public License version 3 as
10
 
# published by the Free Software Foundation.
11
 
#
12
 
# charm-helpers is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
# GNU Lesser General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU Lesser General Public License
18
 
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
19
 
 
20
 
from charmhelpers.fetch import apt_install, apt_update
21
 
from charmhelpers.core.hookenv import log
22
 
 
23
 
try:
24
 
    from pip import main as pip_execute
25
 
except ImportError:
26
 
    apt_update()
27
 
    apt_install('python-pip')
28
 
    from pip import main as pip_execute
29
 
 
30
 
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
31
 
 
32
 
 
33
 
def parse_options(given, available):
34
 
    """Given a set of options, check if available"""
35
 
    for key, value in sorted(given.items()):
36
 
        if key in available:
37
 
            yield "--{0}={1}".format(key, value)
38
 
 
39
 
 
40
 
def pip_install_requirements(requirements, **options):
41
 
    """Install a requirements file """
42
 
    command = ["install"]
43
 
 
44
 
    available_options = ('proxy', 'src', 'log', )
45
 
    for option in parse_options(options, available_options):
46
 
        command.append(option)
47
 
 
48
 
    command.append("-r {0}".format(requirements))
49
 
    log("Installing from file: {} with options: {}".format(requirements,
50
 
                                                           command))
51
 
    pip_execute(command)
52
 
 
53
 
 
54
 
def pip_install(package, fatal=False, upgrade=False, **options):
55
 
    """Install a python package"""
56
 
    command = ["install"]
57
 
 
58
 
    available_options = ('proxy', 'src', 'log', "index-url", )
59
 
    for option in parse_options(options, available_options):
60
 
        command.append(option)
61
 
 
62
 
    if upgrade:
63
 
        command.append('--upgrade')
64
 
 
65
 
    if isinstance(package, list):
66
 
        command.extend(package)
67
 
    else:
68
 
        command.append(package)
69
 
 
70
 
    log("Installing {} package with options: {}".format(package,
71
 
                                                        command))
72
 
    pip_execute(command)
73
 
 
74
 
 
75
 
def pip_uninstall(package, **options):
76
 
    """Uninstall a python package"""
77
 
    command = ["uninstall", "-q", "-y"]
78
 
 
79
 
    available_options = ('proxy', 'log', )
80
 
    for option in parse_options(options, available_options):
81
 
        command.append(option)
82
 
 
83
 
    if isinstance(package, list):
84
 
        command.extend(package)
85
 
    else:
86
 
        command.append(package)
87
 
 
88
 
    log("Uninstalling {} package with options: {}".format(package,
89
 
                                                          command))
90
 
    pip_execute(command)
91
 
 
92
 
 
93
 
def pip_list():
94
 
    """Returns the list of current python installed packages
95
 
    """
96
 
    return pip_execute(["list"])