grafana-pcp/SOURCES/list_bundled_nodejs_packages.py

52 lines
1.8 KiB
Python
Raw Normal View History

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