Skip to content

Commit

Permalink
Fix log compression bug when no logs specified
Browse files Browse the repository at this point in the history
  • Loading branch information
blimlim authored Dec 17, 2024
1 parent 0c2a837 commit 20a8e76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions payu/models/cice.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ def get_log_files(self):
-------
log_files: list of paths to model log files.
"""
if not self.logs_to_compress:
return

log_files = []
for filename in os.listdir(self.work_path):
if re.match("|".join(self.logs_to_compress), filename):
Expand All @@ -343,6 +346,9 @@ def compress_log_files(self):
Compress model log files into tarball.
"""
log_files = self.get_log_files()
if not log_files:
return

with tarfile.open(name=os.path.join(self.work_path, self.log_tar_name),
mode="w:gz") as tar:
for file in log_files:
Expand Down
34 changes: 34 additions & 0 deletions test/models/test_cice.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,37 @@ def test_log_compression(config, cice4_log_files, non_log_file,
with tar.extractfile(entry) as open_entry:
file_contents = open_entry.read().decode("utf-8")
assert file_contents == cice4_log_files[entry_name]


@pytest.mark.parametrize("config", [CONFIG_WITH_COMPRESSION],
indirect=True)
def test_log_compression_no_logs(config, cice4_log_files, non_log_file,
cice_nml # Required by expt.__init__
):
"""
Check that log compression does nothing when no logfiles are
specifed.
"""
with cd(ctrldir):
# Initialise laboratory and experiment
lab = payu.laboratory.Laboratory(lab_path=str(labdir))
expt = payu.experiment.Experiment(lab, reproduce=False)

model = expt.models[0]

initial_workdir_contents = dir_contents_and_dates(expt_workdir)
# Specify no files for compression
model.logs_to_compress = []
# Function to test
model.compress_log_files()

final_workdir_contents = dir_contents_and_dates(expt_workdir)
assert final_workdir_contents == initial_workdir_contents


def dir_contents_and_dates(dir_path):
"""
Return a dict of filenames and their modification dates.
"""
return {name: os.path.getmtime(os.path.join(dir_path, name))
for name in os.listdir(dir_path)}

0 comments on commit 20a8e76

Please sign in to comment.