Blame SOURCES/list_bundled_nodejs_packages.py

b968e9
#!/usr/bin/env python3
b968e9
#
b968e9
# generates Provides: bundled(npm(...)) = ... lines for each declared dependency and devDependency of package.json
b968e9
#
b968e9
import sys
b968e9
import json
b968e9
import re
b968e9
from packaging import version
b968e9
b968e9
b968e9
def read_declared_pkgs(package_json_path):
b968e9
    with open(package_json_path) as f:
b968e9
        package_json = json.load(f)
b968e9
        return list(package_json['dependencies'].keys()) + list(package_json['devDependencies'].keys())
b968e9
b968e9
b968e9
def read_installed_pkgs(yarn_lock_path):
b968e9
    with open(yarn_lock_path) as f:
b968e9
        lockfile = f.read()
b968e9
        return re.findall(r'^"?'  # can start with a "
b968e9
                          r'(.+?)@.+(?:,.*)?:\n'  # characters up to @
b968e9
                          r'  version "(.+)"',  # and the version
b968e9
                          lockfile, re.MULTILINE)
b968e9
b968e9
b968e9
def list_provides(declared_pkgs, installed_pkgs):
b968e9
    for declared_pkg in declared_pkgs:
b968e9
        # there can be multiple versions installed of one package (transitive dependencies)
b968e9
        # but rpm doesn't support Provides: with a single package and multiple versions
b968e9
        # so let's declare the oldest version here
b968e9
        versions = [version.parse(pkg_version)
b968e9
                    for pkg_name, pkg_version in installed_pkgs if pkg_name == declared_pkg]
b968e9
        oldest_version = sorted(versions)[0]
b968e9
        yield f"Provides: bundled(npm({declared_pkg})) = {oldest_version}"
b968e9
b968e9
b968e9
if __name__ == "__main__":
b968e9
    if len(sys.argv) != 2:
b968e9
        print(f"usage: {sys.argv[0]} package-X.Y.Z/", file=sys.stdout)
b968e9
        sys.exit(1)
b968e9
b968e9
    package_dir = sys.argv[1]
b968e9
    declared_pkgs = read_declared_pkgs(f"{package_dir}/package.json")
b968e9
    installed_pkgs = read_installed_pkgs(f"{package_dir}/yarn.lock")
b968e9
    provides = list_provides(declared_pkgs, installed_pkgs)
b968e9
    for provide in sorted(provides):
b968e9
        print(provide)