Blame SOURCES/list_bundled_nodejs_packages.py

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