~gnuoy/charms/trusty/neutron-openvswitch/neutron-contexts

« back to all changes in this revision

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

  • Committer: james.page at ubuntu
  • Date: 2014-12-15 09:12:45 UTC
  • mfrom: (36.1.8 neutron-openvswitch)
  • Revision ID: james.page@ubuntu.com-20141215091245-jm3e66yel69u7zbh
[corey.bryant,r=james-page] Sort out charmhelpers issues.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# coding: utf-8
 
3
 
 
4
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
 
5
 
 
6
from charmhelpers.fetch import apt_install, apt_update
 
7
from charmhelpers.core.hookenv import log
 
8
 
 
9
try:
 
10
    from pip import main as pip_execute
 
11
except ImportError:
 
12
    apt_update()
 
13
    apt_install('python-pip')
 
14
    from pip import main as pip_execute
 
15
 
 
16
 
 
17
def parse_options(given, available):
 
18
    """Given a set of options, check if available"""
 
19
    for key, value in sorted(given.items()):
 
20
        if key in available:
 
21
            yield "--{0}={1}".format(key, value)
 
22
 
 
23
 
 
24
def pip_install_requirements(requirements, **options):
 
25
    """Install a requirements file """
 
26
    command = ["install"]
 
27
 
 
28
    available_options = ('proxy', 'src', 'log', )
 
29
    for option in parse_options(options, available_options):
 
30
        command.append(option)
 
31
 
 
32
    command.append("-r {0}".format(requirements))
 
33
    log("Installing from file: {} with options: {}".format(requirements,
 
34
                                                           command))
 
35
    pip_execute(command)
 
36
 
 
37
 
 
38
def pip_install(package, fatal=False, **options):
 
39
    """Install a python package"""
 
40
    command = ["install"]
 
41
 
 
42
    available_options = ('proxy', 'src', 'log', "index-url", )
 
43
    for option in parse_options(options, available_options):
 
44
        command.append(option)
 
45
 
 
46
    if isinstance(package, list):
 
47
        command.extend(package)
 
48
    else:
 
49
        command.append(package)
 
50
 
 
51
    log("Installing {} package with options: {}".format(package,
 
52
                                                        command))
 
53
    pip_execute(command)
 
54
 
 
55
 
 
56
def pip_uninstall(package, **options):
 
57
    """Uninstall a python package"""
 
58
    command = ["uninstall", "-q", "-y"]
 
59
 
 
60
    available_options = ('proxy', 'log', )
 
61
    for option in parse_options(options, available_options):
 
62
        command.append(option)
 
63
 
 
64
    if isinstance(package, list):
 
65
        command.extend(package)
 
66
    else:
 
67
        command.append(package)
 
68
 
 
69
    log("Uninstalling {} package with options: {}".format(package,
 
70
                                                          command))
 
71
    pip_execute(command)
 
72
 
 
73
 
 
74
def pip_list():
 
75
    """Returns the list of current python installed packages
 
76
    """
 
77
    return pip_execute(["list"])