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, constraints=None, **options):
46
"""Install a requirements file.
48
:param constraints: Path to pip constraints file.
49
http://pip.readthedocs.org/en/stable/user_guide/#constraints-files
53
available_options = ('proxy', 'src', 'log', )
54
for option in parse_options(options, available_options):
55
command.append(option)
57
command.append("-r {0}".format(requirements))
59
command.append("-c {0}".format(constraints))
60
log("Installing from file: {} with constraints {} "
61
"and options: {}".format(requirements, constraints, command))
63
log("Installing from file: {} with options: {}".format(requirements,
68
def pip_install(package, fatal=False, upgrade=False, venv=None, **options):
69
"""Install a python package"""
71
venv_python = os.path.join(venv, 'bin/pip')
72
command = [venv_python, "install"]
76
available_options = ('proxy', 'src', 'log', 'index-url', )
77
for option in parse_options(options, available_options):
78
command.append(option)
81
command.append('--upgrade')
83
if isinstance(package, list):
84
command.extend(package)
86
command.append(package)
88
log("Installing {} package with options: {}".format(package,
91
subprocess.check_call(command)
96
def pip_uninstall(package, **options):
97
"""Uninstall a python package"""
98
command = ["uninstall", "-q", "-y"]
100
available_options = ('proxy', 'log', )
101
for option in parse_options(options, available_options):
102
command.append(option)
104
if isinstance(package, list):
105
command.extend(package)
107
command.append(package)
109
log("Uninstalling {} package with options: {}".format(package,
115
"""Returns the list of current python installed packages
117
return pip_execute(["list"])
120
def pip_create_virtualenv(path=None):
121
"""Create an isolated Python environment."""
122
apt_install('python-virtualenv')
127
venv_path = os.path.join(charm_dir(), 'venv')
129
if not os.path.exists(venv_path):
130
subprocess.check_call(['virtualenv', venv_path])