Skip to content

Commit 1218e2a

Browse files
committed
Some robustness changes picked up to pass testing
The tests required some changes to the configuration and stage config paths. Some minor changes to the tests to account for slightly changed behaviours.
1 parent 4cf0e1b commit 1218e2a

File tree

2 files changed

+79
-74
lines changed

2 files changed

+79
-74
lines changed

payu/models/staged_cable.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def setup(self):
4646
# Initialise the configuration log
4747
self.configuration_log = {}
4848

49-
if not os.path.isfile('configuration_log.yaml'):
49+
conf_log_p = os.path.join(self.control_path, 'configuration_log.yaml')
50+
if not os.path.isfile(conf_log_p):
5051
# Build a new configuration log
5152
self._build_new_configuration_log()
5253
else:
@@ -60,18 +61,14 @@ def setup(self):
6061
# Make the logging directory
6162
mkdir_p(os.path.join(self.work_path, "logs"))
6263

63-
# Get the additional restarts from older restart dirs
64-
# self._get_further_restarts()
65-
66-
# Make necessary adjustments to the configuration log
67-
# self._handle_configuration_log_setup()
68-
6964
self._set_current_stage()
65+
7066
def _build_new_configuration_log(self):
7167
"""Build a new configuration log for the first stage of the run."""
7268

69+
stage_conf_p = os.path.join(self.control_path, 'stage_config.yaml')
7370
# Read the stage_config.yaml file
74-
with open('stage_config.yaml', 'r') as stage_conf_f:
71+
with open(stage_conf_p, 'r') as stage_conf_f:
7572
self.stage_config = yaml.safe_load(stage_conf_f)
7673

7774
# On the first run, we need to read the 'stage_config.yaml' file.
@@ -86,9 +83,12 @@ def _build_new_configuration_log(self):
8683

8784
def _read_configuration_log(self):
8885
"""Read the existing configuration log."""
89-
with open('configuration_log.yaml') as conf_log_file:
86+
conf_log_p = os.path.join(self.control_path, 'configuration_log.yaml')
87+
with open(conf_log_p, 'r') as conf_log_file:
9088
self.configuration_log = yaml.safe_load(conf_log_file)
9189

90+
print(f"After reading configuration_log: {self.configuration_log}")
91+
9292
def _prepare_configuration(self):
9393
"""Prepare the stages in the CABLE configuration."""
9494

@@ -231,7 +231,8 @@ def _set_current_stage(self):
231231
self.configuration_log['queued_stages'].pop(0)
232232

233233
self._save_configuration_log()
234-
shutil.copy('configuration_log.yaml', self.work_path)
234+
conf_log_p = os.path.join(self.control_path, 'configuration_log.yaml')
235+
shutil.copy(conf_log_p, self.work_path)
235236

236237
def archive(self):
237238
"""Store model output to laboratory archive and update the
@@ -250,9 +251,10 @@ def archive(self):
250251
"number of queued stages in the configuration log.")
251252
self.expt.n_runs = remaining_stages
252253

253-
if len(self.configuration_log["queued_stages"]) == 0:
254+
conf_log_p = os.path.join(self.control_path, 'configuration_log.yaml')
255+
if self.expt.n_runs == 0:
254256
# Configuration successfully completed
255-
os.remove('configuration_log.yaml')
257+
os.remove(conf_log_p)
256258

257259
super(StagedCable, self).archive()
258260

@@ -261,26 +263,28 @@ def _collect_restarts(self):
261263
merge of the files in work_path/restart and in prior_restart_path, with
262264
the files in work_path/restart taking precedence."""
263265

264-
# Move the files in work_path/restart first
265-
for f in os.listdir(self.work_restart_path):
266-
shutil.move(os.path.join(self.work_restart_path, f),
267-
self.restart_path)
268-
os.rmdir(self.work_restart_path)
269-
270-
# Now collect any restarts from prior_restart_path only if said restart
271-
# doesn't already exist (except on first run)
266+
# First, collect restarts which do not have a newer version (when the
267+
# counter is greater than 0)
272268
if self.expt.counter > 0:
273-
# self.prior_restart_path nor expt.prior_restart_path are defined here
274-
# build it as in experiment.py
275269
prior_restart_dir = 'restart{0:03}'.format(self.expt.counter - 1)
276270
prior_restart_path = os.path.join(self.expt.archive_path,
277271
prior_restart_dir)
278272

279-
current_restarts = os.listdir(self.restart_path)
273+
# For each restart, check if newer version was created. If not,
274+
# copy into the work restart path.
275+
generated_restarts = os.listdir(self.work_restart_path)
276+
280277
for f in os.listdir(prior_restart_path):
281-
if f not in current_restarts:
278+
if f not in generated_restarts:
282279
shutil.copy(os.path.join(prior_restart_path, f),
283-
self.restart_path)
280+
self.work_restart_path)
281+
282+
283+
# Move the files in work_path/restart first
284+
for f in os.listdir(self.work_restart_path):
285+
shutil.move(os.path.join(self.work_restart_path, f),
286+
self.restart_path)
287+
os.rmdir(self.work_restart_path)
284288

