From 50160842136c9926381e47eca55a7719c8c8a9d8 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 19 Mar 2025 10:28:29 +0100 Subject: [PATCH] Move default implementation of `pkg.command` to PackageBase (#49580) --- lib/spack/spack/package_base.py | 9 +++++++++ lib/spack/spack/spec.py | 27 +-------------------------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/lib/spack/spack/package_base.py b/lib/spack/spack/package_base.py index 04f13d5d603..395a563fc4c 100644 --- a/lib/spack/spack/package_base.py +++ b/lib/spack/spack/package_base.py @@ -48,6 +48,7 @@ import spack.store import spack.url import spack.util.environment +import spack.util.executable import spack.util.path import spack.util.web import spack.variant @@ -1369,6 +1370,14 @@ def prefix(self): def home(self): return self.prefix + @property + def command(self) -> spack.util.executable.Executable: + """Returns the main executable for this package.""" + path = os.path.join(self.home.bin, self.spec.name) + if fsys.is_exe(path): + return spack.util.executable.Executable(path) + raise RuntimeError(f"Unable to locate {self.spec.name} command in {self.home.bin}") + @property # type: ignore[misc] @memoized def compiler(self): diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index a32d35e1078..b391d2c563b 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -97,7 +97,6 @@ import spack.spec_parser import spack.store import spack.traverse -import spack.util.executable import spack.util.hash import spack.util.prefix import spack.util.spack_json as sjson @@ -1110,28 +1109,6 @@ def clear(self): self.edges.clear() -def _command_default_handler(spec: "Spec"): - """Default handler when looking for the 'command' attribute. - - Tries to search for ``spec.name`` in the ``spec.home.bin`` directory. - - Parameters: - spec: spec that is being queried - - Returns: - Executable: An executable of the command - - Raises: - RuntimeError: If the command is not found - """ - home = getattr(spec.package, "home") - path = os.path.join(home.bin, spec.name) - - if fs.is_exe(path): - return spack.util.executable.Executable(path) - raise RuntimeError(f"Unable to locate {spec.name} command in {home.bin}") - - def _headers_default_handler(spec: "Spec"): """Default handler when looking for the 'headers' attribute. @@ -1335,9 +1312,7 @@ class SpecBuildInterface(lang.ObjectWrapper): home = ForwardQueryToPackage("home", default_handler=None) headers = ForwardQueryToPackage("headers", default_handler=_headers_default_handler) libs = ForwardQueryToPackage("libs", default_handler=_libs_default_handler) - command = ForwardQueryToPackage( - "command", default_handler=_command_default_handler, _indirect=True - ) + command = ForwardQueryToPackage("command", default_handler=None, _indirect=True) def __init__( self,