From e4224c8db21c7de3613294d8ad9ed1bf6d50e561 Mon Sep 17 00:00:00 2001 From: Jukka Date: Thu, 12 Dec 2024 14:32:24 +0200 Subject: [PATCH] Add safety feature to avoid overwriting of simulation results Prompts user if existing path includes result files. Raises error if permission to overwrite results is denied. Modify `run_export_script` to print the output of the subprocess on the fly, so that the prompt question is shown. --- .../simulations/export/export_and_run.py | 24 +++++++++---------- .../python/kqcircuits/util/export_helper.py | 5 ++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/klayout_package/python/kqcircuits/simulations/export/export_and_run.py b/klayout_package/python/kqcircuits/simulations/export/export_and_run.py index 537b19160..e6ff7c431 100644 --- a/klayout_package/python/kqcircuits/simulations/export/export_and_run.py +++ b/klayout_package/python/kqcircuits/simulations/export/export_and_run.py @@ -73,22 +73,20 @@ def run_export_script(export_script: Path, export_path: Path, quiet: bool = Fals + args + (["-q"] if quiet else []) ) - # Run export script and capture stdout to be processed - with subprocess.Popen(export_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) as process: - process_stdout, process_stderr = process.communicate() - print(process_stdout) - print(process_stderr, file=sys.stderr) + # Run export script and capture script_export_paths to be processed + script_export_paths = [] + with subprocess.Popen(export_cmd, stdout=subprocess.PIPE, text=True) as process: + for output in process.stdout: + # Print the output of the subprocess on the fly + print(output, end="") + + # Parse export paths from output printed in `create_or_empty_tmp_directory` + if output.strip().startswith(EXPORT_PATH_IDENTIFIER): + script_export_paths.append(Path(output.strip().removeprefix(EXPORT_PATH_IDENTIFIER))) + if process.returncode: raise subprocess.CalledProcessError(process.returncode, export_cmd) - # Parse export paths from stdout printed in `create_or_empty_tmp_directory` - script_export_paths = [l.strip() for l in process_stdout.split("\n")] - script_export_paths = [ - Path(l.removeprefix(EXPORT_PATH_IDENTIFIER)) - for l in script_export_paths - if l.startswith(EXPORT_PATH_IDENTIFIER) - ] - # remove duplicate paths unique_paths = set() script_export_paths = [n for n in script_export_paths if not (n in unique_paths or unique_paths.add(n))] diff --git a/klayout_package/python/kqcircuits/util/export_helper.py b/klayout_package/python/kqcircuits/util/export_helper.py index 28c049a4d..67cfc2dff 100644 --- a/klayout_package/python/kqcircuits/util/export_helper.py +++ b/klayout_package/python/kqcircuits/util/export_helper.py @@ -349,6 +349,7 @@ def generate_probepoints_from_file( def create_or_empty_tmp_directory(dir_name): """Creates directory into TMP_PATH or removes its content if it exists. + Prompts user if existing path includes result files. Raises error if permission to overwrite results is denied. Returns directory path. """ @@ -368,6 +369,10 @@ def remove_content(path): dir_path = get_simulation_directory(dir_name) if dir_path.exists() and dir_path.is_dir(): + if any(f.endswith("_project_results.json") for f in os.listdir(dir_path)): + print(f"Do you want to overwrite the existing results in {dir_path}? (y to continue):") + if not input().lower().startswith("y"): + raise ValueError(f"The permission to overwrite existing results in {dir_path} is denied by user.") remove_content(dir_path) else: dir_path.mkdir()