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

6
7
8
9
'''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
10
11
12

class JobFile:
    def __init__(self, filename):
13
14
15
        env = dict(os.environ)
        env['PATH'] += ':.'
        result = subprocess.run([filename], stdout=subprocess.PIPE, env=env)
16
17
18
19
20
21

        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
22
            parsed_job = yaml.load(self.raw_jobfile, Loader=yaml.CSafeLoader)
23
24
25
26
27
28
29
30
31
32
33
34
            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
35
            job_input_filename = os.path.join(datadir, 'parameters.yml')
36
37
38
            with open(job_input_filename, 'w') as f:
                f.write(self.raw_jobfile)
        except Exception as e:
39
            raise JobFileGenError('Could not write parameters.yml: {}'.format(e))
40
41

        return job_input_filename