Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add require.nodejs module

  • Loading branch information...
commit b3578e0b4992826edad74f0e9960b044ea1c626a 1 parent 375db72
Ronan Amicel authored
2  docs/api/nodejs.rst
View
@@ -5,3 +5,5 @@
.. automodule:: fabtools.nodejs
:members:
+
+ .. seealso:: :ref:`require_nodejs_module`
1  docs/api/require/index.rst
View
@@ -8,6 +8,7 @@ fabtools.require
files
mysql
nginx
+ nodejs
openvz
postfix
postgres
9 docs/api/require/nodejs.rst
View
@@ -0,0 +1,9 @@
+.. _require_nodejs_module:
+
+:mod:`fabtools.require.nodejs`
+------------------------------
+
+.. automodule:: fabtools.require.nodejs
+ :members:
+
+ .. seealso:: :ref:`nodejs_module`
52 fabtools/nodejs.py
View
@@ -5,12 +5,20 @@
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.
1  fabtools/require/__init__.py
View
@@ -4,6 +4,7 @@
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
62 fabtools/require/nodejs.py
View
@@ -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)
1  tox.ini
View
@@ -11,6 +11,7 @@ commands = {envbindir}/unit2 discover []
deps =
unittest2
mock
+ simplejson
[testenv:py26]
commands = {envbindir}/unit2 discover []
Please sign in to comment.
Something went wrong with that request. Please try again.