Skip to content

Commit

Permalink
Showing 7 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/about/release_notes.rst
Original file line number Diff line number Diff line change
@@ -402,6 +402,7 @@ The above change takes effect for the following shell scripts:
- ``MAKE_DIRECTORY``
- ``MAKE_SYMLINK``
- ``MOVE_FILE``
- ``MOVE_DIRECTORY``
- ``SCRIPT``
- ``SYMLINK``

33 changes: 33 additions & 0 deletions src/ert/plugins/hook_implementations/forward_model_steps.py
Original file line number Diff line number Diff line change
@@ -422,6 +422,38 @@ def documentation() -> Optional[ForwardModelStepDocumentation]:
)


class MoveDirectory(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
name="MOVE_DIRECTORY",
command=[
str(
(
Path(__file__)
/ "../../../resources/shell_scripts/move_directory.py"
).resolve()
),
"<FROM>",
"<TO>",
],
)

@staticmethod
def documentation() -> Optional[ForwardModelStepDocumentation]:
return ForwardModelStepDocumentation(
category="utility.file_system",
description="""
The :code:`MOVE_DIRECTORY` job will move a directory.
If the target directory already exists, it will be replaced.
""",
examples="""
.. code-block:: bash
FORWARD_MODEL MOVE_DIRECTORY(<FROM>=dir/to/move,<TO>=to/new/path)
""",
)


class Symlink(ForwardModelStepPlugin):
def __init__(self) -> None:
super().__init__(
@@ -529,6 +561,7 @@ def documentation() -> Optional[ForwardModelStepDocumentation]:
MakeDirectory,
MakeSymlink,
MoveFile,
MoveDirectory,
Symlink,
TemplateRender,
]
26 changes: 26 additions & 0 deletions src/ert/resources/shell_scripts/move_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python
import os
import shutil
import sys


def move_directory(src_dir, target):
"""
Will raise IOError if src_dir is not a folder.
"""
if os.path.isdir(src_dir):
if os.path.exists(target):
shutil.rmtree(target)
shutil.move(src_dir, target)
else:
raise IOError(f"Input argument {src_dir} is not an existing directory")


if __name__ == "__main__":
src = sys.argv[1]
target = sys.argv[2]
try:
move_directory(src, target)
except IOError as e:
sys.exit(f"MOVE_DIRECTORY failed with the following error: {e}")
2 changes: 2 additions & 0 deletions src/ert/resources/workflows/jobs/shell/MOVE_DIRECTORY
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INTERNAL FALSE
EXECUTABLE ../../../shell_scripts/move_directory.py
5 changes: 5 additions & 0 deletions tests/integration_tests/shared/share/test_shell.py
Original file line number Diff line number Diff line change
@@ -31,6 +31,8 @@ def test_shell_scripts_integration(tmpdir):
FORWARD_MODEL MAKE_DIRECTORY(<DIRECTORY>=mydir)
FORWARD_MODEL COPY_DIRECTORY(<FROM>=mydir, <TO>=mydir2)
FORWARD_MODEL DELETE_DIRECTORY(<DIRECTORY>=mydir)
FORWARD_MODEL COPY_FILE(<FROM>=<CONFIG_PATH>/file.txt, <TO>=mydir3/copied.txt)
FORWARD_MODEL MOVE_DIRECTORY(<FROM>=mydir3, <TO>=mydir4/mydir3)
"""
)

@@ -48,3 +50,6 @@ def test_shell_scripts_integration(tmpdir):
assert os.path.exists("realization-0/iter-0/copied3.txt")
assert not os.path.exists("realization-0/iter-0/mydir")
assert os.path.exists("realization-0/iter-0/mydir2")
assert not os.path.exists("realization-0/iter-0/mydir3")
assert os.path.exists("realization-0/iter-0/mydir4/mydir3")
assert os.path.exists("realization-0/iter-0/mydir4/mydir3/copied.txt")
2 changes: 2 additions & 0 deletions tests/unit_tests/config/config_dict_generator.py
Original file line number Diff line number Diff line change
@@ -159,6 +159,7 @@ def default_forward_model_names():
st.sampled_from(
[
"DELETE_FILE",
"move_directory",
"move_file",
"make_directory",
"rms",
@@ -174,6 +175,7 @@ def default_forward_model_names():
"FLOW",
"ECLIPSE300",
"eclipse300",
"MOVE_DIRECTORY",
"MOVE_FILE",
"template_render",
"TEMPLATE_RENDER",
37 changes: 37 additions & 0 deletions tests/unit_tests/shared/share/test_shell.py
Original file line number Diff line number Diff line change
@@ -66,6 +66,9 @@ def delete_file(self, *args):
def move_file(self, *args):
return self._call_script("move_file.py", args)

def move_directory(self, *args):
return self._call_script("move_directory.py", args)


@pytest.fixture
def shell(source_root):
@@ -171,6 +174,39 @@ def test_move_file(shell):
shell.move_file("global_variables.ipl", "rms/ipl/global_variables.ipl")


@pytest.mark.usefixtures("use_tmpdir")
def test_move_directory(shell):
# Test moving directory that does not exist
err = shell.move_directory("dir1", "path/file2").stderr
assert b"Input argument dir1 is not an existing directory" in err

# Test happy path
shell.mkdir("dir1")
Path("dir1/file").write_text("Hei!", encoding="utf-8")
shell.move_directory("dir1", "dir2")
assert os.path.exists("dir2")
assert os.path.exists("dir2/file")
assert not os.path.exists("dir1")

# Test overwriting directory
shell.mkdir("dir1")
Path("dir1/file2").write_text("Hei!", encoding="utf-8")
shell.move_directory("dir1", "dir2")
assert os.path.exists("dir2")
assert os.path.exists("dir2/file2")
assert not os.path.exists("dir2/file")
assert not os.path.exists("dir1")

# Test moving directory inside already existing direcotry
shell.mkdir("dir1")
Path("dir1/file3").write_text("Hei!", encoding="utf-8")
shell.move_directory("dir1", "dir2/dir1")
assert os.path.exists("dir2/dir1")
assert os.path.exists("dir2/file2")
assert os.path.exists("dir2/dir1/file3")
assert not os.path.exists("dir1")


@pytest.mark.usefixtures("use_tmpdir")
def test_move_file_into_folder_file_exists(shell):
shell.mkdir("dst_folder")
@@ -460,6 +496,7 @@ def test_shell_script_jobs_names(minimal_case):
"COPY_DIRECTORY",
"MAKE_SYMLINK",
"MOVE_FILE",
"MOVE_DIRECTORY",
"MAKE_DIRECTORY",
"CAREFUL_COPY_FILE",
"SYMLINK",

0 comments on commit cf02f45

Please sign in to comment.