aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRonan Amicel <ronan.amicel@gmail.com>2012-10-12 18:15:31 +0200
committerRonan Amicel <ronan.amicel@gmail.com>2012-10-13 17:01:49 +0200
commitb3578e0b4992826edad74f0e9960b044ea1c626a (patch)
treeadce987fb5b4d86727b150a9bad8576162d95b24
parentFactor out install_dependencies() in Node.js module (diff)
downloadfabtools-b3578e0b4992826edad74f0e9960b044ea1c626a.tar.xz
Add require.nodejs module
-rw-r--r--docs/api/nodejs.rst2
-rw-r--r--docs/api/require/index.rst1
-rw-r--r--docs/api/require/nodejs.rst9
-rw-r--r--fabtools/nodejs.py52
-rw-r--r--fabtools/require/__init__.py1
-rw-r--r--fabtools/require/nodejs.py62
-rw-r--r--tox.ini1
7 files changed, 126 insertions, 2 deletions
diff --git a/docs/api/nodejs.rst b/docs/api/nodejs.rst
index 01c1654..9517f13 100644
--- a/docs/api/nodejs.rst
+++ b/docs/api/nodejs.rst
@@ -5,3 +5,5 @@
.. automodule:: fabtools.nodejs
:members:
+
+ .. seealso:: :ref:`require_nodejs_module`
diff --git a/docs/api/require/index.rst b/docs/api/require/index.rst
index cd516f0..a077148 100644
--- a/docs/api/require/index.rst
+++ b/docs/api/require/index.rst
@@ -8,6 +8,7 @@ fabtools.require
files
mysql
nginx
+ nodejs
openvz
postfix
postgres
diff --git a/docs/api/require/nodejs.rst b/docs/api/require/nodejs.rst
new file mode 100644
index 0000000..b595247
--- /dev/null
+++ b/docs/api/require/nodejs.rst
@@ -0,0 +1,9 @@
+.. _require_nodejs_module:
+
+:mod:`fabtools.require.nodejs`
+------------------------------
+
+.. automodule:: fabtools.require.nodejs
+ :members:
+
+ .. seealso:: :ref:`nodejs_module`
diff --git a/fabtools/nodejs.py b/fabtools/nodejs.py
index d59a74d..37b2ca6 100644
--- a/fabtools/nodejs.py
+++ b/fabtools/nodejs.py
@@ -5,12 +5,20 @@ Node.js
This module provides tools for installing `Node.js`_ and managing
packages using `npm`_.
+.. note: the ``simplejson`` module is required on Python 2.5
+
.. _Node.js: http://nodejs.org/
.. _npm: http://npmjs.org/
"""
-from fabric.api import run, sudo, cd
-from fabtools import require
+from __future__ import with_statement
+
+try:
+ import json
+except ImportError:
+ import simplejson as json
+
+from fabric.api import run, sudo, cd, settings, hide
DEFAULT_VERSION = '0.8.11'
@@ -30,6 +38,7 @@ def install_from_source(version=DEFAULT_VERSION):
.. note:: This function may not work for old versions of Node.js.
"""
+ from fabtools import require
require.deb.packages([
'build-essential',
'python',
@@ -48,6 +57,20 @@ def install_from_source(version=DEFAULT_VERSION):
run('rm -rf %(filename)s %(foldername)s' % locals())
+def version():
+ """
+ Get the version of Node.js currently installed.
+
+ Returns ``None`` if it is not installed.
+ """
+ with settings(hide('running', 'stdout'), warn_only=True):
+ res = run('/usr/local/bin/node --version')
+ if res.failed:
+ return None
+ else:
+ return res[1:]
+
+
def install_package(package, version=None, local=False):
"""
Install a Node.js package.
@@ -94,6 +117,31 @@ def install_dependencies():
run('npm install')
+def package_version(package, local=False):
+ """
+ Get the installed version of a Node.js package.
+
+ Returns ``None``is the package is not installed. If *local* is
+ ``True``, returns the version of the locally installed package.
+ """
+ options = ['--json true', '--silent']
+ if local:
+ options.append('-l')
+ else:
+ options.append('-g')
+ options = ' '.join(options)
+
+ with hide('running', 'stdout'):
+ res = run('npm list %s' % options)
+
+ dependencies = json.loads(res)['dependencies']
+ pkg_data = dependencies.get(package)
+ if pkg_data:
+ return pkg_data['version']
+ else:
+ return None
+
+
def update_package(package, local=False):
"""
Update a Node.js package.
diff --git a/fabtools/require/__init__.py b/fabtools/require/__init__.py
index 43e576d..2299c26 100644
--- a/fabtools/require/__init__.py
+++ b/fabtools/require/__init__.py
@@ -4,6 +4,7 @@ import fabtools.require.nginx
import fabtools.require.postfix
import fabtools.require.postgres
import fabtools.require.mysql
+import fabtools.require.nodejs
import fabtools.require.openvz
import fabtools.require.python
import fabtools.require.redis
diff --git a/fabtools/require/nodejs.py b/fabtools/require/nodejs.py
new file mode 100644
index 0000000..3003c66
--- /dev/null
+++ b/fabtools/require/nodejs.py
@@ -0,0 +1,62 @@
+"""
+Node.js
+=======
+
+This module provides tools for installing `Node.js`_ and managing
+packages using `npm`_.
+
+.. note: the ``simplejson`` module is required on Python 2.5
+
+.. _Node.js: http://nodejs.org/
+.. _npm: http://npmjs.org/
+
+"""
+
+from fabtools import nodejs
+
+
+def installed_from_source(version=nodejs.DEFAULT_VERSION):
+ """
+ Require Node.js to be installed from source.
+
+ ::
+
+ from fabtools import require
+
+ require.nodejs.installed_from_source()
+
+ """
+ if nodejs.version() != version:
+ nodejs.install_from_source(version)
+
+
+def package(pkg_name, version=None, local=False):
+ """
+ Require a Node.js package.
+
+ If the package is not installed, and no *version* is specified, the
+ latest available version will be installed.
+
+ If a *version* is specified, and a different version of the package
+ is already installed, it will be updated to the specified version.
+
+ If `local` is ``True``, the package will be installed locally.
+
+ ::
+
+ from fabtools import require
+
+ # Install package system-wide
+ require.nodejs.package('foo')
+
+ # Install package locally
+ require.nodejs.package('bar', local=True)
+
+ """
+ pkg_version = nodejs.package_version(pkg_name, local=local)
+ if version:
+ if pkg_version != version:
+ nodejs.install_package(pkg_name, version, local=local)
+ else:
+ if pkg_version is None:
+ nodejs.install_package(pkg_name, local=local)
diff --git a/tox.ini b/tox.ini
index 26737a2..7b9738b 100644
--- a/tox.ini
+++ b/tox.ini
@@ -11,6 +11,7 @@ commands = {envbindir}/unit2 discover []
deps =
unittest2
mock
+ simplejson
[testenv:py26]
commands = {envbindir}/unit2 discover []