4
# Copyright 2014-2015 Canonical Limited.
6
# This file is part of charm-helpers.
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.
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.
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/>.
20
from charmhelpers.fetch import apt_install, apt_update
21
from charmhelpers.core.hookenv import log
24
from pip import main as pip_execute
27
apt_install('python-pip')
28
from pip import main as pip_execute
30
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
33
def parse_options(given, available):
34
"""Given a set of options, check if available"""
35
for key, value in sorted(given.items()):
37
yield "--{0}={1}".format(key, value)
40
def pip_install_requirements(requirements, **options):
41
"""Install a requirements file """
44
available_options = ('proxy', 'src', 'log', )
45
for option in parse_options(options, available_options):
46
command.append(option)
48
command.append("-r {0}".format(requirements))
49
log("Installing from file: {} with options: {}".format(requirements,
54
def pip_install(package, fatal=False, upgrade=False, **options):
55
"""Install a python package"""
58
available_options = ('proxy', 'src', 'log', "index-url", )
59
for option in parse_options(options, available_options):
60
command.append(option)
63
command.append('--upgrade')
65
if isinstance(package, list):
66
command.extend(package)
68
command.append(package)
70
log("Installing {} package with options: {}".format(package,
75
def pip_uninstall(package, **options):
76
"""Uninstall a python package"""
77
command = ["uninstall", "-q", "-y"]
79
available_options = ('proxy', 'log', )
80
for option in parse_options(options, available_options):
81
command.append(option)
83
if isinstance(package, list):
84
command.extend(package)
86
command.append(package)
88
log("Uninstalling {} package with options: {}".format(package,
94
"""Returns the list of current python installed packages
96
return pip_execute(["list"])