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.
This commit is contained in:
parent
1fe5dbf338
commit
3493f7e793
@ -77,7 +77,8 @@
|
||||
# Find all the `cmd-spack-*` references and add them to a command index
|
||||
#
|
||||
import spack
|
||||
command_names = spack.cmd.all_commands
|
||||
import spack.cmd
|
||||
command_names = spack.cmd.all_commands()
|
||||
documented_commands = set()
|
||||
for filename in glob('*rst'):
|
||||
with open(filename) as f:
|
||||
|
@ -83,11 +83,26 @@ def cmd_name(python_name):
|
||||
return python_name.replace('_', '-')
|
||||
|
||||
|
||||
for file in os.listdir(spack.paths.command_path):
|
||||
if file.endswith(".py") and not re.search(ignore_files, file):
|
||||
cmd = re.sub(r'.py$', '', file)
|
||||
all_commands.append(cmd_name(cmd))
|
||||
all_commands.sort()
|
||||
#: global, cached list of all commands -- access through all_commands()
|
||||
_all_commands = None
|
||||
|
||||
|
||||
def all_commands():
|
||||
"""Get a sorted list of all spack commands.
|
||||
|
||||
This will list the lib/spack/spack/cmd directory and find the
|
||||
commands there to construct the list. It does not actually import
|
||||
the python files -- just gets the names.
|
||||
"""
|
||||
global _all_commands
|
||||
if _all_commands is None:
|
||||
_all_commands = []
|
||||
for file in os.listdir(spack.paths.command_path):
|
||||
if file.endswith(".py") and not re.search(ignore_files, file):
|
||||
cmd = re.sub(r'.py$', '', file)
|
||||
_all_commands.append(cmd_name(cmd))
|
||||
_all_commands.sort()
|
||||
return _all_commands
|
||||
|
||||
|
||||
def remove_options(parser, *options):
|
||||
|
@ -131,7 +131,7 @@ def rst(args):
|
||||
|
||||
@formatter
|
||||
def names(args):
|
||||
for cmd in spack.cmd.all_commands:
|
||||
for cmd in spack.cmd.all_commands():
|
||||
print(cmd)
|
||||
|
||||
|
||||
|
@ -108,14 +108,14 @@ def set_working_dir():
|
||||
|
||||
def add_all_commands(parser):
|
||||
"""Add all spack subcommands to the parser."""
|
||||
for cmd in spack.cmd.all_commands:
|
||||
for cmd in spack.cmd.all_commands():
|
||||
parser.add_command(cmd)
|
||||
|
||||
|
||||
def index_commands():
|
||||
"""create an index of commands by section for this help level"""
|
||||
index = {}
|
||||
for command in spack.cmd.all_commands:
|
||||
for command in spack.cmd.all_commands():
|
||||
cmd_module = spack.cmd.get_module(command)
|
||||
|
||||
# make sure command modules have required properties
|
||||
@ -174,7 +174,7 @@ def format_help_sections(self, level):
|
||||
self.actions = self._subparsers._actions[-1]._get_subactions()
|
||||
|
||||
# make a set of commands not yet added.
|
||||
remaining = set(spack.cmd.all_commands)
|
||||
remaining = set(spack.cmd.all_commands())
|
||||
|
||||
def add_group(group):
|
||||
formatter.start_section(group.title)
|
||||
|
@ -39,7 +39,7 @@
|
||||
def test_commands_by_name():
|
||||
"""Test default output of spack commands."""
|
||||
out = commands()
|
||||
assert out.strip().split('\n') == sorted(spack.cmd.all_commands)
|
||||
assert out.strip().split('\n') == sorted(spack.cmd.all_commands())
|
||||
|
||||
|
||||
def test_subcommands():
|
||||
|
Loading…
Reference in New Issue
Block a user