Skip to content

Commit 20a8e76

Browse files
authored
Fix log compression bug when no logs specified
1 parent 0c2a837 commit 20a8e76

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

payu/models/cice.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ def get_log_files(self):
332332
-------
333333
log_files: list of paths to model log files.
334334
"""
335+
if not self.logs_to_compress:
336+
return
337+
335338
log_files = []
336339
for filename in os.listdir(self.work_path):
337340
if re.match("|".join(self.logs_to_compress), filename):
@@ -343,6 +346,9 @@ def compress_log_files(self):
343346
Compress model log files into tarball.
344347
"""
345348
log_files = self.get_log_files()
349+
if not log_files:
350+
return
351+
346352
with tarfile.open(name=os.path.join(self.work_path, self.log_tar_name),
347353
mode="w:gz") as tar:
348354
for file in log_files:

test/models/test_cice.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,37 @@ def test_log_compression(config, cice4_log_files, non_log_file,
595595
with tar.extractfile(entry) as open_entry:
596596
file_contents = open_entry.read().decode("utf-8")
597597
assert file_contents == cice4_log_files[entry_name]
598+
599+
600+
@pytest.mark.parametrize("config", [CONFIG_WITH_COMPRESSION],
601+
indirect=True)
602+
def test_log_compression_no_logs(config, cice4_log_files, non_log_file,
603+
cice_nml # Required by expt.__init__
604+
):
605+
"""
606+
Check that log compression does nothing when no logfiles are
607+
specifed.
608+
"""
609+
with cd(ctrldir):
610+
# Initialise laboratory and experiment
611+
lab = payu.laboratory.Laboratory(lab_path=str(labdir))
612+
expt = payu.experiment.Experiment(lab, reproduce=False)
613+
614+
model = expt.models[0]
615+
616+
initial_workdir_contents = dir_contents_and_dates(expt_workdir)
617+
# Specify no files for compression
618+
model.logs_to_compress = []
619+
# Function to test
620+
model.compress_log_files()
621+
622+
final_workdir_contents = dir_contents_and_dates(expt_workdir)
623+
assert final_workdir_contents == initial_workdir_contents
624+
625+
626+
def dir_contents_and_dates(dir_path):
627+
"""
628+
Return a dict of filenames and their modification dates.
629+
"""
630+
return {name: os.path.getmtime(os.path.join(dir_path, name))
631+
for name in os.listdir(dir_path)}

0 commit comments

Comments
 (0)