jobfile.py 1.54 KB
Newer Older
1
import json
2
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
'''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 json parameter file to stdout. Use the taskmaker module to write the input scripts.'''
8
9
10

class JobFileGenError(Exception):
    pass
11
12
13

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

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

        return job_input_filename