|
|
30582b |
#!/usr/bin/python
|
|
|
30582b |
|
|
|
30582b |
"""
|
|
|
30582b |
Automatic dependency generator for Node.js libraries.
|
|
|
30582b |
|
|
|
30582b |
Parsed from package.json. See `man npm-json` for details.
|
|
|
30582b |
"""
|
|
|
30582b |
|
|
|
149ad3 |
# Copyright 2012, 2013 T.C. Hollingsworth <tchollingsworth@gmail.com>
|
|
|
30582b |
#
|
|
|
30582b |
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
30582b |
# of this software and associated documentation files (the "Software"), to
|
|
|
30582b |
# deal in the Software without restriction, including without limitation the
|
|
|
30582b |
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
30582b |
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
30582b |
# furnished to do so, subject to the following conditions:
|
|
|
30582b |
#
|
|
|
30582b |
# The above copyright notice and this permission notice shall be included in
|
|
|
30582b |
# all copies or substantial portions of the Software.
|
|
|
30582b |
#
|
|
|
30582b |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
30582b |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
30582b |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
30582b |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
30582b |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
30582b |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
30582b |
# IN THE SOFTWARE.
|
|
|
30582b |
|
|
|
30582b |
from __future__ import unicode_literals
|
|
|
30582b |
import json
|
|
|
149ad3 |
import os
|
|
|
30582b |
import re
|
|
|
30582b |
import subprocess
|
|
|
30582b |
import sys
|
|
|
30582b |
|
|
|
149ad3 |
RE_VERSION = re.compile(r'\s*v?([<>=~^]{0,2})\s*([0-9][0-9\.\-]*)\s*')
|
|
|
30582b |
|
|
|
30582b |
def main():
|
|
|
30582b |
#npm2rpm uses functions here to write BuildRequires so don't print anything
|
|
|
30582b |
#until the very end
|
|
|
30582b |
deps = []
|
|
|
30582b |
|
|
|
30582b |
#it's highly unlikely that we'll ever get more than one file but we handle
|
|
|
30582b |
#this like all RPM automatic dependency generation scripts anyway
|
|
|
30582b |
paths = [path.rstrip() for path in sys.stdin.readlines()]
|
|
|
30582b |
|
|
|
30582b |
for path in paths:
|
|
|
149ad3 |
if not path.endswith('package.json'):
|
|
|
149ad3 |
continue
|
|
|
30582b |
|
|
|
149ad3 |
# we only want the package.json in the toplevel module directory
|
|
|
149ad3 |
pathparts = path.split(os.sep)
|
|
|
149ad3 |
if not pathparts[-5:-2] == ['usr', 'lib', 'node_modules']:
|
|
|
149ad3 |
continue
|
|
|
149ad3 |
|
|
|
149ad3 |
fh = open(path)
|
|
|
149ad3 |
metadata = json.load(fh)
|
|
|
149ad3 |
fh.close()
|
|
|
149ad3 |
|
|
|
149ad3 |
#write out the node.js interpreter dependency
|
|
|
149ad3 |
req = 'nodejs010-nodejs(engine)'
|
|
|
149ad3 |
|
|
|
149ad3 |
if 'engines' in metadata and isinstance(metadata['engines'], dict) \
|
|
|
149ad3 |
and 'node' in metadata['engines']:
|
|
|
149ad3 |
deps += process_dep(req, metadata['engines']['node'])
|
|
|
149ad3 |
else:
|
|
|
149ad3 |
deps.append(req)
|
|
|
30582b |
|
|
|
149ad3 |
if 'dependencies' in metadata:
|
|
|
149ad3 |
if isinstance(metadata['dependencies'], dict):
|
|
|
149ad3 |
for name, version in metadata['dependencies'].iteritems():
|
|
|
149ad3 |
req = 'nodejs010-npm(' + name + ')'
|
|
|
149ad3 |
deps += process_dep(req, version)
|
|
|
149ad3 |
elif isinstance(metadata['dependencies'], list):
|
|
|
149ad3 |
for name in metadata['dependencies']:
|
|
|
149ad3 |
req = 'nodejs010-npm(' + name + ')'
|
|
|
30582b |
deps.append(req)
|
|
|
149ad3 |
elif isinstance(metadata['dependencies'], basestring):
|
|
|
149ad3 |
req = 'nodejs010-npm(' + metadata['dependencies'] + ')'
|
|
|
149ad3 |
deps.append(req)
|
|
|
149ad3 |
else:
|
|
|
149ad3 |
raise TypeError('invalid package.json: dependencies not a valid type')
|
|
|
30582b |
|
|
|
30582b |
print '\n'.join(deps)
|
|
|
30582b |
|
|
|
30582b |
# invoke the regular RPM requires generator
|
|
|
30582b |
p = subprocess.Popen(['/usr/lib/rpm/find-requires'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
|
|
30582b |
print p.communicate(input='\n'.join(paths))[0]
|
|
|
30582b |
|
|
|
30582b |
def process_dep(req, version):
|
|
|
30582b |
"""Converts an individual npm dependency into RPM dependencies"""
|
|
|
30582b |
|
|
|
30582b |
deps = []
|
|
|
30582b |
|
|
|
30582b |
#there's no way RPM can do anything like an OR dependency
|
|
|
30582b |
if '||' in version:
|
|
|
30582b |
sys.stderr.write("WARNING: The {0} dependency contains an ".format(req) +
|
|
|
30582b |
"OR (||) dependency: '{0}.\nPlease manually include ".format(version) +
|
|
|
30582b |
"a versioned dependency in your spec file if necessary")
|
|
|
30582b |
deps.append(req)
|
|
|
30582b |
|
|
|
30582b |
elif ' - ' in version:
|
|
|
30582b |
gt, lt = version.split(' - ')
|
|
|
30582b |
deps.append(req + ' >= ' + gt)
|
|
|
30582b |
deps.append(req + ' <= ' + lt)
|
|
|
30582b |
|
|
|
30582b |
else:
|
|
|
30582b |
m = re.match(RE_VERSION, version)
|
|
|
30582b |
|
|
|
30582b |
if m:
|
|
|
30582b |
deps += convert_dep(req, m.group(1), m.group(2))
|
|
|
30582b |
|
|
|
30582b |
#There could be up to two versions here (e.g.">1.0 <3.1")
|
|
|
30582b |
if len(version) > m.end():
|
|
|
30582b |
m = re.match(RE_VERSION, version[m.end():])
|
|
|
30582b |
|
|
|
30582b |
if m:
|
|
|
30582b |
deps += convert_dep(req, m.group(1), m.group(2))
|
|
|
30582b |
else:
|
|
|
30582b |
deps.append(req)
|
|
|
30582b |
|
|
|
30582b |
return deps
|
|
|
30582b |
|
|
|
30582b |
def convert_dep(req, operator, version):
|
|
|
30582b |
"""Converts one of the two possibly listed versions into an RPM dependency"""
|
|
|
30582b |
|
|
|
30582b |
deps = []
|
|
|
30582b |
|
|
|
30582b |
#any version will do
|
|
|
30582b |
if not version or version == '*':
|
|
|
30582b |
deps.append(req)
|
|
|
30582b |
|
|
|
30582b |
#any prefix but ~ makes things dead simple
|
|
|
30582b |
elif operator in ['>', '<', '<=', '>=', '=']:
|
|
|
30582b |
deps.append(' '.join([req, operator, version]))
|
|
|
30582b |
|
|
|
30582b |
#oh boy, here we go...
|
|
|
30582b |
else:
|
|
|
30582b |
#split the dotted portions into a list (handling trailing dots properly)
|
|
|
30582b |
parts = [part if part else 'x' for part in version.split('.')]
|
|
|
30582b |
parts = [int(part) if part != 'x' and not '-' in part
|
|
|
30582b |
else part for part in parts]
|
|
|
30582b |
|
|
|
149ad3 |
# 1 or 1.x or 1.x.x or ~1 or ^1
|
|
|
30582b |
if len(parts) == 1 or parts[1] == 'x':
|
|
|
30582b |
if parts[0] != 0:
|
|
|
30582b |
deps.append('{0} >= {1}'.format(req, parts[0]))
|
|
|
30582b |
deps.append('{0} < {1}'.format(req, parts[0]+1))
|
|
|
30582b |
|
|
|
149ad3 |
# 1.2.3 or 1.2.3-4 or 1.2.x or ~1.2.3 or ^1.2.3 or 1.2
|
|
|
30582b |
elif len(parts) == 3 or operator != '~':
|
|
|
30582b |
# 1.2.x or 1.2
|
|
|
30582b |
if len(parts) == 2 or parts[2] == 'x':
|
|
|
30582b |
deps.append('{0} >= {1}.{2}'.format(req, parts[0], parts[1]))
|
|
|
30582b |
deps.append('{0} < {1}.{2}'.format(req, parts[0], parts[1]+1))
|
|
|
149ad3 |
# ~1.2.3 or ^0.1.2 (zero is special with the caret operator)
|
|
|
149ad3 |
elif operator == '~' or (operator == '^' and parts[0] == 0 and parts[1] > 0):
|
|
|
30582b |
deps.append('{0} >= {1}'.format(req, version))
|
|
|
30582b |
deps.append('{0} < {1}.{2}'.format(req, parts[0], parts[1]+1))
|
|
|
149ad3 |
#^1.2.3
|
|
|
149ad3 |
elif operator == '^' and parts[0:1] != [0,0]:
|
|
|
149ad3 |
deps.append('{0} >= {1}'.format(req, version))
|
|
|
149ad3 |
deps.append('{0} < {1}'.format(req, parts[0]+1))
|
|
|
149ad3 |
# 1.2.3 or 1.2.3-4 or ^0.0.3
|
|
|
30582b |
else:
|
|
|
30582b |
deps.append('{0} = {1}'.format(req, version))
|
|
|
30582b |
|
|
|
30582b |
# ~1.2
|
|
|
149ad3 |
elif operator == '~':
|
|
|
30582b |
deps.append('{0} >= {1}'.format(req, version))
|
|
|
30582b |
deps.append('{0} < {1}'.format(req, parts[0]+1))
|
|
|
149ad3 |
|
|
|
149ad3 |
#^1.2
|
|
|
149ad3 |
elif operator == '^':
|
|
|
149ad3 |
deps.append('{0} >= {1}'.format(req, version))
|
|
|
149ad3 |
deps.append('{0} < {1}'.format(req, parts[0]+1))
|
|
|
149ad3 |
|
|
|
30582b |
|
|
|
30582b |
return deps
|
|
|
30582b |
|
|
|
30582b |
if __name__ == '__main__':
|
|
|
30582b |
main()
|