Skip to content

Commit

Permalink
Merge pull request #146 from TheoChem-VU/145-runing-post-scripts-not-…
Browse files Browse the repository at this point in the history
…starting-in-the-right-directory

Post scripts are now added when the job is being set up
  • Loading branch information
YHordijk authored Feb 29, 2024
2 parents eb9bc8f + 2097f6f commit d4e1208
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/tcutility/job/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, test_mode: bool = False, overwrite: bool = False, wait_for_fi
self.wait_for_finish = wait_for_finish
self._preambles = []
self._postambles = []
self._postscripts = []

def __enter__(self):
return self
Expand Down Expand Up @@ -104,6 +105,10 @@ def run(self):
log.info(f'Skipping calculation {j(self.rundir, self.name)}, it is already finished or currently pending or running.')
return

# write the post-script calls to the post-ambles:
for postscript in self._postscripts:
print(postscript)
self._postambles.append(f'python {postscript[0]} {" ".join(postscript[1])}')
# setup the job and check if it was successfull
setup_success = self._setup_job()

Expand Down Expand Up @@ -155,9 +160,19 @@ def add_postamble(self, line: str):
'''
self._postambles.append(line)

def add_postscript(self, script):
self.add_postamble(f'cd {self.workdir}')
self.add_postamble(f'python {script.__file__}')
def add_postscript(self, script, *args):
'''
Add a post-script to this calculation.
This should be either a Python module with a __file__ attribute or the path to a Python script.
The post-script will be called with Python and any given args will be added as arguments when calling the script.
Args:
script: a Python object with a __file__ attribute or the file-path to a script.
*args: positional arguments to pass to the post-script.
'''
if not isinstance(script, str):
script = script.__file__
self._postscripts.append((script, args))

def dependency(self, otherjob: 'Job'):
'''
Expand Down

0 comments on commit d4e1208

Please sign in to comment.