Blame SOURCES/list_bundled_nodejs_packages.py

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