jobfile.py 1.66 KB
Newer Older
1 2
import yaml
import os
3
import subprocess
Lukas Weber's avatar
Lukas Weber committed
4
import errno
5

Lukas Weber's avatar
Lukas Weber committed
6 7 8 9 10
try:
    from yaml import CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeLoader

11 12 13 14
'''Helpers for handling loadleveller jobfiles/scripts. For lack of a better idea, the job description files of loadleveller are actually executables that output a more verbose yaml parameter file to stdout. Use the taskmaker module to write the input scripts.'''

class JobFileGenError(Exception):
    pass
15 16 17

class JobFile:
    def __init__(self, filename):
18 19 20
        env = dict(os.environ)
        env['PATH'] += ':.'
        result = subprocess.run([filename], stdout=subprocess.PIPE, env=env)
21 22 23 24 25 26

        self.raw_jobfile = result.stdout.decode('utf-8')
        if result.returncode != 0:
            raise JobFileGenError('Generation script "{}" had a non-zero return code. Treating as error.'.format(filename))
 
        try:
Lukas Weber's avatar
Lukas Weber committed
27
            parsed_job = yaml.load(self.raw_jobfile, Loader=SafeLoader)
28 29 30 31 32 33 34 35 36 37 38 39
            self.__dict__.update(parsed_job)
        except Exception as e: 
            raise JobFileGenError('Could not parse job generation script output: {}'.format(e))

    def write_job_input_file(self):
        try:
            datadir = self.jobname + '.data'
            try:
                os.makedirs(datadir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
40
            job_input_filename = os.path.join(datadir, 'parameters.yml')
41 42 43
            with open(job_input_filename, 'w') as f:
                f.write(self.raw_jobfile)
        except Exception as e:
44
            raise JobFileGenError('Could not write parameters.yml: {}'.format(e))
45 46

        return job_input_filename