#!/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 <tchollingsworth@gmail.com>
# Copyright 2017 Tomas Tomecek <ttomecek@redhat.com>
#
# 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 print_function
import os
import sys
import json
provides = set()
def handle_package_json(path, bundled=False):
"""
process package.json file available on path, print RPM dependency based on name and version
"""
if not path.endswith('package.json') or not os.path.isfile(path):
return
fh = open(path)
metadata = json.load(fh)
fh.close()
try:
if metadata['private']:
return
except KeyError:
pass
try:
name = metadata["name"]
except KeyError:
return
try:
version = metadata["version"]
except KeyError:
return
if bundled:
value = "bundled(nodejs-%s) = %s" % (name, version)
else:
value = "rh-nodejs10-npm(%s) = %s" % (name, version)
provides.add(value)
def handle_module(path, bundled):
"""
process npm module and all its bundled dependencies
"""
handle_package_json(path, bundled=bundled)
if not os.path.isdir(path):
path = os.path.dirname(path)
node_modules_dir_candidate = os.path.join(path, "node_modules")
if os.path.isdir(node_modules_dir_candidate):
for module_path in os.listdir(node_modules_dir_candidate):
module_abs_path = os.path.join(node_modules_dir_candidate, module_path)
# skip modules which are linked against system module
if not os.path.islink(module_abs_path):
p_json_file = os.path.join(module_abs_path, "package.json")
handle_module(p_json_file, bundled=True)
def main():
""" read list of package.json paths from stdin """
paths = [path.rstrip() for path in sys.stdin.readlines()]
for path in paths:
handle_module(path, bundled=False)
for provide in sorted(provides):
print(provide)
if __name__ == '__main__':
main()