Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log compression bug when no logs specified #542

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}
Loading