Skip to content
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

update git deployment and cleanup tasks deployment #15

Merged
merged 8 commits into from
Oct 26, 2023
Merged
69 changes: 44 additions & 25 deletions pyflow/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def deploy_task(self, deploy_path, full_script, required_includes):

# None is a valid deploy path for Notebooks
if deploy_path is not None:
assert deploy_path[0] == "/"
if not deploy_path.startswith(self._files):
print("Deploy path: {}".format(deploy_path))
print("Suite base path: {}".format(self._files))
Expand Down Expand Up @@ -185,62 +184,83 @@ def create_directories(*args):


class FileSystem(Deployment):
"""
A filesystem target for suite deployment
Parameters:
suite(Suite_): The suite object to deploy.
path(str): The target directory (by default ECF_FILES).
Example:
s = pf.Suite('suite')
pyflow.FileSystem(s, path='/path/to/suite/files')
"""

def __init__(self, suite, path=None, **kwargs):
super().__init__(suite, **kwargs)
self.path = path

def patch_path(self, path):
"""
Allow derived types to simply modify the storage behaviour
Allows to deploy the suite to a different place than ECF_FILES
"""
if self.path:
rel_path = os.path.relpath(path, self._files)
path = os.path.join(self.path, rel_path)
return path

def create_directory(self, path):
path = self.patch_path(path)
if not os.path.exists(path):
try:
os.makedirs(path)
print("Create directory: {}".format(path))
except Exception:
print("WARNING: Couldn't create directory: {}".format(path))

def check(self, target):
def check(self, target, content):
if target is None:
raise RuntimeError(
"None is not a valid path for deployment. Most likely files/ECF_FILES unspecified"
)
if os.path.exists(target):
if not self._overwrite:
print("File %s exists, not overwriting" % (target,))
return False
else:
print("Overwriting existing file: %s" % (target,))

self.create_directory(os.path.dirname(target))

if not os.path.exists(target):
self.create_directory(os.path.dirname(target))
return True

previous = open(target, "r").read()

if previous == content:
return False

if not self._overwrite:
raise RuntimeError("File %s exists, not overwriting." % (target,))

print("Overwriting existing file: %s" % (target,))
return True

def copy(self, source, target):
target = self.patch_path(target)
super().copy(source, target)

if not self.check(target):
content = open(source, "r").read()

if not self.check(target, content):
return

print("Copy %s to %s" % (source, target))

with open(source, "r") as f:
with open(target, "w") as g:
g.write(f.read())
with open(target, "w") as g:
g.write(content)

def save(self, source, target):
target = self.patch_path(target)
super().save(source, target)

if not self.check(target):
return
content = "\n".join(source) if isinstance(source, list) else source

print("Save %s" % (target,))
if not self.check(target, content):
return

output = "\n".join(source) if isinstance(source, list) else source
assert isinstance(output, (str, bytes))
with open(target, "w" if isinstance(output, str) else "wb") as g:
g.write(output)
assert isinstance(content, (str, bytes))
with open(target, "w" if isinstance(content, str) else "wb") as g:
g.write(content)

def create_directories(self, path):
pass
Expand Down Expand Up @@ -277,7 +297,6 @@ def __init__(self, suite, path=None):

self._deploy_path = path

print(self._deploy_path)
assert os.path.exists(os.path.join(self._deploy_path, ".git"))

# Cleaning existing deploy directory
Expand Down
29 changes: 4 additions & 25 deletions pyflow/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def family(self):
return self

def _build(self, ecflow_parent):
if type(ecflow_parent) == ecflow.Task:
if isinstance(ecflow_parent, ecflow.Task):
raise GenerateError(
"Cannot add Family '{}' to Task '{}'".format(
self.name, ecflow_parent.name
Expand Down Expand Up @@ -1042,8 +1042,8 @@ def deploy_suite(self, target=FileSystem, **options):

target = target(self, **options)
for t in self.all_tasks:
t.install_file_stub(target)
t.create_directories(target)
script, includes = t.generate_script()
target.deploy_task(t.deploy_path, script, includes)
target.deploy_headers()
return target

Expand Down Expand Up @@ -1134,7 +1134,7 @@ def ecflow_object(self):
return ecflow.Task(str(self._name))

def _build(self, ecflow_parent):
if type(ecflow_parent) == ecflow.Task:
if isinstance(ecflow_parent, ecflow.Task):
raise GenerateError(
"Cannot add '{}' to task '{}'".format(self.name, ecflow_parent.name())
)
Expand Down Expand Up @@ -1207,27 +1207,6 @@ def deploy_path(self):
except ValueError:
return None

def install_file_stub(self, target):
"""
Deploys current task to the provided target.

Parameters:
target(Deployment): Deployment target for the task.
"""

script, includes = self.generate_script()
target.deploy_task(self.deploy_path, script, includes)

def create_directories(self, target):
"""
Creates task directories for deployment.

Parameters:
target(Deployment): Deployment target for the task.
"""

target.create_directories(self.parent.fullname)

def task_modules(self):
"""
Returns list of task modules.
Expand Down
Loading