285289
def _archive_current_stage(self):
286290
"""Move the current stage to the list of completed stages."""
@@ -291,12 +295,14 @@ def _archive_current_stage(self):
291295
self._save_configuration_log()
292296

293297
# Copy the configuration log to the restart directory for shareability
294-
shutil.copy('configuration_log.yaml', self.restart_path)
298+
conf_log_p = os.path.join(self.control_path, 'configuration_log.yaml')
299+
shutil.copy(conf_log_p, self.restart_path)
295300

296301
def collate(self):
297302
pass
298303

299304
def _save_configuration_log(self):
300305
"""Write the updated configuration log back to the staging area."""
301-
with open('configuration_log.yaml', 'w+') as config_log_f:
306+
conf_log_p = os.path.join(self.control_path, 'configuration_log.yaml')
307+
with open(conf_log_p, 'w+') as config_log_f:
302308
yaml.dump(self.configuration_log, config_log_f)

test/models/test_staged_cable.py

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def setup_module(module):
3333
ctrldir.mkdir()
3434
expt_workdir.mkdir(parents=True)
3535
archive_dir.mkdir()
36+
restart_dir = archive_dir / 'ctrl' / 'restart000'
37+
restart_dir.mkdir(parents=True)
3638
except Exception as e:
3739
print(e)
3840

@@ -107,10 +109,10 @@ def teardown_module(module):
107109
if verbose:
108110
print("teardown_module module:%s" % module.__name__)
109111

110-
try:
111-
shutil.rmtree(tmpdir)
112-
except Exception as e:
113-
print(e)
112+
# try:
113+
# shutil.rmtree(tmpdir)
114+
# except Exception as e:
115+
# print(e)
114116

115117

116118
def test_staged_cable():
@@ -123,50 +125,47 @@ def test_staged_cable():
123125
expt = payu.experiment.Experiment(lab, reproduce=False)
124126
model = expt.models[0]
125127

126-
# Since we've called the initialiser, we should be able to inspect the
127-
# stages immediately (through the configuration log)
128-
expected_queued_stages = [
129-
'stage_1',
130-
'stage_2',
131-
'stage_3',
132-
'stage_4',
133-
'stage_3',
134-
'stage_3',
135-
'stage_5',
136-
'stage_6',
137-
'stage_6',
138-
'stage_7']
139-
assert model.configuration_log['queued_stages'] == expected_queued_stages
140-
141-
# Now prepare for a stage- should see changes in the configuration log
142-
# and the patched namelist in the workdir
143-
model.setup()
144-
expected_current_stage = expected_queued_stages.pop(0)
145-
assert model.configuration_log['current_stage'] == expected_current_stage
146-
assert model.configuration_log['queued_stages'] == expected_queued_stages
147-
148-
# Now check the namelist
149-
expected_namelist = {
150-
'cablenml': {
151-
'option1': 10,
152-
'struct1': {
153-
'option2': 20,
154-
'option3': 3,
155-
'option5': 50
156-
},
157-
'option4': 4,
158-
'option6': 60
128+
# Now prepare for a stage- should see changes in the configuration log
129+
# and the patched namelist in the workdir
130+
model.setup()
131+
132+
# Since we've called the initialiser, we should be able to inspect the
133+
# stages immediately (through the configuration log)
134+
expected_queued_stages = [
135+
'stage_2',
136+
'stage_3',
137+
'stage_4',
138+
'stage_3',
139+
'stage_3',
140+
'stage_5',
141+
'stage_6',
142+
'stage_6',
143+
'stage_7']
144+
assert model.configuration_log['queued_stages'] == expected_queued_stages
145+
assert model.configuration_log['current_stage'] == 'stage_1'
146+
147+
# Now check the namelist
148+
expected_namelist = {
149+
'cablenml': {
150+
'option1': 10,
151+
'struct1': {
152+
'option2': 20,
153+
'option3': 3,
154+
'option5': 50
155+
},
156+
'option4': 4,
157+
'option6': 60
158+
}
159159
}
160-
}
161160

162-
with open(expt_workdir / 'cable.nml') as stage_nml_f:
163-
stage_nml = f90nml.read(stage_nml_f)
161+
with open(expt_workdir / 'cable.nml') as stage_nml_f:
162+
stage_nml = f90nml.read(stage_nml_f)
164163

165-
assert stage_nml == expected_namelist
164+
assert stage_nml == expected_namelist
166165

167-
# Archive the stage and make sure the configuration log is correct
168-
model.archive()
169-
expected_comp_stages = [expected_current_stage]
170-
expected_current_stage = ''
171-
assert model.configuration_log['completed_stages'] == expected_comp_stages
172-
assert model.configuration_log['current_stage'] == expected_current_stage
166+
# Archive the stage and make sure the configuration log is correct
167+
model.archive()
168+
expected_comp_stages = ['stage_1']
169+
expected_current_stage = ''
170+
assert model.configuration_log['completed_stages'] == expected_comp_stages
171+
assert model.configuration_log['current_stage'] == expected_current_stage

0 commit comments

Comments
 (0)