Blame SOURCES/list_bundled_nodejs_packages.py

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