Skip to content

Commit

Permalink
Dump node repository contents for PortableCode on _prepare_.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeigerJ2 committed Oct 3, 2024
1 parent 76c0d86 commit ee539ef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/aiida/orm/nodes/data/code/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from __future__ import annotations

import abc
import contextlib
import functools
import pathlib
import typing as t
Expand Down Expand Up @@ -391,16 +390,15 @@ def _prepare_yaml(self, *args, **kwargs):
sort = kwargs.get('sort', False)

for key in self.Model.model_fields.keys():
with contextlib.suppress(AttributeError):
value = getattr(self, key).label if key == 'computer' else getattr(self, key)
value = getattr(self, key).label if key == 'computer' else getattr(self, key)

# If the attribute is not set, for example ``with_mpi`` do not export it
# so that there are no null-values in the resulting YAML file
if value is not None:
code_data[key] = str(value)
# If the attribute is not set, for example ``with_mpi`` do not export it
# so that there are no null-values in the resulting YAML file
if value is not None:
code_data[key] = str(value)

return yaml.dump(code_data, sort_keys=sort, encoding='utf-8'), {}

def _prepare_yml(self, *args, **kwargs):
"""Also allow for export as .yaml"""
"""Also allow for export as .yml"""
return self._prepare_yaml(*args, **kwargs)
15 changes: 15 additions & 0 deletions src/aiida/orm/nodes/data/code/portable.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from __future__ import annotations

import logging
import pathlib
import typing as t

Expand All @@ -34,6 +35,7 @@
from .legacy import Code

__all__ = ('PortableCode',)
_LOGGER = logging.getLogger(__name__)


class PortableCode(Code):
Expand Down Expand Up @@ -177,3 +179,16 @@ def filepath_executable(self, value: str) -> None:
raise ValueError('The `filepath_executable` should not be absolute.')

self.base.attributes.set(self._KEY_ATTRIBUTE_FILEPATH_EXECUTABLE, value)

def _prepare_yaml(self, *args, **kwargs):
"""Export code to a YAML file."""
target_path = pathlib.Path().cwd() / f'portablecode-{self.pk}'
setattr(self, 'filepath_files', str(target_path))
self.base.repository.copy_tree(target=target_path)
_LOGGER.warning(f'Repository files for PortableCode <{self.pk}> dumped to folder `{target_path}`.')

return super()._prepare_yaml(*args, **kwargs)

def _prepare_yml(self, *args, **kwargs):
"""Also allow for export as .yml"""
return self._prepare_yaml(*args, **kwargs)
17 changes: 17 additions & 0 deletions tests/orm/data/code/test_portable.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,20 @@ def test_get_execname(tmp_path):
code = PortableCode(label='some-label', filepath_executable='bash', filepath_files=tmp_path)
with pytest.warns(AiidaDeprecationWarning):
assert code.get_execname() == 'bash'


def test_portablecode_repo_dump(tmp_path, chdir_tmp_path):
"""Test that the node repository contents of an orm.PortableCode are dumped upon YAML export."""
filepath_files = tmp_path / 'tmp'
filepath_files.mkdir()
(filepath_files / 'bash').touch()
(filepath_files / 'subdir').mkdir()
(filepath_files / 'subdir/test').touch()
code = PortableCode(label='some-label', filepath_executable='bash', filepath_files=filepath_files)
code.store()
chdir_tmp_path
code._prepare_yaml()
repo_dump_path = tmp_path / f'portablecode-{code.pk}'
assert repo_dump_path.is_dir()
assert (repo_dump_path / 'bash').is_file()
assert (repo_dump_path / 'subdir/test').is_file()

0 comments on commit ee539ef

Please sign in to comment.