Blame SOURCES/list_bundled_nodejs_packages.py

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