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/>.
23
from charmhelpers.fetch import apt_install, apt_update
24
from charmhelpers.core.hookenv import charm_dir, log
27
from pip import main as pip_execute
30
apt_install('python-pip')
31
from pip import main as pip_execute
33
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
36
def parse_options(given, available):
37
"""Given a set of options, check if available"""
38
for key, value in sorted(given.items()):
42
yield "--{0}={1}".format(key, value)
45
def pip_install_requirements(requirements, **options):
46
"""Install a requirements file """
49
available_options = ('proxy', 'src', 'log', )
50
for option in parse_options(options, available_options):
51
command.append(option)
53
command.append("-r {0}".format(requirements))
54
log("Installing from file: {} with options: {}".format(requirements,
59
def pip_install(package, fatal=False, upgrade=False, venv=None, **options):
60
"""Install a python package"""
62
venv_python = os.path.join(venv, 'bin/pip')
63
command = [venv_python, "install"]
67
available_options = ('proxy', 'src', 'log', 'index-url', )
68
for option in parse_options(options, available_options):
69
command.append(option)
72
command.append('--upgrade')
74
if isinstance(package, list):
75
command.extend(package)
77
command.append(package)
79
log("Installing {} package with options: {}".format(package,
82
subprocess.check_call(command)
87
def pip_uninstall(package, **options):
88
"""Uninstall a python package"""
89
command = ["uninstall", "-q", "-y"]
91
available_options = ('proxy', 'log', )
92
for option in parse_options(options, available_options):
93
command.append(option)
95
if isinstance(package, list):
96
command.extend(package)
98
command.append(package)
100
log("Uninstalling {} package with options: {}".format(package,
106
"""Returns the list of current python installed packages
108
return pip_execute(["list"])
111
def pip_create_virtualenv(path=None):
112
"""Create an isolated Python environment."""
113
apt_install('python-virtualenv')
118
venv_path = os.path.join(charm_dir(), 'venv')
120
if not os.path.exists(venv_path):
121
subprocess.check_call(['virtualenv', venv_path])