~james-page/charms/trusty/swift-proxy/trunk

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/python/packages.py

  • Committer: James Page
  • Date: 2016-01-19 14:46:01 UTC
  • mfrom: (134.1.1 stable.remote)
  • Revision ID: james.page@ubuntu.com-20160119144601-66bdh4r0va0pn9og
Fix liberty/mitaka typo from previous test definition update batch.

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
import os
 
21
import subprocess
 
22
 
 
23
from charmhelpers.fetch import apt_install, apt_update
 
24
from charmhelpers.core.hookenv import charm_dir, log
 
25
 
 
26
try:
 
27
    from pip import main as pip_execute
 
28
except ImportError:
 
29
    apt_update()
 
30
    apt_install('python-pip')
 
31
    from pip import main as pip_execute
 
32
 
 
33
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
 
34
 
 
35
 
 
36
def parse_options(given, available):
 
37
    """Given a set of options, check if available"""
 
38
    for key, value in sorted(given.items()):
 
39
        if not value:
 
40
            continue
 
41
        if key in available:
 
42
            yield "--{0}={1}".format(key, value)
 
43
 
 
44
 
 
45
def pip_install_requirements(requirements, constraints=None, **options):
 
46
    """Install a requirements file.
 
47
 
 
48
    :param constraints: Path to pip constraints file.
 
49
    http://pip.readthedocs.org/en/stable/user_guide/#constraints-files
 
50
    """
 
51
    command = ["install"]
 
52
 
 
53
    available_options = ('proxy', 'src', 'log', )
 
54
    for option in parse_options(options, available_options):
 
55
        command.append(option)
 
56
 
 
57
    command.append("-r {0}".format(requirements))
 
58
    if constraints:
 
59
        command.append("-c {0}".format(constraints))
 
60
        log("Installing from file: {} with constraints {} "
 
61
            "and options: {}".format(requirements, constraints, command))
 
62
    else:
 
63
        log("Installing from file: {} with options: {}".format(requirements,
 
64
                                                               command))
 
65
    pip_execute(command)
 
66
 
 
67
 
 
68
def pip_install(package, fatal=False, upgrade=False, venv=None, **options):
 
69
    """Install a python package"""
 
70
    if venv:
 
71
        venv_python = os.path.join(venv, 'bin/pip')
 
72
        command = [venv_python, "install"]
 
73
    else:
 
74
        command = ["install"]
 
75
 
 
76
    available_options = ('proxy', 'src', 'log', 'index-url', )
 
77
    for option in parse_options(options, available_options):
 
78
        command.append(option)
 
79
 
 
80
    if upgrade:
 
81
        command.append('--upgrade')
 
82
 
 
83
    if isinstance(package, list):
 
84
        command.extend(package)
 
85
    else:
 
86
        command.append(package)
 
87
 
 
88
    log("Installing {} package with options: {}".format(package,
 
89
                                                        command))
 
90
    if venv:
 
91
        subprocess.check_call(command)
 
92
    else:
 
93
        pip_execute(command)
 
94
 
 
95
 
 
96
def pip_uninstall(package, **options):
 
97
    """Uninstall a python package"""
 
98
    command = ["uninstall", "-q", "-y"]
 
99
 
 
100
    available_options = ('proxy', 'log', )
 
101
    for option in parse_options(options, available_options):
 
102
        command.append(option)
 
103
 
 
104
    if isinstance(package, list):
 
105
        command.extend(package)
 
106
    else:
 
107
        command.append(package)
 
108
 
 
109
    log("Uninstalling {} package with options: {}".format(package,
 
110
                                                          command))
 
111
    pip_execute(command)
 
112
 
 
113
 
 
114
def pip_list():
 
115
    """Returns the list of current python installed packages
 
116
    """
 
117
    return pip_execute(["list"])
 
118
 
 
119
 
 
120
def pip_create_virtualenv(path=None):
 
121
    """Create an isolated Python environment."""
 
122
    apt_install('python-virtualenv')
 
123
 
 
124
    if path:
 
125
        venv_path = path
 
126
    else:
 
127
        venv_path = os.path.join(charm_dir(), 'venv')
 
128
 
 
129
    if not os.path.exists(venv_path):
 
130
        subprocess.check_call(['virtualenv', venv_path])