diff --git a/xfel_calibrate/calibrate.py b/xfel_calibrate/calibrate.py index 730283b956bde37fae107baeb3f52d63028bdace..0edd6f6ba773ccff1088f802612a89ae3c1d2066 100755 --- a/xfel_calibrate/calibrate.py +++ b/xfel_calibrate/calibrate.py @@ -19,6 +19,7 @@ import warnings from .settings import * from .notebooks import notebooks from jinja2 import Template +import stat import textwrap from .finalize import tex_escape @@ -491,6 +492,36 @@ def set_figure_format(nb, enable_vector_format): cell.source += "\n%config InlineBackend.figure_formats = ['svg']\n" +def create_finalize_script(fmt_args, temp_path, job_list): + """ + Create a finalize script to produce output report + :param fmt_args: Dictionary of fmt arguments + :param temp_path: Path to temopary folder to run slurm job + :param job_list: List of slurm jobs + """ + tmpl = Template(''' + #!/bin/tcsh + source /etc/profile.d/modules.sh + module load texlive + echo 'Running finalize script' + python3 -c "from xfel_calibrate.finalize import finalize; + finalize({{joblist}}, $1, '{{run_path}}', '{{out_path}}', + '{{project}}', '{{calibration}}', '{{author}}', + '{{version}}', '{{report_to}}', '{{in_folder}}' )" + + ''') + + fmt_args['joblist'] = job_list + f_name = "{}/finalize.sh".format(temp_path) + with open(f_name, "w") as finfile: + finfile.write(textwrap.dedent(tmpl.render(**fmt_args))) + + # change rights of the file to be: + # executed and writable for user, readable for user, group and others + all_stats = stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH + os.chmod(f_name, all_stats) + + def get_launcher_command(args, temp_path, dependent, job_list): """ Return a slurm launcher command @@ -529,7 +560,7 @@ def get_launcher_command(args, temp_path, dependent, job_list): launcher_slurm += " --mem {}G".format(args.get('slurm_mem', '500')) if dependent: - srun_dep = "--dependency=afterok" + srun_dep = " --dependency=afterok" for jobid in job_list: srun_dep += ":{}".format(jobid) launcher_slurm += srun_dep @@ -538,7 +569,7 @@ def get_launcher_command(args, temp_path, dependent, job_list): def concurrent_run(temp_path, nb, nbname, args, cparm=None, cval=None, - final_job=False, job_list=[], fmtcmd="", cluster_cores=8, + final_job=False, job_list=[], fmt_args={}, cluster_cores=8, sequential=False, dependent=False, show_title=True): """ Launch a concurrent job on the cluster via SLURM @@ -570,16 +601,7 @@ def concurrent_run(temp_path, nb, nbname, args, cparm=None, cval=None, # add finalization to the last job if final_job: - import stat - with open("{}/finalize.sh".format(temp_path), "w") as finfile: - finfile.write("#!/bin/tcsh\n") - finfile.write("source /etc/profile.d/modules.sh\n") - finfile.write("module load texlive\n") - finfile.write("echo 'Running finalize script'\n") - finfile.write( - "python3 -c {}\n".format(fmtcmd.format(joblist=job_list))) - all_stats = stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH - os.chmod("{}/finalize.sh".format(temp_path), all_stats) + create_finalize_script(fmt_args, temp_path, job_list) # then run an sbatch job srun_base = [] @@ -775,10 +797,6 @@ def run(): out_path)) os.makedirs(out_path, exist_ok=True) - cmd = ('"from xfel_calibrate import finalize; ' + - 'finalize.finalize({{joblist}}, $1, \'{run_path}\', \'{out_path}\', ' + - '\'{project}\', \'{calibration}\', \'{author}\', ' - '\'{version}\', \'{report_to}\', \'{in_folder}\' )"') report_to = title.replace(" ", "") if args["report_to"] is not None: @@ -786,10 +804,14 @@ def run(): folder = get_par_attr(parms, 'in_folder', 'value', '') - fmtcmd = cmd.format(run_path=run_tmp_path, out_path=out_path, - project=title, calibration=title, - author=author, version=version, - report_to=report_to, in_folder=folder) + fmt_args = {'run_path': run_tmp_path, + 'out_path': out_path, + 'project': title, + 'calibration': title, + 'author': author, + 'version': version, + 'report_to': report_to, + 'in_folder': folder} joblist = [] if concurrency.get("parameter", None) is None: @@ -798,7 +820,7 @@ def run(): jobid = concurrent_run(run_tmp_path, nb, os.path.basename(notebook), args, final_job=True, job_list=joblist, - fmtcmd=fmtcmd, + fmt_args=fmt_args, cluster_cores=cluster_cores, sequential=sequential) @@ -860,7 +882,7 @@ def run(): jobid = concurrent_run(run_tmp_path, nb, notebook, args, cvar, cval, final_job, - joblist, fmtcmd, + joblist, fmt_args, cluster_cores=cluster_cores, sequential=sequential, show_title=show_title) @@ -877,7 +899,7 @@ def run(): os.path.basename(notebook), args, final_job=final_job, - job_list=joblist, fmtcmd=fmtcmd, + job_list=joblist, fmt_args=fmt_args, cluster_cores=cluster_cores, sequential=sequential, dependent=True)