Skip to content

Commit

Permalink
Run generator improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sjpfenninger committed Mar 6, 2018
1 parent 8977b85 commit be5e8c3
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions calliope/core/util/generate_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,30 @@ def generate_runs(model_file, override_file, groups=None, additional_args=None):
return commands


def generate_bash_script(out_file, model_file, override_file, groups, additional_args=None):
base_string = '{i}) {cmd} ;;\n'
lines_start = ['#!/bin/sh', '', 'case "$1" in', '']
lines_end = ['esac', '']

def generate_bash_script(out_file, model_file, override_file, groups, additional_args=None, **kwargs):
commands = generate_runs(model_file, override_file, groups, additional_args)

base_string = ' {i}) {cmd} ;;\n'
lines_start = [
'#!/bin/sh',
'',
'function process_case () {',
' case "$1" in',
''
]
lines_end = [
' esac', '}',
'',
'if [[ $# -eq 0 ]] ; then',
' echo No parameter given, running all runs sequentially...',
' for i in $(seq 1 {}); do process_case $i; done'.format(len(commands)),
'else',
' echo Running run $1',
' process_case $1',
'fi',
'',
]

lines_all = lines_start + [base_string.format(i=i+1, cmd=cmd) for i, cmd in enumerate(commands)] + lines_end

with open(out_file, 'w') as f:
Expand All @@ -63,7 +80,9 @@ def generate_bash_script(out_file, model_file, override_file, groups, additional
return commands


def generate_bsub_script(out_file, model_file, override_file, groups, additional_args, cluster_mem, cluster_time, cluster_threads=1):
def generate_bsub_script(out_file, model_file, override_file, groups, additional_args, cluster_mem, cluster_time, cluster_threads=1, **kwargs):

# We also need to generate the bash script to run on the cluster
bash_out_file = out_file + '.array.sh'
bash_out_file_basename = os.path.basename(bash_out_file)
commands = generate_bash_script(bash_out_file, model_file, override_file, groups, additional_args)
Expand All @@ -85,10 +104,30 @@ def generate_bsub_script(out_file, model_file, override_file, groups, additional
f.write('\n'.join(lines))


def generate_windows_script(out_file, model_file, override_file, groups, additional_args=None, **kwargs):
commands = generate_runs(model_file, override_file, groups, additional_args)

# \r\n are Windows line endings
base_string = 'echo "Run {i}"\r\n{cmd}\r\n'
lines_start = [
'@echo off',
'',
]

lines_all = lines_start + [base_string.format(i=i+1, cmd=cmd) for i, cmd in enumerate(commands)]

with open(out_file, 'w') as f:
f.write('\r\n'.join(lines_all))

os.chmod(out_file, 0o755)

return commands


_KINDS = {
'bash': generate_bash_script,
'bsub': generate_bsub_script
# 'windows': generate_windows_script,
'bsub': generate_bsub_script,
'windows': generate_windows_script,
}


Expand Down

0 comments on commit be5e8c3

Please sign in to comment.