Blame SOURCES/list_bundled_nodejs_packages.py

0fc7f8
#!/usr/bin/env python3
0fc7f8
import sys
0fc7f8
import os.path
0fc7f8
import tarfile
0fc7f8
from io import TextIOWrapper
0fc7f8
import json
0fc7f8
import re
0fc7f8
from packaging import version
0fc7f8
0fc7f8
0fc7f8
def read_declared_pkgs(f):
0fc7f8
    package_json = json.load(f)
0fc7f8
    return list(package_json['devDependencies'].keys()) + list(package_json['dependencies'].keys())
0fc7f8
0fc7f8
0fc7f8
def read_installed_pkgs(f):
0fc7f8
    lockfile = f.read()
0fc7f8
    return re.findall(r'^"?'  # can start with a "
0fc7f8
                      r'(.+?)@.+(?:,.*)?:\n'  # characters up to @
0fc7f8
                      r'  version "(.+)"',  # and the version
0fc7f8
                      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]
0fc7f8
        yield f"Provides: bundled(nodejs-{declared_pkg}) = {oldest_version}"
0fc7f8
0fc7f8
0fc7f8
if __name__ == "__main__":
0fc7f8
    if len(sys.argv) != 2:
0fc7f8
        print(f"usage: {sys.argv[0]} grafana-pcp-X.Y.Z.tar.gz", file=sys.stdout)
0fc7f8
        sys.exit(1)
0fc7f8
0fc7f8
    source_archive_path = sys.argv[1]
0fc7f8
    root_dir = os.path.basename(source_archive_path)[:-len('.tar.gz')]
0fc7f8
    with tarfile.open(source_archive_path) as tar:
0fc7f8
        package_json = TextIOWrapper(tar.extractfile(f'{root_dir}/package.json'))
0fc7f8
        declared_pkgs = read_declared_pkgs(package_json)
0fc7f8
0fc7f8
        yarn_lock = TextIOWrapper(tar.extractfile(f'{root_dir}/yarn.lock'))
0fc7f8
        installed_pkgs = read_installed_pkgs(yarn_lock)
0fc7f8
0fc7f8
    provides = list_provides(declared_pkgs, installed_pkgs)
0fc7f8
    for provide in sorted(provides):
0fc7f8
        print(provide)