Support config variables in config.yaml extensions paths (#17772)

This commit is contained in:
Jordan Galby 2022-02-07 11:40:52 +01:00 committed by GitHub
parent f3139555b1
commit 37ae4c0fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 10 deletions

View File

@ -38,8 +38,7 @@ obtained by cloning the corresponding git repository:
.. code-block:: console .. code-block:: console
$ pwd $ cd ~/
/home/user
$ mkdir tmp && cd tmp $ mkdir tmp && cd tmp
$ git clone https://github.com/alalazo/spack-scripting.git $ git clone https://github.com/alalazo/spack-scripting.git
Cloning into 'spack-scripting'... Cloning into 'spack-scripting'...
@ -62,7 +61,7 @@ paths to ``config.yaml``. In the case of our example this means ensuring that:
config: config:
extensions: extensions:
- /home/user/tmp/spack-scripting - ~/tmp/spack-scripting
is part of your configuration file. Once this is setup any command that the extension provides is part of your configuration file. Once this is setup any command that the extension provides
will be available from the command line: will be available from the command line:

View File

@ -202,7 +202,7 @@ def unit_test(parser, args, unknown_args):
pytest_root = spack.paths.spack_root pytest_root = spack.paths.spack_root
if args.extension: if args.extension:
target = args.extension target = args.extension
extensions = spack.config.get('config:extensions') extensions = spack.extensions.get_extension_paths()
pytest_root = spack.extensions.path_for_extension(target, *extensions) pytest_root = spack.extensions.path_for_extension(target, *extensions)
# pytest.ini lives in the root of the spack repository. # pytest.ini lives in the root of the spack repository.

View File

@ -15,6 +15,7 @@
import spack.config import spack.config
import spack.error import spack.error
import spack.util.path
_extension_regexp = re.compile(r'spack-(\w[-\w]*)$') _extension_regexp = re.compile(r'spack-(\w[-\w]*)$')
@ -105,10 +106,19 @@ def ensure_package_creation(name):
return module return module
def get_extension_paths():
"""Return the list of canonicalized extension paths from config:extensions.
"""
extension_paths = spack.config.get('config:extensions') or []
paths = [spack.util.path.canonicalize_path(p) for p in extension_paths]
return paths
def get_command_paths(): def get_command_paths():
"""Return the list of paths where to search for command files.""" """Return the list of paths where to search for command files."""
command_paths = [] command_paths = []
extension_paths = spack.config.get('config:extensions') or [] extension_paths = get_extension_paths()
for path in extension_paths: for path in extension_paths:
extension = _python_name(extension_name(path)) extension = _python_name(extension_name(path))
@ -145,7 +155,7 @@ def get_module(cmd_name):
""" """
# If built-in failed the import search the extension # If built-in failed the import search the extension
# directories in order # directories in order
extensions = spack.config.get('config:extensions') or [] extensions = get_extension_paths()
for folder in extensions: for folder in extensions:
module = load_command_extension(cmd_name, folder) module = load_command_extension(cmd_name, folder)
if module: if module:
@ -158,7 +168,7 @@ def get_template_dirs():
"""Returns the list of directories where to search for templates """Returns the list of directories where to search for templates
in extensions. in extensions.
""" """
extension_dirs = spack.config.get('config:extensions') or [] extension_dirs = get_extension_paths()
extensions = [os.path.join(x, 'templates') for x in extension_dirs] extensions = [os.path.join(x, 'templates') for x in extension_dirs]
return extensions return extensions

View File

@ -249,14 +249,27 @@ def test_get_command_paths(config):
for ext in extensions: for ext in extensions:
ext_path = os.path.join('my', 'path', 'to', 'spack-' + ext) ext_path = os.path.join('my', 'path', 'to', 'spack-' + ext)
ext_paths.append(ext_path) ext_paths.append(ext_path)
expected_cmd_paths.append(os.path.join(ext_path, path = os.path.join(ext_path, spack.cmd.python_name(ext), 'cmd')
spack.cmd.python_name(ext), path = os.path.abspath(path)
'cmd')) expected_cmd_paths.append(path)
with spack.config.override('config:extensions', ext_paths): with spack.config.override('config:extensions', ext_paths):
assert spack.extensions.get_command_paths() == expected_cmd_paths assert spack.extensions.get_command_paths() == expected_cmd_paths
def test_variable_in_extension_path(config, working_env):
"""Test variables in extension paths."""
os.environ['_MY_VAR'] = "my/var"
ext_paths = [
os.path.join("~", "${_MY_VAR}", "spack-extension-1")
]
expected_ext_paths = [
os.path.join(os.environ['HOME'], os.environ['_MY_VAR'], "spack-extension-1")
]
with spack.config.override('config:extensions', ext_paths):
assert spack.extensions.get_extension_paths() == expected_ext_paths
@pytest.mark.parametrize('command_name,contents,exception', @pytest.mark.parametrize('command_name,contents,exception',
[('bad-cmd', 'from oopsie.daisy import bad\n', [('bad-cmd', 'from oopsie.daisy import bad\n',
ImportError), ImportError),