Skip to content

Commit 074c0f7

Browse files
committed
spack.cmd.all_commands becomes more general, allowing for the location of subcommands.
1 parent a411f19 commit 074c0f7

4 files changed

Lines changed: 29 additions & 20 deletions

File tree

lib/spack/spack/cmd/__init__.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,33 @@ def cmd_name(python_name):
4545

4646
#: global, cached list of all commands -- access through all_commands()
4747
_all_commands = None
48+
_default_command_path = spack.paths.command_path
4849

4950

50-
def all_commands():
51-
"""Get a sorted list of all spack commands.
51+
def all_commands(command_list, command_path=None):
52+
"""Get a sorted list of all commands at the specified path.
5253
53-
This will list the lib/spack/spack/cmd directory and find the
54-
commands there to construct the list. It does not actually import
55-
the python files -- just gets the names.
54+
This will list the command_path directory and find the commands
55+
there to construct the list. It does not actually import the python
56+
files -- just gets the names. The names are cached in command_list.
5657
"""
57-
global _all_commands
58-
if _all_commands is None:
59-
_all_commands = []
58+
if command_list is None:
59+
command_list = []
6060
extensions = spack.config.get('config:extensions') or []
61-
command_paths = itertools.chain(
62-
[spack.paths.command_path],
63-
spack.extensions.command_paths(*extensions)
64-
)
61+
command_paths \
62+
= [command_path] if command_path else \
63+
itertools.chain(
64+
[_default_command_path],
65+
spack.extensions.command_paths(*extensions)
66+
)
6567
for path in command_paths:
6668
for file in os.listdir(path):
6769
if file.endswith(".py") and not re.search(ignore_files, file):
6870
cmd = re.sub(r'.py$', '', file)
69-
_all_commands.append(cmd_name(cmd))
70-
_all_commands.sort()
71+
command_list.append(cmd_name(cmd))
72+
command_list.sort()
7173

72-
return _all_commands
74+
return command_list
7375

7476

7577
def remove_options(parser, *options):

lib/spack/spack/cmd/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def rst(args):
112112

113113
@formatter
114114
def names(args):
115-
for cmd in spack.cmd.all_commands():
115+
for cmd in spack.main.all_commands():
116116
print(cmd)
117117

118118

lib/spack/spack/main.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080

8181
#: Recorded directory where spack command was originally invoked
8282
spack_working_dir = None
83+
#: All found spack commands
84+
_all_commands = None
8385

8486

8587
def set_working_dir():
@@ -92,16 +94,21 @@ def set_working_dir():
9294
spack_working_dir = spack.paths.prefix
9395

9496

97+
def all_commands():
98+
"""Return all top level commands available with Spack."""
99+
return spack.cmd.all_commands(_all_commands)
100+
101+
95102
def add_all_commands(parser):
96103
"""Add all spack subcommands to the parser."""
97-
for cmd in spack.cmd.all_commands():
104+
for cmd in spack.cmd.all_commands(_all_commands):
98105
parser.add_command(cmd)
99106

100107

101108
def index_commands():
102109
"""create an index of commands by section for this help level"""
103110
index = {}
104-
for command in spack.cmd.all_commands():
111+
for command in all_commands():
105112
cmd_module = spack.cmd.get_module(command)
106113

107114
# make sure command modules have required properties
@@ -160,7 +167,7 @@ def format_help_sections(self, level):
160167
self.actions = self._subparsers._actions[-1]._get_subactions()
161168

162169
# make a set of commands not yet added.
163-
remaining = set(spack.cmd.all_commands())
170+
remaining = set(spack.main.all_commands())
164171

165172
def add_group(group):
166173
formatter.start_section(group.title)

lib/spack/spack/test/cmd/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def test_commands_by_name():
2121
"""Test default output of spack commands."""
2222
out = commands()
23-
assert out.strip().split('\n') == sorted(spack.cmd.all_commands())
23+
assert out.strip().split('\n') == sorted(spack.main.all_commands())
2424

2525

2626
def test_subcommands():

0 commit comments

Comments
 (0)