Skip to content

Commit

Permalink
Add safety feature to avoid overwriting of simulation results
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jukkarabina committed Dec 13, 2024
1 parent 71dae89 commit e4224c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
5 changes: 5 additions & 0 deletions klayout_package/python/kqcircuits/util/export_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Expand All @@ -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()
Expand Down

0 comments on commit e4224c8

Please sign in to comment.