Skip to content

Commit

Permalink
virtualenv support for pkg_pip
Browse files Browse the repository at this point in the history
untested, closes #148
  • Loading branch information
trehn committed Feb 19, 2015
1 parent 2d4554c commit 503d01c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 4 additions & 1 deletion doc/item_pkg_pip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ Handles Python packages installed by ``pip``.
.. code-block:: python
pkg_pip = {
"foopkg": {
"foo": {
"installed": True, # default
"version": "1.0", # optional
},
"bar": {
"installed": False,
},
"/path/to/virtualenv/foo": {
# will install foo in the virtualenv at /path/to/virtualenv
},
}
Attribute reference
Expand Down
20 changes: 14 additions & 6 deletions src/bundlewrap/items/pkg_pip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from os.path import join, split
from pipes import quote

from bundlewrap.exceptions import BundleError
Expand All @@ -12,15 +13,15 @@

def pkg_install(node, pkgname, version=None):
if version:
pkg = "{}=={}".format(pkgname, version)
else:
pkg = pkgname
return node.run("pip install -U {}".format(quote(pkg)))
pkgname = "{}=={}".format(pkgname, version)
pip_path, pkgname = split_path(pkgname)
return node.run("{} install -U {}".format(quote(pip_path), quote(pkgname)))


def pkg_installed(node, pkgname):
pip_path, pkgname = split_path(pkgname)
result = node.run(
"pip freeze | grep '^{}=='".format(pkgname),
"{} freeze | grep '^{}=='".format(quote(pip_path), pkgname),
may_fail=True,
)
if result.return_code != 0:
Expand All @@ -30,7 +31,8 @@ def pkg_installed(node, pkgname):


def pkg_remove(node, pkgname):
return node.run("pip uninstall -y {}".format(quote(pkgname)))
pip_path, pkgname = split_path(pkgname)
return node.run("{} uninstall -y {}".format(quote(pip_path), quote(pkgname)))


class PipPkg(Item):
Expand Down Expand Up @@ -110,3 +112,9 @@ def validate_attributes(cls, bundle, item_id, attributes):
bundle=bundle.name,
item=item_id,
))


def split_path(pkgname):
virtualenv, pkgname = split(pkgname)
pip_path = join(virtualenv, "bin", "pip") if virtualenv else "pip"
return pip_path, pkgname

0 comments on commit 503d01c

Please sign in to comment.