package_base.py: correctly compute package name

This commit is contained in:
Harmen Stoppels 2025-04-24 18:25:25 +02:00
parent 9dc3ad4db7
commit 4779322247
3 changed files with 23 additions and 8 deletions

View File

@ -850,15 +850,9 @@ def fullnames(cls):
@classproperty @classproperty
def name(cls): def name(cls):
"""The name of this package. """The canonical name of this package"""
The name of a package is the name of its Python module, without
the containing module names.
"""
if cls._name is None: if cls._name is None:
cls._name = cls.module.__name__ cls._name = spack.repo.pkg_name_from_module(cls.__module__)
if "." in cls._name:
cls._name = cls._name[cls._name.rindex(".") + 1 :]
return cls._name return cls._name
@classproperty @classproperty

View File

@ -83,6 +83,21 @@ def namespace_from_fullname(fullname):
return namespace return namespace
def pkg_name_from_module(module_name: str) -> str:
"""Return the actual package name from a module name.
For instance ``spack.pkg.builtin.num3dtk`` has package name ``3dtk``
and ``spack.pkg.builtin.py_numpy`` has package name ``py-numpy``
"""
if not module_name.startswith(f"{ROOT_PYTHON_NAMESPACE}."):
raise ValueError(f"Module '{module_name}' is not a Spack package module")
namespace, _, import_name = module_name[len(ROOT_PYTHON_NAMESPACE) + 1 :].rpartition(".")
name = PATH.get_repo(namespace).real_name(import_name)
if name is None:
raise ValueError(f"Module '{module_name}' does not correspond to a known package")
return name
class SpackNamespaceLoader: class SpackNamespaceLoader:
def create_module(self, spec): def create_module(self, spec):
return SpackNamespace(spec.name) return SpackNamespace(spec.name)

View File

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import importlib
import os import os
import pytest import pytest
@ -333,3 +334,8 @@ def test_package_can_have_sparse_checkout_properties(mock_packages, mock_fetch,
assert isinstance(fetcher, spack.fetch_strategy.GitFetchStrategy) assert isinstance(fetcher, spack.fetch_strategy.GitFetchStrategy)
assert hasattr(fetcher, "git_sparse_paths") assert hasattr(fetcher, "git_sparse_paths")
assert fetcher.git_sparse_paths == pkg_cls.git_sparse_paths assert fetcher.git_sparse_paths == pkg_cls.git_sparse_paths
def test_package_name_from_class_type(mock_packages):
module = importlib.import_module("spack.pkg.builtin.mock.num7zip")
assert module._7zip.name == "7zip"