Commit 738dd515 authored by laochailan's avatar laochailan
Browse files

add warning for overwriting existing parameters.

parent 0fd798b9
...@@ -62,7 +62,20 @@ def run(): ...@@ -62,7 +62,20 @@ def run():
except StopIteration: except StopIteration:
pass pass
job_input_filename = job.write_job_input_file() try:
job_input_filename = job.write_job_input_file(force_overwrite=args_run.force)
except jobfile.JobFileGenError as e:
print('Error: {}'.format(e))
sys.exit(1)
except jobfile.JobFileOverwriteError as e:
diff_summary = ''
for taskname, taskdiff in e.difference.items():
diff_summary += '{}:\n'.format(taskname)
for param, diff in taskdiff.items():
diff_summary += ' {}: {} -> {}\n'.format(param, diff[1], diff[0])
print('Error: You changed the job parameters of an existing simulation.\nSummary of changes:\n{}\nThis can be no problem or data-breaking depending on the situation. Use \'--force\' to force overwriting the parameters or \'--restart\' to start a blank run.'.format(diff_summary))
sys.exit(1)
if args_run.single: if args_run.single:
cmd = '{} single "{}"'.format(job.jobconfig['mc_binary'], job_input_filename) cmd = '{} single "{}"'.format(job.jobconfig['mc_binary'], job_input_filename)
print('$ '+cmd) print('$ '+cmd)
......
...@@ -2,6 +2,7 @@ import json ...@@ -2,6 +2,7 @@ import json
import os import os
import subprocess import subprocess
import errno import errno
from collections import defaultdict
'''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.''' '''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.'''
...@@ -9,6 +10,10 @@ import errno ...@@ -9,6 +10,10 @@ import errno
class JobFileGenError(Exception): class JobFileGenError(Exception):
pass pass
class JobFileOverwriteError(Exception):
def __init__(self, difference):
self.difference = difference
class JobFile: class JobFile:
def __init__(self, filename): def __init__(self, filename):
env = dict(os.environ) env = dict(os.environ)
...@@ -25,8 +30,19 @@ class JobFile: ...@@ -25,8 +30,19 @@ class JobFile:
except Exception as e: except Exception as e:
raise JobFileGenError('Could not parse job generation script output: {}'.format(e)) raise JobFileGenError('Could not parse job generation script output: {}'.format(e))
def write_job_input_file(self): def _compare_to_old(self, old_data):
try: diff = defaultdict(dict)
for taskname, task in self.tasks.items():
if taskname in old_data['tasks']:
old_task = old_data['tasks'][taskname]
for param, value in task.items():
if param not in old_task:
diff[taskname][param] = (value, None)
elif value != old_task[param]:
diff[taskname][param] = (value, old_task[param])
return diff
def write_job_input_file(self, force_overwrite):
datadir = self.jobname + '.data' datadir = self.jobname + '.data'
try: try:
os.makedirs(datadir) os.makedirs(datadir)
...@@ -34,9 +50,18 @@ class JobFile: ...@@ -34,9 +50,18 @@ class JobFile:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise
job_input_filename = os.path.join(datadir, 'parameters.json') job_input_filename = os.path.join(datadir, 'parameters.json')
if os.path.isfile(job_input_filename):
with open(job_input_filename, 'r') as f:
old_data = json.load(f)
diff = self._compare_to_old(old_data)
if not force_overwrite and len(diff) > 0:
raise JobFileOverwriteError(difference=diff)
try:
with open(job_input_filename, 'w') as f: with open(job_input_filename, 'w') as f:
f.write(self.raw_jobfile) f.write(self.raw_jobfile)
except Exception as e: except JobFileGenError as e:
raise JobFileGenError('Could not write parameters.json: {}'.format(e)) raise JobFileGenError('Could not write parameters.json: {}'.format(e))
return job_input_filename return job_input_filename
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment