Skip to content

Commit 05acd29

Browse files
authored
extensions.py: remove import of spack.cmd (#47963)
1 parent 77e2187 commit 05acd29

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

lib/spack/spack/cmd/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
55

66
import argparse
7+
import difflib
78
import importlib
89
import os
910
import re
@@ -125,6 +126,8 @@ def get_module(cmd_name):
125126
tty.debug("Imported {0} from built-in commands".format(pname))
126127
except ImportError:
127128
module = spack.extensions.get_module(cmd_name)
129+
if not module:
130+
raise CommandNotFoundError(cmd_name)
128131

129132
attr_setdefault(module, SETUP_PARSER, lambda *args: None) # null-op
130133
attr_setdefault(module, DESCRIPTION, "")
@@ -691,3 +694,24 @@ def find_environment(args):
691694
def first_line(docstring):
692695
"""Return the first line of the docstring."""
693696
return docstring.split("\n")[0]
697+
698+
699+
class CommandNotFoundError(spack.error.SpackError):
700+
"""Exception class thrown when a requested command is not recognized as
701+
such.
702+
"""
703+
704+
def __init__(self, cmd_name):
705+
msg = (
706+
f"{cmd_name} is not a recognized Spack command or extension command; "
707+
"check with `spack commands`."
708+
)
709+
long_msg = None
710+
711+
similar = difflib.get_close_matches(cmd_name, all_commands())
712+
713+
if 1 <= len(similar) <= 5:
714+
long_msg = "\nDid you mean one of the following commands?\n "
715+
long_msg += "\n ".join(similar)
716+
717+
super().__init__(msg, long_msg)

lib/spack/spack/extensions.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""Service functions and classes to implement the hooks
66
for Spack's command extensions.
77
"""
8-
import difflib
98
import glob
109
import importlib
1110
import os
@@ -17,17 +16,13 @@
1716

1817
import llnl.util.lang
1918

20-
import spack.cmd
2119
import spack.config
2220
import spack.error
2321
import spack.util.path
2422

2523
_extension_regexp = re.compile(r"spack-(\w[-\w]*)$")
2624

2725

28-
# TODO: For consistency we should use spack.cmd.python_name(), but
29-
# currently this would create a circular relationship between
30-
# spack.cmd and spack.extensions.
3126
def _python_name(cmd_name):
3227
return cmd_name.replace("-", "_")
3328

@@ -211,8 +206,7 @@ def get_module(cmd_name):
211206
module = load_command_extension(cmd_name, folder)
212207
if module:
213208
return module
214-
else:
215-
raise CommandNotFoundError(cmd_name)
209+
return None
216210

217211

218212
def get_template_dirs():
@@ -224,27 +218,6 @@ def get_template_dirs():
224218
return extensions
225219

226220

227-
class CommandNotFoundError(spack.error.SpackError):
228-
"""Exception class thrown when a requested command is not recognized as
229-
such.
230-
"""
231-
232-
def __init__(self, cmd_name):
233-
msg = (
234-
"{0} is not a recognized Spack command or extension command;"
235-
" check with `spack commands`.".format(cmd_name)
236-
)
237-
long_msg = None
238-
239-
similar = difflib.get_close_matches(cmd_name, spack.cmd.all_commands())
240-
241-
if 1 <= len(similar) <= 5:
242-
long_msg = "\nDid you mean one of the following commands?\n "
243-
long_msg += "\n ".join(similar)
244-
245-
super().__init__(msg, long_msg)
246-
247-
248221
class ExtensionNamingError(spack.error.SpackError):
249222
"""Exception class thrown when a configured extension does not follow
250223
the expected naming convention.

lib/spack/spack/repo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import spack.provider_index
4242
import spack.spec
4343
import spack.tag
44+
import spack.tengine
4445
import spack.util.file_cache
4546
import spack.util.git
4647
import spack.util.naming as nm
@@ -1485,8 +1486,6 @@ def add_package(self, name, dependencies=None):
14851486
Both "dep_type" and "condition" can default to ``None`` in which case
14861487
``spack.dependency.default_deptype`` and ``spack.spec.Spec()`` are used.
14871488
"""
1488-
import spack.tengine # avoid circular import
1489-
14901489
dependencies = dependencies or []
14911490
context = {"cls_name": nm.mod_to_class(name), "dependencies": dependencies}
14921491
template = spack.tengine.make_environment().get_template("mock-repository/package.pyt")

lib/spack/spack/test/cmd_extensions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_missing_command():
210210
"""Ensure that we raise the expected exception if the desired command is
211211
not present.
212212
"""
213-
with pytest.raises(spack.extensions.CommandNotFoundError):
213+
with pytest.raises(spack.cmd.CommandNotFoundError):
214214
spack.cmd.get_module("no-such-command")
215215

216216

@@ -220,9 +220,9 @@ def test_missing_command():
220220
("/my/bad/extension", spack.extensions.ExtensionNamingError),
221221
("", spack.extensions.ExtensionNamingError),
222222
("/my/bad/spack--extra-hyphen", spack.extensions.ExtensionNamingError),
223-
("/my/good/spack-extension", spack.extensions.CommandNotFoundError),
224-
("/my/still/good/spack-extension/", spack.extensions.CommandNotFoundError),
225-
("/my/spack-hyphenated-extension", spack.extensions.CommandNotFoundError),
223+
("/my/good/spack-extension", spack.cmd.CommandNotFoundError),
224+
("/my/still/good/spack-extension/", spack.cmd.CommandNotFoundError),
225+
("/my/spack-hyphenated-extension", spack.cmd.CommandNotFoundError),
226226
],
227227
ids=["no_stem", "vacuous", "leading_hyphen", "basic_good", "trailing_slash", "hyphenated"],
228228
)

0 commit comments

Comments
 (0)