Skip to content

Commit 3493f7e

Browse files
tgamblinscheibelp
authored andcommitted
init: make spack.cmd.all_commands lazy
- `spack.cmd.all_commands` does a directory listing on `lib/spack/spack/cmd`, regardless of whether it is needed - make this lazy so that the directory listing won't happen unless it's necessary.
1 parent 1fe5dbf commit 3493f7e

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

lib/spack/docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
# Find all the `cmd-spack-*` references and add them to a command index
7878
#
7979
import spack
80-
command_names = spack.cmd.all_commands
80+
import spack.cmd
81+
command_names = spack.cmd.all_commands()
8182
documented_commands = set()
8283
for filename in glob('*rst'):
8384
with open(filename) as f:

lib/spack/spack/cmd/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,26 @@ def cmd_name(python_name):
8383
return python_name.replace('_', '-')
8484

8585

86-
for file in os.listdir(spack.paths.command_path):
87-
if file.endswith(".py") and not re.search(ignore_files, file):
88-
cmd = re.sub(r'.py$', '', file)
89-
all_commands.append(cmd_name(cmd))
90-
all_commands.sort()
86+
#: global, cached list of all commands -- access through all_commands()
87+
_all_commands = None
88+
89+
90+
def all_commands():
91+
"""Get a sorted list of all spack commands.
92+
93+
This will list the lib/spack/spack/cmd directory and find the
94+
commands there to construct the list. It does not actually import
95+
the python files -- just gets the names.
96+
"""
97+
global _all_commands
98+
if _all_commands is None:
99+
_all_commands = []
100+
for file in os.listdir(spack.paths.command_path):
101+
if file.endswith(".py") and not re.search(ignore_files, file):
102+
cmd = re.sub(r'.py$', '', file)
103+
_all_commands.append(cmd_name(cmd))
104+
_all_commands.sort()
105+
return _all_commands
91106

92107

93108
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
@@ -131,7 +131,7 @@ def rst(args):
131131

132132
@formatter
133133
def names(args):
134-
for cmd in spack.cmd.all_commands:
134+
for cmd in spack.cmd.all_commands():
135135
print(cmd)
136136

137137

lib/spack/spack/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ def set_working_dir():
108108

109109
def add_all_commands(parser):
110110
"""Add all spack subcommands to the parser."""
111-
for cmd in spack.cmd.all_commands:
111+
for cmd in spack.cmd.all_commands():
112112
parser.add_command(cmd)
113113

114114

115115
def index_commands():
116116
"""create an index of commands by section for this help level"""
117117
index = {}
118-
for command in spack.cmd.all_commands:
118+
for command in spack.cmd.all_commands():
119119
cmd_module = spack.cmd.get_module(command)
120120

121121
# make sure command modules have required properties
@@ -174,7 +174,7 @@ def format_help_sections(self, level):
174174
self.actions = self._subparsers._actions[-1]._get_subactions()
175175

176176
# make a set of commands not yet added.
177-
remaining = set(spack.cmd.all_commands)
177+
remaining = set(spack.cmd.all_commands())
178178

179179
def add_group(group):
180180
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
@@ -39,7 +39,7 @@
3939
def test_commands_by_name():
4040
"""Test default output of spack commands."""
4141
out = commands()
42-
assert out.strip().split('\n') == sorted(spack.cmd.all_commands)
42+
assert out.strip().split('\n') == sorted(spack.cmd.all_commands())
4343

4444

4545
def test_subcommands():

0 commit comments

Comments
 (0)