Blame SOURCES/nodejs.prov

fb565e
#!/usr/bin/python
fb565e
fb565e
"""
fb565e
Automatic provides generator for Node.js libraries.
fb565e
fb565e
Taken from package.json.  See `man npm-json` for details.
fb565e
"""
fb565e
# Copyright 2012 T.C. Hollingsworth <tchollingsworth@gmail.com>
fb565e
# Copyright 2017 Tomas Tomecek <ttomecek@redhat.com>
fb565e
#
fb565e
# Permission is hereby granted, free of charge, to any person obtaining a copy
fb565e
# of this software and associated documentation files (the "Software"), to
fb565e
# deal in the Software without restriction, including without limitation the
fb565e
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
fb565e
# sell copies of the Software, and to permit persons to whom the Software is
fb565e
# furnished to do so, subject to the following conditions:
fb565e
#
fb565e
# The above copyright notice and this permission notice shall be included in
fb565e
# all copies or substantial portions of the Software.
fb565e
#
fb565e
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
fb565e
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
fb565e
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
fb565e
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
fb565e
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
fb565e
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
fb565e
# IN THE SOFTWARE.
fb565e
fb565e
from __future__ import print_function
fb565e
fb565e
import os
fb565e
import sys
fb565e
import json
fb565e
fb565e
provides = set()
fb565e
fb565e
def handle_package_json(path, bundled=False):
fb565e
    """
fb565e
    process package.json file available on path, print RPM dependency based on name and version
fb565e
    """
fb565e
    if not path.endswith('package.json') or not os.path.isfile(path):
fb565e
        return
fb565e
    fh = open(path)
fb565e
    metadata = json.load(fh)
fb565e
    fh.close()
fb565e
fb565e
    try:
fb565e
        if metadata['private']:
fb565e
            return
fb565e
    except KeyError:
fb565e
        pass
fb565e
fb565e
    try:
fb565e
        name = metadata["name"]
fb565e
    except KeyError:
fb565e
        return
fb565e
    try:
fb565e
        version = metadata["version"]
fb565e
    except KeyError:
fb565e
        return
fb565e
fb565e
    if bundled:
fb565e
        value = "bundled(nodejs-%s) = %s" % (name, version)
fb565e
    else:
fb565e
        value = "rh-nodejs10-npm(%s) = %s" % (name, version)
fb565e
    provides.add(value)
fb565e
fb565e
fb565e
def handle_module(path, bundled):
fb565e
    """
fb565e
    process npm module and all its bundled dependencies
fb565e
    """
fb565e
    handle_package_json(path, bundled=bundled)
fb565e
    if not os.path.isdir(path):
fb565e
        path = os.path.dirname(path)
fb565e
    node_modules_dir_candidate = os.path.join(path, "node_modules")
fb565e
    if os.path.isdir(node_modules_dir_candidate):
fb565e
        for module_path in os.listdir(node_modules_dir_candidate):
fb565e
            module_abs_path = os.path.join(node_modules_dir_candidate, module_path)
fb565e
            # skip modules which are linked against system module
fb565e
            if not os.path.islink(module_abs_path):
fb565e
                p_json_file = os.path.join(module_abs_path, "package.json")
fb565e
                handle_module(p_json_file, bundled=True)
fb565e
fb565e
fb565e
def main():
fb565e
    """ read list of package.json paths from stdin """
fb565e
    paths = [path.rstrip() for path in sys.stdin.readlines()]
fb565e
fb565e
    for path in paths:
fb565e
        handle_module(path, bundled=False)
fb565e
fb565e
    for provide in sorted(provides):
fb565e
        print(provide)
fb565e
fb565e
fb565e
if __name__ == '__main__':
fb565e
    main()