Blame SOURCES/nodejs.req

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