~openstack-charmers-next/charms/vivid/hacluster/trunk

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2016-03-30 09:07:46 UTC
  • mfrom: (63.1.4 trunk)
  • Revision ID: liam.young@canonical.com-20160330090746-rwwe91yjqi9j9ry3
[gnuoy, r=james-page] Add pause/resume actions

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import os
21
21
import subprocess
 
22
import sys
22
23
 
23
24
from charmhelpers.fetch import apt_install, apt_update
24
25
from charmhelpers.core.hookenv import charm_dir, log
25
26
 
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
27
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
34
28
 
35
29
 
 
30
def pip_execute(*args, **kwargs):
 
31
    """Overriden pip_execute() to stop sys.path being changed.
 
32
 
 
33
    The act of importing main from the pip module seems to cause add wheels
 
34
    from the /usr/share/python-wheels which are installed by various tools.
 
35
    This function ensures that sys.path remains the same after the call is
 
36
    executed.
 
37
    """
 
38
    try:
 
39
        _path = sys.path
 
40
        try:
 
41
            from pip import main as _pip_execute
 
42
        except ImportError:
 
43
            apt_update()
 
44
            apt_install('python-pip')
 
45
            from pip import main as _pip_execute
 
46
        _pip_execute(*args, **kwargs)
 
47
    finally:
 
48
        sys.path = _path
 
49
 
 
50
 
36
51
def parse_options(given, available):
37
52
    """Given a set of options, check if available"""
38
53
    for key, value in sorted(given.items()):
42
57
            yield "--{0}={1}".format(key, value)
43
58
 
44
59
 
45
 
def pip_install_requirements(requirements, **options):
46
 
    """Install a requirements file """
 
60
def pip_install_requirements(requirements, constraints=None, **options):
 
61
    """Install a requirements file.
 
62
 
 
63
    :param constraints: Path to pip constraints file.
 
64
    http://pip.readthedocs.org/en/stable/user_guide/#constraints-files
 
65
    """
47
66
    command = ["install"]
48
67
 
49
68
    available_options = ('proxy', 'src', 'log', )
51
70
        command.append(option)
52
71
 
53
72
    command.append("-r {0}".format(requirements))
54
 
    log("Installing from file: {} with options: {}".format(requirements,
55
 
                                                           command))
 
73
    if constraints:
 
74
        command.append("-c {0}".format(constraints))
 
75
        log("Installing from file: {} with constraints {} "
 
76
            "and options: {}".format(requirements, constraints, command))
 
77
    else:
 
78
        log("Installing from file: {} with options: {}".format(requirements,
 
79
                                                               command))
56
80
    pip_execute(command)
57
81
 
58
82