|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) 2024-2025 The WfCommons Team. |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import shutil |
| 13 | + |
| 14 | +from logging import Logger |
| 15 | +from typing import Optional, Union |
| 16 | + |
| 17 | +from .abstract_translator import Translator |
| 18 | +from ...common import Workflow |
| 19 | + |
| 20 | +this_dir = pathlib.Path(__file__).resolve().parent |
| 21 | + |
| 22 | +class MakeflowTranslator(Translator): |
| 23 | + """ |
| 24 | + A WfFormat parser for creating Makeflow workflow applications. |
| 25 | +
|
| 26 | + :param workflow: Workflow benchmark object or path to the workflow benchmark JSON instance. |
| 27 | + :type workflow: Union[Workflow, pathlib.Path], |
| 28 | + :param logger: The logger where to log information/warning or errors (optional). |
| 29 | + :type logger: Logger |
| 30 | + """ |
| 31 | + def __init__(self, |
| 32 | + workflow: Union[Workflow, pathlib.Path], |
| 33 | + logger: Optional[Logger] = None) -> None: |
| 34 | + """Create an object of the translator.""" |
| 35 | + super().__init__(workflow, logger) |
| 36 | + self._script = "" |
| 37 | + |
| 38 | + def translate(self, output_folder: pathlib.Path) -> None: |
| 39 | + """ |
| 40 | + Translate a workflow benchmark description (WfFormat) into an actual workflow application. |
| 41 | +
|
| 42 | + :param output_folder: The path to the folder in which the workflow benchmark will be generated. |
| 43 | + :type output_folder: pathlib.Path |
| 44 | + """ |
| 45 | + |
| 46 | + # Generate code |
| 47 | + self._generate_code() |
| 48 | + |
| 49 | + # write benchmark files |
| 50 | + output_folder.mkdir(parents=True) |
| 51 | + with open(output_folder.joinpath("workflow.makeflow"), "w") as fp: |
| 52 | + fp.write(self._script) |
| 53 | + |
| 54 | + # additional files |
| 55 | + self._copy_binary_files(output_folder) |
| 56 | + self._generate_input_files(output_folder) |
| 57 | + |
| 58 | + # README file |
| 59 | + self._write_readme_file(output_folder) |
| 60 | + |
| 61 | + def _generate_code(self): |
| 62 | + """ |
| 63 | + Generate the Makeflow code |
| 64 | +
|
| 65 | + :return: the code |
| 66 | + :rtype: str |
| 67 | + """ |
| 68 | + self._script = "# Makeflow workflow specification\n\n" |
| 69 | + for task_name, task in self.workflow.tasks.items(): |
| 70 | + make_clause = "" |
| 71 | + # output files |
| 72 | + for output_file in task.output_files: |
| 73 | + make_clause += f"data/{output_file.file_id} " |
| 74 | + make_clause += ": " |
| 75 | + # input files |
| 76 | + for input_file in task.input_files: |
| 77 | + make_clause += f"data/{input_file.file_id} " |
| 78 | + make_clause += "\n" |
| 79 | + # Command |
| 80 | + make_clause += "\t" |
| 81 | + make_clause += task.program + " " |
| 82 | + |
| 83 | + input_spec = "\"[" |
| 84 | + for file in task.input_files: |
| 85 | + input_spec += f"\\\\\"data/{file.file_id}\\\\\"," |
| 86 | + input_spec = input_spec[:-1] + "]\"" |
| 87 | + |
| 88 | + output_spec = "\"{" |
| 89 | + for file in task.output_files: |
| 90 | + output_spec += f"\\\\\"data/{file.file_id}\\\\\":{str(file.size)}," |
| 91 | + output_spec = output_spec[:-1] + "}\"" |
| 92 | + |
| 93 | + args = [] |
| 94 | + for a in task.args: |
| 95 | + if "--output-files" in a: |
| 96 | + args.append(f"--output-files {output_spec}") |
| 97 | + elif "--input-files" in a: |
| 98 | + args.append(f"--input-files {input_spec}") |
| 99 | + else: |
| 100 | + args.append(a) |
| 101 | + |
| 102 | + args = " ".join(f"{a}" for a in args) |
| 103 | + make_clause += args + "\n" |
| 104 | + self._script += make_clause + "\n\n" |
| 105 | + return |
| 106 | + |
| 107 | + def _write_readme_file(self, output_folder: pathlib.Path) -> None: |
| 108 | + """ |
| 109 | + Write the README file. |
| 110 | +
|
| 111 | + :param output_folder: The path of the output folder. |
| 112 | + :type output_folder: pathlib.Path |
| 113 | + """ |
| 114 | + readme_file_path = output_folder.joinpath("README") |
| 115 | + with open(readme_file_path, "w") as out: |
| 116 | + out.write(f"In directory {str(output_folder)}:\n") |
| 117 | + out.write(f" - The Makeflow input file: workflow.makeflow\n") |
| 118 | + out.write(f" - Run the workflow: makeflow workflow.makeflow\n") |
0 commit comments