From ffa815958e61aa978c715eb6e279c290617a6615 Mon Sep 17 00:00:00 2001 From: DavHau Date: Sun, 11 Feb 2024 21:10:07 +0700 Subject: [PATCH] pythonCatchConflictsHook: make compatible to all python 3 versions --- .../python/catch_conflicts/catch_conflicts.py | 23 +++++++++++-------- .../interpreters/python/hooks/default.nix | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkgs/development/interpreters/python/catch_conflicts/catch_conflicts.py b/pkgs/development/interpreters/python/catch_conflicts/catch_conflicts.py index 319b92bee3bc..d4219192790b 100644 --- a/pkgs/development/interpreters/python/catch_conflicts/catch_conflicts.py +++ b/pkgs/development/interpreters/python/catch_conflicts/catch_conflicts.py @@ -3,22 +3,25 @@ from pathlib import Path import collections import sys import os - - +from typing import Dict, List, Tuple do_abort: bool = False -packages: dict[str, dict[str, list[dict[str, list[str]]]]] = collections.defaultdict(list) +packages: Dict[str, Dict[str, List[Dict[str, List[str]]]]] = collections.defaultdict(list) out_path: Path = Path(os.getenv("out")) -version: tuple[int, int] = sys.version_info +version: Tuple[int, int] = sys.version_info site_packages_path: str = f'lib/python{version[0]}.{version[1]}/site-packages' +def get_name(dist: PathDistribution) -> str: + return dist.metadata['name'].lower().replace('-', '_') + + # pretty print a package def describe_package(dist: PathDistribution) -> str: - return f"{dist._normalized_name} {dist.version} ({dist._path})" + return f"{get_name(dist)} {dist.version} ({dist._path})" # pretty print a list of parents (dependency chain) -def describe_parents(parents: list[str]) -> str: +def describe_parents(parents: List[str]) -> str: if not parents: return "" return \ @@ -27,7 +30,7 @@ def describe_parents(parents: list[str]) -> str: # inserts an entry into 'packages' -def add_entry(name: str, version: str, store_path: str, parents: list[str]) -> None: +def add_entry(name: str, version: str, store_path: str, parents: List[str]) -> None: if name not in packages: packages[name] = {} if store_path not in packages[name]: @@ -39,7 +42,7 @@ def add_entry(name: str, version: str, store_path: str, parents: list[str]) -> N # transitively discover python dependencies and store them in 'packages' -def find_packages(store_path: Path, site_packages_path: str, parents: list[str]) -> None: +def find_packages(store_path: Path, site_packages_path: str, parents: List[str]) -> None: site_packages: Path = (store_path / site_packages_path) propagated_build_inputs: Path = (store_path / "nix-support/propagated-build-inputs") @@ -47,12 +50,12 @@ def find_packages(store_path: Path, site_packages_path: str, parents: list[str]) if site_packages.exists(): for dist_info in site_packages.glob("*.dist-info"): dist: PathDistribution = PathDistribution(dist_info) - add_entry(dist._normalized_name, dist.version, store_path, parents) + add_entry(get_name(dist), dist.version, store_path, parents) # recursively add dependencies if propagated_build_inputs.exists(): with open(propagated_build_inputs, "r") as f: - build_inputs: list[str] = f.read().strip().split(" ") + build_inputs: List[str] = f.read().strip().split(" ") for build_input in build_inputs: find_packages(Path(build_input), site_packages_path, parents + [build_input]) diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index e6d093a10fb7..39a958b69ab3 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -108,7 +108,7 @@ in { makePythonHook { name = "python-catch-conflicts-hook"; substitutions = let - useLegacyHook = lib.versionOlder python.pythonVersion "3.10"; + useLegacyHook = lib.versionOlder python.pythonVersion "3"; in { inherit pythonInterpreter pythonSitePackages; catchConflicts = if useLegacyHook then