diff --git a/nodejs-symlink-deps b/nodejs-symlink-deps new file mode 100755 index 0000000..4850cab --- /dev/null +++ b/nodejs-symlink-deps @@ -0,0 +1,43 @@ +#!/usr/bin/python + +"""Symlink a node module's dependencies into the node_modules directory so users +can `npm link` RPM-installed modules into their personal projects.""" + +# Copyright 2012 T.C. Hollingsworth +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +import json +import os +import sys + +#the %nodejs_symlink_deps macro passes %nodejs_sitelib as the first argument +sitelib = sys.argv[1] + +try: + os.mkdir('node_modules') +except OSError: + pass + +metadata = json.load(open('package.json')) + +os.chdir('node_modules') + +for dep in metadata['dependencies'].iterkeys(): + os.symlink(os.path.join(sitelib, dep), dep) diff --git a/nodejs.attr b/nodejs.attr new file mode 100644 index 0000000..ffeb484 --- /dev/null +++ b/nodejs.attr @@ -0,0 +1,3 @@ +%__nodejs_provides %{_rpmconfigdir}/nodejs.prov +%__nodejs_requires %{_rpmconfigdir}/nodejs.req +%__nodejs_path ^/usr/lib.*/node_modules/.*/package\.json$ diff --git a/nodejs.prov b/nodejs.prov new file mode 100755 index 0000000..7c8dac2 --- /dev/null +++ b/nodejs.prov @@ -0,0 +1,45 @@ +#!/usr/bin/python + +""" +Automatic provides generator for Node.js libraries. + +Taken from package.json. See `man npm-json` for details. +""" +# Copyright 2012 T.C. Hollingsworth +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +import json +import sys + +paths = [path.rstrip() for path in sys.stdin.readlines()] + +for path in paths: + if path.endswith('package.json'): + fh = open(path) + metadata = json.load(fh) + fh.close() + + if 'name' in metadata and not ('private' in metadata and metadata['private']): + print 'npm(' + metadata['name'] + ')', + + if 'version' in metadata: + print '= ' + metadata['version'] + else: + print diff --git a/nodejs.req b/nodejs.req new file mode 100755 index 0000000..71c3cc6 --- /dev/null +++ b/nodejs.req @@ -0,0 +1,152 @@ +#!/usr/bin/python + +""" +Automatic dependency generator for Node.js libraries. + +Parsed from package.json. See `man npm-json` for details. +""" + +# Copyright 2012 T.C. Hollingsworth +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +from __future__ import unicode_literals +import json +import re +import sys + +RE_VERSION = re.compile(r'\s*v?([<>=~]{0,2})\s*([0-9][0-9\.\-]*)\s*') + +def main(): + #npm2rpm uses functions here to write BuildRequires so don't print anything + #until the very end + deps = [] + + #it's highly unlikely that we'll ever get more than one file but we handle + #this like all RPM automatic dependency generation scripts anyway + paths = [path.rstrip() for path in sys.stdin.readlines()] + + for path in paths: + if path.endswith('package.json'): + fh = open(path) + metadata = json.load(fh) + fh.close() + + #write out the node.js interpreter dependency + req = 'nodejs(engine)' + + if 'engines' in metadata and 'node' in metadata['engines']: + deps += process_dep(req, metadata['engines']['node']) + else: + print req + + if 'dependencies' in metadata: + for name, version in metadata['dependencies'].iteritems(): + req = 'npm(' + name + ')' + deps += process_dep(req, version) + + print '\n'.join(deps) + +def process_dep(req, version): + """Converts an individual npm dependency into RPM dependencies""" + + deps = [] + + #there's no way RPM can do anything like an OR dependency + if '||' in version: + sys.stderr.write("WARNING: The {0} dependency contains an ".format(req) + + "OR (||) dependency: '{0}. Please manually include ".format(version) + + "a versioned dependency in your spec file if necessary") + deps.append(req) + + elif ' - ' in version: + gt, lt = version.split(' - ') + deps.append(req + ' >= ' + gt) + deps.append(req + ' <= ' + lt) + + else: + m = re.match(RE_VERSION, version) + + if m: + deps += convert_dep(req, m.group(1), m.group(2)) + + #There could be up to two versions here (e.g.">1.0 <3.1") + if len(version) > m.end(): + m = re.match(RE_VERSION, version[m.end():]) + + if m: + deps += convert_dep(req, m.group(1), m.group(2)) + else: + #uh oh! + sys.stderr.write('WARNING: the automatic dependency generator ' + + 'couldn\'t parse the entry for {0}. '.format(req) + + 'Please check to see if the package.json is valid. If so, file ' + + 'a bug against the nodejs package in bugzilla.') + deps.append(req) + + return deps + +def convert_dep(req, operator, version): + """Converts one of the two possibly listed versions into an RPM dependency""" + + deps = [] + + #any version will do + if not version or version == '*': + deps.append(req) + + #any prefix but ~ makes things dead simple + elif operator in ['>', '<', '<=', '>=', '=']: + deps.append(' '.join([req, operator, version])) + + #oh boy, here we go... + else: + #split the dotted portions into a list (handling trailing dots properly) + parts = [part if part else 'x' for part in version.split('.')] + parts = [int(part) if part != 'x' and not '-' in part + else part for part in parts] + + # 1 or 1.x or 1.x.x or ~1 + if len(parts) == 1 or parts[1] == 'x': + deps.append('{0} >= {1}'.format(req, parts[0])) + deps.append('{0} < {1}'.format(req, parts[0]+1)) + + # 1.2.3 or 1.2.3-4 or 1.2.x or ~1.2.3 or 1.2 + elif len(parts) == 3 or operator != '~': + # 1.2.x or 1.2 + if len(parts) == 2 or parts[2] == 'x': + deps.append('{0} >= {1}.{2}'.format(req, parts[0], parts[1])) + deps.append('{0} < {1}.{2}'.format(req, parts[0], parts[1]+1)) + # ~1.2.3 + elif operator == '~': + deps.append('{0} >= {1}'.format(req, version)) + deps.append('{0} < {1}.{2}'.format(req, parts[0], parts[1]+1)) + # 1.2.3 or 1.2.3-4 + else: + deps.append('{0} = {1}'.format(req, version)) + + # ~1.2 + else: + deps.append('{0} >= {1}'.format(req, version)) + deps.append('{0} < {1}'.format(req, parts[0]+1)) + + return deps + +if __name__ == '__main__': + main() diff --git a/nodejs.spec b/nodejs.spec index 7771a8d..2eb1c62 100644 --- a/nodejs.spec +++ b/nodejs.spec @@ -6,6 +6,11 @@ License: MIT and ASL 2.0 and ISC and BSD Group: Development/Languages URL: http://nodejs.org/ Source0: http://nodejs.org/dist/v%{version}/node-v%{version}.tar.gz +Source1: macros.nodejs +Source2: nodejs.attr +Source3: nodejs.prov +Source4: nodejs.req +Source5: nodejs-symlink-deps BuildRequires: v8-devel BuildRequires: http-parser-devel >= 2.0 BuildRequires: libuv-devel @@ -98,6 +103,16 @@ rm -rf %{buildroot}/%{_prefix}/lib/dtrace # Set the binary permissions properly chmod 0755 %{buildroot}/%{_bindir}/node +# own the sitelib directory +mkdir -p %{buildroot}%{_prefix}/lib/node_modules + +# install rpm magic +install -Dpm0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/rpm/macros.nodejs +install -Dpm0644 %{SOURCE2} %{buildroot}%{_rpmconfigdir}/fileattrs/nodejs.attr +install -pm0755 %{SOURCE3} %{buildroot}%{_rpmconfigdir}/nodejs.prov +install -pm0755 %{SOURCE4} %{buildroot}%{_rpmconfigdir}/nodejs.req +install -pm0755 %{SOURCE5} %{buildroot}%{_rpmconfigdir}/nodejs-symlink-deps + #install documentation mkdir -p %{buildroot}%{_defaultdocdir}/%{name}-doc-%{version}/html cp -pr doc/* %{buildroot}%{_defaultdocdir}/%{name}-doc-%{version}/html @@ -107,6 +122,9 @@ rm -f %{_defaultdocdir}/%{name}-docs-%{version}/html/nodejs.1 %doc ChangeLog LICENSE README.md AUTHORS %{_bindir}/node %{_mandir}/man1/node.* +%{_rpmconfigdir}/fileattrs/nodejs.attr +%{_rpmconfigdir}/nodejs* +%dir %{_prefix}/lib/node_modules %files docs %{_defaultdocdir}/%{name}-docs-%{version} @@ -118,6 +136,7 @@ rm -f %{_defaultdocdir}/%{name}-docs-%{version}/html/nodejs.1 - system library patches are now upstream - respect optflags - include documentation in subpackage +- add RPM dependency generation and related magic * Wed Dec 19 2012 Dan HorĂ¡k - 0.9.3-8 - set exclusive arch list to match v8