Skip to content

WIP: Hotfix/file_resource_script_generation #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pyflow/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,11 +1077,7 @@ def deploy_suite(self, target=FileSystem, **options):
target = target(self, **options)
for t in self.all_tasks:
script, includes = t.generate_script()
try:
target.deploy_task(t.deploy_path, script, includes)
except RuntimeError:
print(f"\nERROR when deploying task: {t.fullname}\n")
raise
target.deploy_task(t.deploy_path, script, includes)
for f in self.all_families:
manual = self.generate_stub(f.manual)
if manual:
Expand Down
34 changes: 6 additions & 28 deletions pyflow/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,7 @@ def get_resource(self, filename):

return []

def install_file_stub(self, target):
"""
Installs any data associated with the resource object that is going to be deployed from the **ecFlow** server.

Parameters:
target(Deployment): The target deployment where the resource data should be installed.
"""

"""
n.b. If a resource does not need to save data at deployment time, it should not do so (e.g. WebResource)
"""
# Install path is for the suite, so we don't need to include the suite name
assert self.fullname.count("/") > 1
subpath = self.fullname[self.fullname.find("/", 1) + 1 :]

self._server_filename = os.path.join(
target.files_install_path(), subpath, self.name
)

super().install_file_stub(target)

self.save_data(target, self._server_filename)

def build_script(self):
def generate_script(self):
"""
Returns the installer script for the data resource.

Expand All @@ -128,7 +105,7 @@ def build_script(self):
for h in self._hosts:
lines += h.copy_file_to(self._server_filename, self.location()).split("\n")

return lines
return lines, []

def location(self):
"""
Expand Down Expand Up @@ -183,6 +160,8 @@ def save_data(self, target, filename):
filename(str): The filename for the resource data.
"""

self._server_filename = filename

"""
Resources don't all need to save data at generation time
"""
Expand All @@ -206,9 +185,8 @@ class FileResource(Resource):
"""

def __init__(self, name, hosts, source_file):
self._source = source_file

super().__init__(name, hosts)
self._server_filename = source_file

def md5(self):
"""
Expand All @@ -230,7 +208,7 @@ def data(self):
The resource data.
"""

with open(self._source, "rb") as f:
with open(self._server_filename, "rb") as f:
return f.read()

def save_data(self, target, filename):
Expand Down
1 change: 1 addition & 0 deletions tests/file_resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Example for a file resource
44 changes: 44 additions & 0 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from os import path
from os.path import join

from pyflow import FileResource, Notebook, Suite
from pyflow.host import SSHHost


def test_file_resource():

resouces_directory = "/resources_directory"

sshhost_1 = SSHHost("example_ssh_host_1", resources_directory=resouces_directory)
sshhost_2 = SSHHost("example_ssh_host_2", resources_directory=resouces_directory)
host_set = [sshhost_1, sshhost_2]

source_file = path.join(path.dirname(path.abspath(__file__)), "file_resource.txt")
name = "file_resource"

with Suite("s", host=sshhost_1) as s:
s.resource_file = FileResource(name, hosts=host_set, source_file=source_file)

# Check that variables are set correctly
assert s.resource_file.host == sshhost_1
assert s.resource_file.location() == join(
str(sshhost_1.resources_directory), s.name, name
)
assert s.resource_file._hosts == host_set

# Check that the deployment scripts have been generated
s.check_definition()
s.generate_node()

s.deploy_suite(target=Notebook)

generate_file_resource_script_lines, _ = s.resource_file.generate_script()
assert any(sshhost_1.name in s for s in generate_file_resource_script_lines)
assert any(sshhost_2.name in s for s in generate_file_resource_script_lines)


if __name__ == "__main__":

import pytest

pytest.main(path.abspath(__file__))
Loading