Blame SOURCES/nodejs.req

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
30582b
# Copyright 2012 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
30582b
import re
30582b
import subprocess
30582b
import sys
30582b
30582b
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:
30582b
        if path.endswith('package.json'):
30582b
            fh = open(path)
30582b
            metadata = json.load(fh)
30582b
            fh.close()
30582b
30582b
            #write out the node.js interpreter dependency
30582b
            req = 'nodejs010-nodejs(engine)'
30582b
            
30582b
            if 'engines' in metadata and isinstance(metadata['engines'], dict) \
30582b
                                                and 'node' in metadata['engines']:
30582b
                deps += process_dep(req, metadata['engines']['node'])
30582b
            else:
30582b
                deps.append(req)
30582b
30582b
            if 'dependencies' in metadata:
30582b
		if isinstance(metadata['dependencies'], dict):
30582b
                    for name, version in metadata['dependencies'].iteritems():
30582b
                        req = 'nodejs010-npm(' + name + ')'
30582b
                        deps += process_dep(req, version)
30582b
                elif isinstance(metadata['dependencies'], list):
30582b
                    for name in metadata['dependencies']:
30582b
                        req = 'nodejs010-npm(' + name + ')'
30582b
                        deps.append(req)
30582b
                elif isinstance(metadata['dependencies'], basestring):
30582b
                    req = 'nodejs010-npm(' + metadata['dependencies'] + ')'
30582b
                    deps.append(req)
30582b
                else:
30582b
                    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
30582b
        # 1 or 1.x or 1.x.x 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
30582b
        # 1.2.3 or 1.2.3-4 or 1.2.x 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))
30582b
            # ~1.2.3
30582b
            elif operator == '~':
30582b
                deps.append('{0} >= {1}'.format(req, version))
30582b
                deps.append('{0} < {1}.{2}'.format(req, parts[0], parts[1]+1))
30582b
            # 1.2.3 or 1.2.3-4
30582b
            else:
30582b
                deps.append('{0} = {1}'.format(req, version))
30582b
30582b
        # ~1.2
30582b
        else:
30582b
            deps.append('{0} >= {1}'.format(req, version))
30582b
            deps.append('{0} < {1}'.format(req, parts[0]+1))
30582b
30582b
    return deps
30582b
            
30582b
if __name__ == '__main__':
30582b
    main()