From 0baee4be650f6284a1c4d63e7c0e646b587840b5 Mon Sep 17 00:00:00 2001 From: Tomas Tomecek Date: Sep 01 2017 14:11:07 +0000 Subject: [PATCH 1/2] nodejs.prov: account for bundled libraries Signed-off-by: Tomas Tomecek --- diff --git a/nodejs.prov b/nodejs.prov index 7c8dac2..ac4ac71 100755 --- a/nodejs.prov +++ b/nodejs.prov @@ -6,6 +6,7 @@ Automatic provides generator for Node.js libraries. Taken from package.json. See `man npm-json` for details. """ # Copyright 2012 T.C. Hollingsworth +# Copyright 2017 Tomas Tomecek # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to @@ -25,21 +26,51 @@ Taken from package.json. See `man npm-json` for details. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import json +from __future__ import print_function + +import os import sys +import json + + +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() + + if 'name' in metadata and not ('private' in metadata and metadata['private']): + if bundled: + value = "bundled(nodejs-%s) = %s" % (metadata["name"], metadata["version"]) + else: + value = "npm(%s) = %s" % (metadata["name"], metadata["version"]) + print(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): + p_json_file = os.path.join(node_modules_dir_candidate, module_path, "package.json") + handle_module(p_json_file, bundled=True) + -paths = [path.rstrip() for path in sys.stdin.readlines()] +def main(): + paths = [path.rstrip() for path in sys.stdin.readlines()] -for path in paths: - if path.endswith('package.json'): - fh = open(path) - metadata = json.load(fh) - fh.close() + for path in paths: + handle_module(path, bundled=False) - if 'name' in metadata and not ('private' in metadata and metadata['private']): - print 'npm(' + metadata['name'] + ')', - if 'version' in metadata: - print '= ' + metadata['version'] - else: - print +if __name__ == '__main__': + main() From 69757e15e17bd70483d961b0c418377cb6bb5c8e Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Sep 01 2017 14:54:09 +0000 Subject: [PATCH 2/2] Add sorting and uniqueness for bundled provides --- diff --git a/nodejs.prov b/nodejs.prov index ac4ac71..1d06fbd 100755 --- a/nodejs.prov +++ b/nodejs.prov @@ -32,6 +32,7 @@ import os import sys import json +provides = set() def handle_package_json(path, bundled=False): """ @@ -48,7 +49,7 @@ def handle_package_json(path, bundled=False): value = "bundled(nodejs-%s) = %s" % (metadata["name"], metadata["version"]) else: value = "npm(%s) = %s" % (metadata["name"], metadata["version"]) - print(value) + provides.add(value) def handle_module(path, bundled): @@ -71,6 +72,9 @@ def main(): for path in paths: handle_module(path, bundled=False) + for provide in sorted(provides): + print(provide) + if __name__ == '__main__': main()