Blame SOURCES/list_bundled_nodejs_packages.py

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