Skip to content

Commit c7c0ea6

Browse files
authored
fix implementation of overwrite flag in src/output_manager.py (#760)
1 parent b3fedb3 commit c7c0ea6

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/output_manager.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,35 @@ def copy_to_output(self):
534534
self.WORK_DIR, self.OUT_DIR)
535535
try:
536536
if os.path.exists(self.OUT_DIR):
537-
if not self.overwrite:
537+
if self.overwrite:
538+
# if overwrite flag is true, replace OUT_DIR contents with WORK_DIR
538539
self.obj.log.error("%s: '%s' exists, overwriting.",
539540
self.obj.full_name, self.OUT_DIR)
540-
shutil.rmtree(self.OUT_DIR)
541+
shutil.rmtree(self.OUT_DIR)
542+
shutil.move(self.WORK_DIR, self.OUT_DIR)
543+
return
544+
elif not self.overwrite:
545+
# if ovewrite flag is false, find the next suitable 'MDTF_output.v#' dir to write to
546+
if not os.path.exists(os.path.join(self.OUT_DIR, 'index.html')):
547+
# this will catch the majority of cases
548+
shutil.rmtree(self.OUT_DIR)
549+
shutil.move(self.WORK_DIR, self.OUT_DIR)
550+
return
551+
# the rest of this if statement is not strictly necessary, but may be useful for fringe edge cases
552+
# if some reason a index.html already exists in self.OUT_DIR, it will move to the next .v#
553+
out_main_dir = os.path.abspath(os.path.join(self.OUT_DIR, ".."))
554+
v_dirs = [d for d in os.listdir(out_main_dir) if 'MDTF_output.v' in d]
555+
if not v_dirs:
556+
NEW_BASE = 'MDTF_output.v1'
557+
v_nums = sorted([int(''.join(filter(str.isdigit, d))) for d in v_dirs], reverse=True)
558+
NEW_BASE = f'MDTF_output.v{v_nums[0]+1}'
559+
self.OUT_DIR = os.path.join(out_main_dir, NEW_BASE)
560+
if os.path.isdir(self.OUT_DIR):
561+
shutil.rmtree(self.OUT_DIR)
562+
shutil.move(self.WORK_DIR, self.OUT_DIR)
563+
return
541564
except Exception:
542565
raise
543-
shutil.move(self.WORK_DIR, self.OUT_DIR)
544566

545567
def verify_pod_links(self, pod):
546568
"""Check for missing files linked to from POD's html page.

src/util/path_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ def __init__(self, config: NameSpace = None,
6060
self.OUTPUT_DIR = os.path.join(self._init_path('OUTPUT_DIR', config, env=env))
6161

6262
if new_work_dir:
63+
output_dir_main = os.path.abspath(os.path.join(self.OUTPUT_DIR, ".."))
6364
self.WORK_DIR, ver = filesystem.bump_version(
64-
self.WORK_DIR, extra_dirs=[self.OUTPUT_DIR])
65+
self.WORK_DIR, extra_dirs=[output_dir_main])
6566
self.OUTPUT_DIR, _ = filesystem.bump_version(self.OUTPUT_DIR, new_v=ver)
6667

6768
# set root directory for TempDirManager

0 commit comments

Comments
 (0)