~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

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

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

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
33
36
def parse_options(given, available):
34
37
    """Given a set of options, check if available"""
35
38
    for key, value in sorted(given.items()):
 
39
        if not value:
 
40
            continue
36
41
        if key in available:
37
42
            yield "--{0}={1}".format(key, value)
38
43
 
51
56
    pip_execute(command)
52
57
 
53
58
 
54
 
def pip_install(package, fatal=False, upgrade=False, **options):
 
59
def pip_install(package, fatal=False, upgrade=False, venv=None, **options):
55
60
    """Install a python package"""
56
 
    command = ["install"]
 
61
    if venv:
 
62
        venv_python = os.path.join(venv, 'bin/pip')
 
63
        command = [venv_python, "install"]
 
64
    else:
 
65
        command = ["install"]
57
66
 
58
 
    available_options = ('proxy', 'src', 'log', "index-url", )
 
67
    available_options = ('proxy', 'src', 'log', 'index-url', )
59
68
    for option in parse_options(options, available_options):
60
69
        command.append(option)
61
70
 
69
78
 
70
79
    log("Installing {} package with options: {}".format(package,
71
80
                                                        command))
72
 
    pip_execute(command)
 
81
    if venv:
 
82
        subprocess.check_call(command)
 
83
    else:
 
84
        pip_execute(command)
73
85
 
74
86
 
75
87
def pip_uninstall(package, **options):
94
106
    """Returns the list of current python installed packages
95
107
    """
96
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])