Skip to content

Commit b3578e0

Browse files
committed
Add require.nodejs module
1 parent 375db72 commit b3578e0

File tree

7 files changed

+126
-2
lines changed

7 files changed

+126
-2
lines changed

docs/api/nodejs.rst

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55

66
.. automodule:: fabtools.nodejs
77
:members:
8+
9+
.. seealso:: :ref:`require_nodejs_module`

docs/api/require/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fabtools.require
88
files
99
mysql
1010
nginx
11+
nodejs
1112
openvz
1213
postfix
1314
postgres

docs/api/require/nodejs.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. _require_nodejs_module:
2+
3+
:mod:`fabtools.require.nodejs`
4+
------------------------------
5+
6+
.. automodule:: fabtools.require.nodejs
7+
:members:
8+
9+
.. seealso:: :ref:`nodejs_module`

fabtools/nodejs.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
This module provides tools for installing `Node.js`_ and managing
66
packages using `npm`_.
77
8+
.. note: the ``simplejson`` module is required on Python 2.5
9+
810
.. _Node.js: http://nodejs.org/
911
.. _npm: http://npmjs.org/
1012
1113
"""
12-
from fabric.api import run, sudo, cd
13-
from fabtools import require
14+
from __future__ import with_statement
15+
16+
try:
17+
import json
18+
except ImportError:
19+
import simplejson as json
20+
21+
from fabric.api import run, sudo, cd, settings, hide
1422

1523

1624
DEFAULT_VERSION = '0.8.11'
@@ -30,6 +38,7 @@ def install_from_source(version=DEFAULT_VERSION):
3038
.. note:: This function may not work for old versions of Node.js.
3139
3240
"""
41+
from fabtools import require
3342
require.deb.packages([
3443
'build-essential',
3544
'python',
@@ -48,6 +57,20 @@ def install_from_source(version=DEFAULT_VERSION):
4857
run('rm -rf %(filename)s %(foldername)s' % locals())
4958

5059

60+
def version():
61+
"""
62+
Get the version of Node.js currently installed.
63+
64+
Returns ``None`` if it is not installed.
65+
"""
66+
with settings(hide('running', 'stdout'), warn_only=True):
67+
res = run('/usr/local/bin/node --version')
68+
if res.failed:
69+
return None
70+
else:
71+
return res[1:]
72+
73+
5174
def install_package(package, version=None, local=False):
5275
"""
5376
Install a Node.js package.
@@ -94,6 +117,31 @@ def install_dependencies():
94117
run('npm install')
95118

96119

120+
def package_version(package, local=False):
121+
"""
122+
Get the installed version of a Node.js package.
123+
124+
Returns ``None``is the package is not installed. If *local* is
125+
``True``, returns the version of the locally installed package.
126+
"""
127+
options = ['--json true', '--silent']
128+
if local:
129+
options.append('-l')
130+
else:
131+
options.append('-g')
132+
options = ' '.join(options)
133+
134+
with hide('running', 'stdout'):
135+
res = run('npm list %s' % options)
136+
137+
dependencies = json.loads(res)['dependencies']
138+
pkg_data = dependencies.get(package)
139+
if pkg_data:
140+
return pkg_data['version']
141+
else:
142+
return None
143+
144+
97145
def update_package(package, local=False):
98146
"""
99147
Update a Node.js package.

fabtools/require/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import fabtools.require.postfix
55
import fabtools.require.postgres
66
import fabtools.require.mysql
7+
import fabtools.require.nodejs
78
import fabtools.require.openvz
89
import fabtools.require.python
910
import fabtools.require.redis

fabtools/require/nodejs.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Node.js
3+
=======
4+
5+
This module provides tools for installing `Node.js`_ and managing
6+
packages using `npm`_.
7+
8+
.. note: the ``simplejson`` module is required on Python 2.5
9+
10+
.. _Node.js: http://nodejs.org/
11+
.. _npm: http://npmjs.org/
12+
13+
"""
14+
15+
from fabtools import nodejs
16+
17+
18+
def installed_from_source(version=nodejs.DEFAULT_VERSION):
19+
"""
20+
Require Node.js to be installed from source.
21+
22+
::
23+
24+
from fabtools import require
25+
26+
require.nodejs.installed_from_source()
27+
28+
"""
29+
if nodejs.version() != version:
30+
nodejs.install_from_source(version)
31+
32+
33+
def package(pkg_name, version=None, local=False):
34+
"""
35+
Require a Node.js package.
36+
37+
If the package is not installed, and no *version* is specified, the
38+
latest available version will be installed.
39+
40+
If a *version* is specified, and a different version of the package
41+
is already installed, it will be updated to the specified version.
42+
43+
If `local` is ``True``, the package will be installed locally.
44+
45+
::
46+
47+
from fabtools import require
48+
49+
# Install package system-wide
50+
require.nodejs.package('foo')
51+
52+
# Install package locally
53+
require.nodejs.package('bar', local=True)
54+
55+
"""
56+
pkg_version = nodejs.package_version(pkg_name, local=local)
57+
if version:
58+
if pkg_version != version:
59+
nodejs.install_package(pkg_name, version, local=local)
60+
else:
61+
if pkg_version is None:
62+
nodejs.install_package(pkg_name, local=local)

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ commands = {envbindir}/unit2 discover []
1111
deps =
1212
unittest2
1313
mock
14+
simplejson
1415

1516
[testenv:py26]
1617
commands = {envbindir}/unit2 discover []

0 commit comments

Comments
 (0)