-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Python package management.
- Loading branch information
Showing
3 changed files
with
45 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |