From 58d0f51f8f72995f14bab07995b5d6f5b10a4397 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Wed, 20 Sep 2023 08:34:26 +0200 Subject: [PATCH] fix(python): prevented app crashes on "normal" python prints Now we check if the output was a file before using it, and also prevent an app crash if the file is unusable. --- runner/scripts/em_python.py | 4 +++- runner/scripts/task_runner.py | 45 +++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/runner/scripts/em_python.py b/runner/scripts/em_python.py index 636dd76f..8af2a99d 100644 --- a/runner/scripts/em_python.py +++ b/runner/scripts/em_python.py @@ -70,7 +70,9 @@ def run(self) -> Optional[List[Path]]: # debug statements output = ast.literal_eval(self.output.splitlines()[-1]) if isinstance(output, List): - return output + # check if they are existing + valid_paths = [x for x in output if Path(x).is_file()] + return valid_paths or None except BaseException: # will raise an exception on some output. pass diff --git a/runner/scripts/task_runner.py b/runner/scripts/task_runner.py index ea09f23e..acea1e1e 100644 --- a/runner/scripts/task_runner.py +++ b/runner/scripts/task_runner.py @@ -729,24 +729,33 @@ def __process(self) -> None: f"Processing script generated files:\n{output}", ) - # convert the files into a set of named temp files and remove them. - self.source_files = [] - for file in output: - original = Path(file) - original_name = str(original.absolute()) - with tempfile.NamedTemporaryFile( - mode="wb+", delete=False, dir=self.temp_path - ) as data_file: - # write contents - data_file.write(original.read_bytes()) - - # set name and remove original - original.unlink() - - os.link(data_file.name, original_name) - - data_file.name = original_name # type: ignore[misc] - self.source_files.append(data_file) + try: + # convert the files into a set of named temp files and remove them. + self.source_files = [] + for file in output: + original = Path(file) + original_name = str(original.absolute()) + with tempfile.NamedTemporaryFile( + mode="wb+", delete=False, dir=self.temp_path + ) as data_file: + # write contents + data_file.write(original.read_bytes()) + + # set name and remove original + original.unlink() + + os.link(data_file.name, original_name) + + data_file.name = original_name # type: ignore[misc] + self.source_files.append(data_file) + + except BaseException as e: + raise RunnerException( + self.task, + self.run_id, + 8, + f"Failed to load files from processing script output:\n{e}", + ) def __store_files(self) -> None: if not self.source_files or len(self.source_files) == 0: