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