summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarmen Stoppels <me@harmenstoppels.nl>2024-12-07 10:08:04 +0100
committerGitHub <noreply@github.com>2024-12-07 10:08:04 +0100
commit05acd29f384abf357f93fa760cfece736bd734cd (patch)
tree6b8bbbf2bfff485c8c1e37f4be983e3678fef1f5
parent77e2187e133579691df67ec88d431bc518395621 (diff)
downloadspack-05acd29f384abf357f93fa760cfece736bd734cd.tar.gz
spack-05acd29f384abf357f93fa760cfece736bd734cd.tar.bz2
spack-05acd29f384abf357f93fa760cfece736bd734cd.tar.xz
spack-05acd29f384abf357f93fa760cfece736bd734cd.zip
extensions.py: remove import of spack.cmd (#47963)
-rw-r--r--lib/spack/spack/cmd/__init__.py24
-rw-r--r--lib/spack/spack/extensions.py29
-rw-r--r--lib/spack/spack/repo.py3
-rw-r--r--lib/spack/spack/test/cmd_extensions.py8
4 files changed, 30 insertions, 34 deletions
diff --git a/lib/spack/spack/cmd/__init__.py b/lib/spack/spack/cmd/__init__.py
index e0bcf6da8d..4b64a2529a 100644
--- a/lib/spack/spack/cmd/__init__.py
+++ b/lib/spack/spack/cmd/__init__.py
@@ -4,6 +4,7 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import argparse
+import difflib
import importlib
import os
import re
@@ -125,6 +126,8 @@ def get_module(cmd_name):
tty.debug("Imported {0} from built-in commands".format(pname))
except ImportError:
module = spack.extensions.get_module(cmd_name)
+ if not module:
+ raise CommandNotFoundError(cmd_name)
attr_setdefault(module, SETUP_PARSER, lambda *args: None) # null-op
attr_setdefault(module, DESCRIPTION, "")
@@ -691,3 +694,24 @@ def find_environment(args):
def first_line(docstring):
"""Return the first line of the docstring."""
return docstring.split("\n")[0]
+
+
+class CommandNotFoundError(spack.error.SpackError):
+ """Exception class thrown when a requested command is not recognized as
+ such.
+ """
+
+ def __init__(self, cmd_name):
+ msg = (
+ f"{cmd_name} is not a recognized Spack command or extension command; "
+ "check with `spack commands`."
+ )
+ long_msg = None
+
+ similar = difflib.get_close_matches(cmd_name, all_commands())
+
+ if 1 <= len(similar) <= 5:
+ long_msg = "\nDid you mean one of the following commands?\n "
+ long_msg += "\n ".join(similar)
+
+ super().__init__(msg, long_msg)
diff --git a/lib/spack/spack/extensions.py b/lib/spack/spack/extensions.py
index e13e3f17d4..af7766f098 100644
--- a/lib/spack/spack/extensions.py
+++ b/lib/spack/spack/extensions.py
@@ -5,7 +5,6 @@
"""Service functions and classes to implement the hooks
for Spack's command extensions.
"""
-import difflib
import glob
import importlib
import os
@@ -17,7 +16,6 @@ from typing import List
import llnl.util.lang
-import spack.cmd
import spack.config
import spack.error
import spack.util.path
@@ -25,9 +23,6 @@ import spack.util.path
_extension_regexp = re.compile(r"spack-(\w[-\w]*)$")
-# TODO: For consistency we should use spack.cmd.python_name(), but
-# currently this would create a circular relationship between
-# spack.cmd and spack.extensions.
def _python_name(cmd_name):
return cmd_name.replace("-", "_")
@@ -211,8 +206,7 @@ def get_module(cmd_name):
module = load_command_extension(cmd_name, folder)
if module:
return module
- else:
- raise CommandNotFoundError(cmd_name)
+ return None
def get_template_dirs():
@@ -224,27 +218,6 @@ def get_template_dirs():
return extensions
-class CommandNotFoundError(spack.error.SpackError):
- """Exception class thrown when a requested command is not recognized as
- such.
- """
-
- def __init__(self, cmd_name):
- msg = (
- "{0} is not a recognized Spack command or extension command;"
- " check with `spack commands`.".format(cmd_name)
- )
- long_msg = None
-
- similar = difflib.get_close_matches(cmd_name, spack.cmd.all_commands())
-
- if 1 <= len(similar) <= 5:
- long_msg = "\nDid you mean one of the following commands?\n "
- long_msg += "\n ".join(similar)
-
- super().__init__(msg, long_msg)
-
-
class ExtensionNamingError(spack.error.SpackError):
"""Exception class thrown when a configured extension does not follow
the expected naming convention.
diff --git a/lib/spack/spack/repo.py b/lib/spack/spack/repo.py
index c4a104c6b9..502aa4734b 100644
--- a/lib/spack/spack/repo.py
+++ b/lib/spack/spack/repo.py
@@ -41,6 +41,7 @@ import spack.patch
import spack.provider_index
import spack.spec
import spack.tag
+import spack.tengine
import spack.util.file_cache
import spack.util.git
import spack.util.naming as nm
@@ -1485,8 +1486,6 @@ class MockRepositoryBuilder:
Both "dep_type" and "condition" can default to ``None`` in which case
``spack.dependency.default_deptype`` and ``spack.spec.Spec()`` are used.
"""
- import spack.tengine # avoid circular import
-
dependencies = dependencies or []
context = {"cls_name": nm.mod_to_class(name), "dependencies": dependencies}
template = spack.tengine.make_environment().get_template("mock-repository/package.pyt")
diff --git a/lib/spack/spack/test/cmd_extensions.py b/lib/spack/spack/test/cmd_extensions.py
index 6a4fcc0fad..fe2ab2c70b 100644
--- a/lib/spack/spack/test/cmd_extensions.py
+++ b/lib/spack/spack/test/cmd_extensions.py
@@ -210,7 +210,7 @@ def test_missing_command():
"""Ensure that we raise the expected exception if the desired command is
not present.
"""
- with pytest.raises(spack.extensions.CommandNotFoundError):
+ with pytest.raises(spack.cmd.CommandNotFoundError):
spack.cmd.get_module("no-such-command")
@@ -220,9 +220,9 @@ def test_missing_command():
("/my/bad/extension", spack.extensions.ExtensionNamingError),
("", spack.extensions.ExtensionNamingError),
("/my/bad/spack--extra-hyphen", spack.extensions.ExtensionNamingError),
- ("/my/good/spack-extension", spack.extensions.CommandNotFoundError),
- ("/my/still/good/spack-extension/", spack.extensions.CommandNotFoundError),
- ("/my/spack-hyphenated-extension", spack.extensions.CommandNotFoundError),
+ ("/my/good/spack-extension", spack.cmd.CommandNotFoundError),
+ ("/my/still/good/spack-extension/", spack.cmd.CommandNotFoundError),
+ ("/my/spack-hyphenated-extension", spack.cmd.CommandNotFoundError),
],
ids=["no_stem", "vacuous", "leading_hyphen", "basic_good", "trailing_slash", "hyphenated"],
)