Skip to content

Commit

Permalink
Add the verdi code export command (#5860)
Browse files Browse the repository at this point in the history
This command takes an `AbstractCode` instance (or subclass thereof) and
dumps a description of its attributes to a YAML file. This file can then
be used in `verdi code create` using the `--config` flag to recreate a
new code instance with the same configuration.
  • Loading branch information
unkcpz authored Jan 30, 2023
1 parent ff3ae82 commit a6f3d60
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
21 changes: 21 additions & 0 deletions aiida/cmdline/commands/cmd_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import click
import tabulate
import yaml

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.groups.dynamic import DynamicEntryPointCommandGroup
Expand Down Expand Up @@ -235,6 +236,26 @@ def show(code):
echo.echo(tabulate.tabulate(table))


@verdi_code.command()
@arguments.CODE()
@arguments.OUTPUT_FILE(type=click.Path(exists=False))
@with_dbenv()
def export(code, output_file):
"""Export code to a yaml file."""
code_data = {}

for key in code.get_cli_options().keys():
if key == 'computer':
value = getattr(code, key).label
else:
value = getattr(code, key)

code_data[key] = str(value)

with open(output_file, 'w', encoding='utf-8') as yfhandle:
yaml.dump(code_data, yfhandle)


@verdi_code.command()
@arguments.CODES()
@options.DRY_RUN()
Expand Down
1 change: 0 additions & 1 deletion aiida/orm/nodes/data/code/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def __init__(
self.append_text = append_text
self.prepend_text = prepend_text
self.use_double_quotes = use_double_quotes
self.use_double_quotes = use_double_quotes
self.is_hidden = is_hidden

@abc.abstractmethod
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Below is a list with all available subcommands.
create Create a new code.
delete Delete a code.
duplicate Duplicate a code allowing to change some parameters.
export Export code to a yaml file.
hide Hide one or more codes from `verdi code list`.
list List the available codes.
relabel Relabel a code.
Expand Down
25 changes: 25 additions & 0 deletions tests/cmdline/commands/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,31 @@ def test_code_duplicate_ignore(run_cli_command, aiida_local_code_factory, non_in
assert duplicate.description == ''


@pytest.mark.usefixtures('aiida_profile_clean')
def test_code_export(run_cli_command, aiida_local_code_factory, tmp_path, file_regression):
"""Test export the code setup to str."""
prepend_text = 'module load something\n some command'
code = aiida_local_code_factory('core.arithmetic.add', '/bin/cat', label='code', prepend_text=prepend_text)
filepath = tmp_path / 'code.yml'
options = [str(code.pk), str(filepath)]
run_cli_command(cmd_code.export, options)

# file regression check
with open(filepath, 'r', encoding='utf-8') as fhandle:
content = fhandle.read()
file_regression.check(content, extension='.yml')

# round trip test by create code from the config file
# we pass the new label to override since cannot have two code with same labels
new_label = 'code0'
run_cli_command(
cmd_code.code_create, ['core.code.installed', '--non-interactive', '--config', filepath, '--label', new_label]
)
new_code = load_code(new_label)
assert code.base.attributes.all == new_code.base.attributes.all
assert isinstance(new_code, InstalledCode)


@pytest.mark.parametrize('non_interactive_editor', ('vim -cwq',), indirect=True)
def test_from_config_local_file(non_interactive_editor, run_cli_command, aiida_localhost):
"""Test setting up a code from a config file on disk."""
Expand Down
8 changes: 8 additions & 0 deletions tests/cmdline/commands/test_code/test_code_export.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
append_text: ''
computer: localhost
default_calc_job_plugin: core.arithmetic.add
description: code
filepath_executable: /bin/cat
label: code
prepend_text: "module load something\n some command"
use_double_quotes: 'False'

0 comments on commit a6f3d60

Please sign in to comment.