diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2018-05-12 22:19:11 -0700 |
---|---|---|
committer | scheibelp <scheibel1@llnl.gov> | 2018-05-17 14:10:30 -0700 |
commit | 3493f7e793e5bf5fdc0c51978ea476185025c192 (patch) | |
tree | 5ae0a5fed870501a2f3b8029f51ba7ffda2d0a47 /lib | |
parent | 1fe5dbf338ed2d937a22cb32d75fb599dd00abf3 (diff) | |
download | spack-3493f7e793e5bf5fdc0c51978ea476185025c192.tar.gz spack-3493f7e793e5bf5fdc0c51978ea476185025c192.tar.bz2 spack-3493f7e793e5bf5fdc0c51978ea476185025c192.tar.xz spack-3493f7e793e5bf5fdc0c51978ea476185025c192.zip |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/docs/conf.py | 3 | ||||
-rw-r--r-- | lib/spack/spack/cmd/__init__.py | 25 | ||||
-rw-r--r-- | lib/spack/spack/cmd/commands.py | 2 | ||||
-rw-r--r-- | lib/spack/spack/main.py | 6 | ||||
-rw-r--r-- | lib/spack/spack/test/cmd/commands.py | 2 |
5 files changed, 27 insertions, 11 deletions
diff --git a/lib/spack/docs/conf.py b/lib/spack/docs/conf.py index c269c7ee27..0b664eb5c0 100644 --- a/lib/spack/docs/conf.py +++ b/lib/spack/docs/conf.py @@ -77,7 +77,8 @@ with open('package_list.html', 'w') as plist_file: # 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: diff --git a/lib/spack/spack/cmd/__init__.py b/lib/spack/spack/cmd/__init__.py index 8aab63d4f3..6c71cf7de9 100644 --- a/lib/spack/spack/cmd/__init__.py +++ b/lib/spack/spack/cmd/__init__.py @@ -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): diff --git a/lib/spack/spack/cmd/commands.py b/lib/spack/spack/cmd/commands.py index 3b9e780268..59607de4a4 100644 --- a/lib/spack/spack/cmd/commands.py +++ b/lib/spack/spack/cmd/commands.py @@ -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) diff --git a/lib/spack/spack/main.py b/lib/spack/spack/main.py index 68ea436be2..740367168f 100644 --- a/lib/spack/spack/main.py +++ b/lib/spack/spack/main.py @@ -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 @@ class SpackArgumentParser(argparse.ArgumentParser): 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) diff --git a/lib/spack/spack/test/cmd/commands.py b/lib/spack/spack/test/cmd/commands.py index f3ca3e7249..2d236666ca 100644 --- a/lib/spack/spack/test/cmd/commands.py +++ b/lib/spack/spack/test/cmd/commands.py @@ -39,7 +39,7 @@ spack.main.add_all_commands(parser) 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(): |