~le-charmers/charms/trusty/rabbitmq-server/leadership-election

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-06-04 08:45:11 UTC
  • Revision ID: liam.young@canonical.com-20150604084511-9epl3cdjdh9rueno
Resync le charm helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# You should have received a copy of the GNU Lesser General Public License
18
18
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
19
19
 
 
20
import os
 
21
import subprocess
 
22
 
20
23
from charmhelpers.fetch import apt_install, apt_update
21
 
from charmhelpers.core.hookenv import log
 
24
from charmhelpers.core.hookenv import charm_dir, log
22
25
 
23
26
try:
24
27
    from pip import main as pip_execute
51
54
    pip_execute(command)
52
55
 
53
56
 
54
 
def pip_install(package, fatal=False, upgrade=False, **options):
 
57
def pip_install(package, fatal=False, upgrade=False, venv=None, **options):
55
58
    """Install a python package"""
56
 
    command = ["install"]
 
59
    if venv:
 
60
        venv_python = os.path.join(venv, 'bin/pip')
 
61
        command = [venv_python, "install"]
 
62
    else:
 
63
        command = ["install"]
57
64
 
58
 
    available_options = ('proxy', 'src', 'log', "index-url", )
 
65
    available_options = ('proxy', 'src', 'log', 'index-url', )
59
66
    for option in parse_options(options, available_options):
60
67
        command.append(option)
61
68
 
69
76
 
70
77
    log("Installing {} package with options: {}".format(package,
71
78
                                                        command))
72
 
    pip_execute(command)
 
79
    if venv:
 
80
        subprocess.check_call(command)
 
81
    else:
 
82
        pip_execute(command)
73
83
 
74
84
 
75
85
def pip_uninstall(package, **options):
94
104
    """Returns the list of current python installed packages
95
105
    """
96
106
    return pip_execute(["list"])
 
107
 
 
108
 
 
109
def pip_create_virtualenv(path=None):
 
110
    """Create an isolated Python environment."""
 
111
    apt_install('python-virtualenv')
 
112
 
 
113
    if path:
 
114
        venv_path = path
 
115
    else:
 
116
        venv_path = os.path.join(charm_dir(), 'venv')
 
117
 
 
118
    if not os.path.exists(venv_path):
 
119
        subprocess.check_call(['virtualenv', venv_path])