Skip to content

Commit

Permalink
Refactored Python package management.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnix committed Sep 5, 2011
1 parent 36a0857 commit 5cd4705
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
1 change: 1 addition & 0 deletions fabtools/icanhaz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fabtools.icanhaz.nginx
import fabtools.icanhaz.postfix
import fabtools.icanhaz.postgres
import fabtools.icanhaz.python
import fabtools.icanhaz.service
import fabtools.icanhaz.supervisor
import fabtools.icanhaz.users
Expand Down
21 changes: 21 additions & 0 deletions fabtools/icanhaz/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Idempotent API for managing python packages
"""
from fabtools.python import *


def package(pkg_name, virtualenv=None, use_sudo=False):
"""
I can haz python package
"""
if not is_installed(pkg_name):
install(pkg_name, virtualenv, use_sudo)


def packages(pkg_list, virtualenv=None, use_sudo=False):
"""
I can haz python packages
"""
pkg_list = [pkg for pkg in pkg_list if not is_installed(pkg)]
if pkg_list:
install(pkg_list, virtualenv, use_sudo)
51 changes: 23 additions & 28 deletions fabtools/python.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
"""
Fabric tools for managing Python packages
Fabric tools for managing Python packages using pip
"""
from fabric.api import *


def install_packages(pkg_list, installer=None, upgrade=False, use_sudo=False):
def is_installed(package, virtualenv=None):
"""
Install Python packages
Packages can be installed with pip or easy_install (setuptools/distribute)
Check if a Python package is installed
"""
func = use_sudo and sudo or run
opts = []

# Use pip if possible, or easy_install
if installer is None:
with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
if func('pip -h').succeeded:
installer = 'pip'
elif func('easy_install -h').succeeded:
installer = 'easy_install'
else:
abort("You need 'pip' or 'easy_install' (setuptools/distribute) to install Python packages")
options = []
if virtualenv:
options.append('-E "%s"' % virtualenv)
options = " ".join(options)
with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True):
res = run("pip freeze %(options)s" % locals())
packages = [line.split('==')[0] for line in res.splitlines()]
return (package in packages)

if installer == 'pip':
opts += "--use-mirrors"
if upgrade:
opts += "-U"
for pkg in pkg_list:
func('pip install %s%s' % (' '.join(opts), pkg))

elif installer == 'easy_install':
if upgrade:
opts += "-U"
for pkg in pkg_list:
func('easy_install %s%s' % (' '.join(opts), pkg))
def install(packages, installer=None, upgrade=False, use_sudo=False):
"""
Install Python packages
"""
func = use_sudo and sudo or run
if not isinstance(packages, basestring):
packages = " ".join(packages)
options = []
if upgrade:
options.append("-U")
options = " ".join(options)
func('PIP_USE_MIRRORS=true pip install %(options)s %(packages)s' % locals())

0 comments on commit 5cd4705

Please sign in to comment.