From 2097f6f665b954bcffc31723d18de038fff9310c Mon Sep 17 00:00:00 2001 From: Yuman Hordijk Date: Wed, 28 Feb 2024 17:49:19 +0100 Subject: [PATCH] Post scripts are now added when the job is being set up --- src/tcutility/job/generic.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/tcutility/job/generic.py b/src/tcutility/job/generic.py index 381f6097..3c0ead8a 100644 --- a/src/tcutility/job/generic.py +++ b/src/tcutility/job/generic.py @@ -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 @@ -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() @@ -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'): '''