~wuwenbin2/onosfw/neutron-api-onos

« back to all changes in this revision

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

  • Committer: wuwenbin2
  • Date: 2015-12-31 03:27:06 UTC
  • Revision ID: git-v1:5ea6261efb13b069f3fb3fc63b2cfdac128f447f
neutron-api-onos

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, **options):
 
46
    """Install a requirements file """
 
47
    command = ["install"]
 
48
 
 
49
    available_options = ('proxy', 'src', 'log', )
 
50
    for option in parse_options(options, available_options):
 
51
        command.append(option)
 
52
 
 
53
    command.append("-r {0}".format(requirements))
 
54
    log("Installing from file: {} with options: {}".format(requirements,
 
55
                                                           command))
 
56
    pip_execute(command)
 
57
 
 
58
 
 
59
def pip_install(package, fatal=False, upgrade=False, venv=None, **options):
 
60
    """Install a python package"""
 
61
    if venv:
 
62
        venv_python = os.path.join(venv, 'bin/pip')
 
63
        command = [venv_python, "install"]
 
64
    else:
 
65
        command = ["install"]
 
66
 
 
67
    available_options = ('proxy', 'src', 'log', 'index-url', )
 
68
    for option in parse_options(options, available_options):
 
69
        command.append(option)
 
70
 
 
71
    if upgrade:
 
72
        command.append('--upgrade')
 
73
 
 
74
    if isinstance(package, list):
 
75
        command.extend(package)
 
76
    else:
 
77
        command.append(package)
 
78
 
 
79
    log("Installing {} package with options: {}".format(package,
 
80
                                                        command))
 
81
    if venv:
 
82
        subprocess.check_call(command)
 
83
    else:
 
84
        pip_execute(command)
 
85
 
 
86
 
 
87
def pip_uninstall(package, **options):
 
88
    """Uninstall a python package"""
 
89
    command = ["uninstall", "-q", "-y"]
 
90
 
 
91
    available_options = ('proxy', 'log', )
 
92
    for option in parse_options(options, available_options):
 
93
        command.append(option)
 
94
 
 
95
    if isinstance(package, list):
 
96
        command.extend(package)
 
97
    else:
 
98
        command.append(package)
 
99
 
 
100
    log("Uninstalling {} package with options: {}".format(package,
 
101
                                                          command))
 
102
    pip_execute(command)
 
103
 
 
104
 
 
105
def pip_list():
 
106
    """Returns the list of current python installed packages
 
107
    """
 
108
    return pip_execute(["list"])
 
109
 
 
110
 
 
111
def pip_create_virtualenv(path=None):
 
112
    """Create an isolated Python environment."""
 
113
    apt_install('python-virtualenv')
 
114
 
 
115
    if path:
 
116
        venv_path = path
 
117
    else:
 
118
        venv_path = os.path.join(charm_dir(), 'venv')
 
119
 
 
120
    if not os.path.exists(venv_path):
 
121
        subprocess.check_call(['virtualenv', venv_path])