From 095f65b09d7c80742a4b9598023ab328fa9a4c34 Mon Sep 17 00:00:00 2001 From: classabbyamp Date: Wed, 4 Dec 2024 11:27:02 -0500 Subject: [PATCH] fixup! common/scripts/parse-py-metadata.py: add script to parse python module metadata Closes: #53326 [via git-merge-pr] --- common/scripts/parse-py-metadata.py | 32 +++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/common/scripts/parse-py-metadata.py b/common/scripts/parse-py-metadata.py index b97783f5f727c7..1585df91e7493d 100644 --- a/common/scripts/parse-py-metadata.py +++ b/common/scripts/parse-py-metadata.py @@ -10,7 +10,7 @@ - $DESTDIR/$py3_sitelib/*.dist-info/METADATA - $DESTDIR/$py3_sitelib/*.egg-info/PKG-INFO -./parse-py-metadata.py -S "$DESTDIR/$py3_sitelib" depends [-s] [-C] -e "extra1 extra2 ..." +./parse-py-metadata.py -S "$DESTDIR/$py3_sitelib" [-s] [-C] depends -e "extra1 extra2 ..." -D "$PKGDESTDIR/rdeps" -V <( xbps-query -R -p provides -s "py3:" ) check that the dependencies of a package match what's listed in the python @@ -24,10 +24,13 @@ import argparse from pathlib import Path +from sys import stderr +from typing import TYPE_CHECKING -from packaging.metadata import Metadata -from packaging.requirements import Requirement -from packaging.utils import canonicalize_name +if TYPE_CHECKING: + from packaging.metadata import Metadata + from packaging.requirements import Requirement + from packaging.utils import canonicalize_name def msg_err(msg: str, *, nocolor: bool = False, strict: bool = False): @@ -35,10 +38,10 @@ def msg_err(msg: str, *, nocolor: bool = False, strict: bool = False): print(msg) else: color = "31" if strict else "33" - print(f"\033[1m\033[{color}m{msg}\033[m") + print(f"\033[1m\033[{color}m{msg}\033[m", file=stderr) -def vpkgname(val: str | Requirement, *, version: str | None = None) -> str: +def vpkgname(val: "str | Requirement", *, version: str | None = None) -> str: sfx = "" if version is not None: sfx = f"-{version}" @@ -66,7 +69,7 @@ def getpkgdepname(pkgdep: str) -> str: return pkgdep.rpartition("-")[0] -def match_markers(req: Requirement, extras: set[str]) -> bool: +def match_markers(req: "Requirement", extras: set[str]) -> bool: # unconditional requirement if req.marker is None: return True @@ -172,6 +175,8 @@ def parse_depends(args): parser = argparse.ArgumentParser() parser.add_argument("-S", dest="sitepkgs", type=Path) parser.add_argument("-v", dest="pkgver") + parser.add_argument("-s", dest="strict", action="store_true") + parser.add_argument("-C", dest="nocolor", action="store_true") subparsers = parser.add_subparsers() prov_parser = subparsers.add_parser("provides") @@ -181,9 +186,18 @@ def parse_depends(args): deps_parser.add_argument("-e", dest="extras", default="") deps_parser.add_argument("-V", dest="vpkgs", type=Path) deps_parser.add_argument("-D", dest="rdeps", type=Path) - deps_parser.add_argument("-s", dest="strict", action="store_true") - deps_parser.add_argument("-C", dest="nocolor", action="store_true") deps_parser.set_defaults(func=parse_depends) args = parser.parse_args() + + try: + from packaging.metadata import Metadata + from packaging.requirements import Requirement + from packaging.utils import canonicalize_name + except ImportError: + msg_err(f"=> WARNING: {args.pkgver}: missing packaging module!\n" + f"=> WARNING: {args.pkgver}: please add python3-packaging-bootstrap to hostmakedepends to run this check", + nocolor=args.nocolor) + exit(0) + args.func(args)