Blame SOURCES/list_bundled_nodejs_packages.py

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