Skip to content

Commit 9151944

Browse files
authored
Make sure that we can make a copy of dataset (#20)
1 parent a41f1d1 commit 9151944

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

src/environment_provider/iut/prepare.py

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def __init__(self, jsontas, prepare_ruleset):
3939
self.prepare_ruleset = prepare_ruleset
4040
self.jsontas = jsontas
4141
self.dataset = self.jsontas.dataset
42+
# Pop the config as it contains values that are not pickleable.
43+
# Pickle is used by deepcopy inside of 'dataset.copy' which is required
44+
# during the preparation step.
45+
self.config = self.dataset._Dataset__dataset.pop("config")
4246

4347
def execute_preparation_steps(self, iut, preparation_steps):
4448
"""Execute the preparation steps for the environment provider on an IUT.
@@ -48,21 +52,27 @@ def execute_preparation_steps(self, iut, preparation_steps):
4852
:param preparation_steps: Steps to execute to prepare an IUT.
4953
:type preparation_steps: dict
5054
"""
51-
with self.lock:
52-
dataset = self.dataset.copy()
53-
jsontas = JsonTas(dataset=dataset)
54-
steps = {}
55-
dataset.add("iut", iut)
56-
dataset.add("steps", steps)
57-
for step, definition in preparation_steps.items():
58-
definition = OrderedDict(**definition)
59-
self.logger.info("Executing step %r", step)
60-
step_result = jsontas.run(json_data=definition)
61-
self.logger.info("%r", step_result)
62-
if not step_result:
63-
self.logger.error("Failed to execute step %r", step)
64-
return False, iut
65-
steps[step] = step_result
55+
try:
56+
with self.lock:
57+
dataset = self.dataset.copy()
58+
dataset.add("config", self.config)
59+
jsontas = JsonTas(dataset=dataset)
60+
steps = {}
61+
dataset.add("iut", iut)
62+
dataset.add("steps", steps)
63+
for step, definition in preparation_steps.items():
64+
definition = OrderedDict(**definition)
65+
self.logger.info("Executing step %r", step)
66+
step_result = jsontas.run(json_data=definition)
67+
self.logger.info("%r", step_result)
68+
if not step_result:
69+
self.logger.error("Failed to execute step %r", step)
70+
return False, iut
71+
steps[step] = step_result
72+
except Exception as exception: # pylint:disable=broad-except
73+
self.logger.error("Failure when preparing IUT %r", iut)
74+
self.logger.error("%r", exception)
75+
return False, iut
6676
return True, iut
6777

6878
def prepare(self, iuts):
@@ -73,27 +83,31 @@ def prepare(self, iuts):
7383
:return: Prepared IUTs.
7484
:rtype: list
7585
"""
76-
if not self.prepare_ruleset:
77-
self.logger.info("No defined preparation rule.")
78-
return iuts
79-
thread_pool = ThreadPool()
86+
try:
87+
if not self.prepare_ruleset:
88+
self.logger.info("No defined preparation rule.")
89+
return iuts
90+
thread_pool = ThreadPool()
8091

81-
stages = self.prepare_ruleset.get("stages", {})
82-
steps = stages.get("environment_provider", {}).get("steps", {})
83-
results = []
84-
for iut in reversed(iuts):
85-
self.logger.info("Preparing IUT %r", iut)
86-
results.append(
87-
thread_pool.apply_async(
88-
self.execute_preparation_steps, args=(iut, deepcopy(steps))
92+
stages = self.prepare_ruleset.get("stages", {})
93+
steps = stages.get("environment_provider", {}).get("steps", {})
94+
results = []
95+
for iut in reversed(iuts):
96+
self.logger.info("Preparing IUT %r", iut)
97+
results.append(
98+
thread_pool.apply_async(
99+
self.execute_preparation_steps, args=(iut, deepcopy(steps))
100+
)
89101
)
90-
)
91-
for result in results:
92-
success, iut = result.get()
93-
if not success:
94-
self.logger.error("Unable to prepare %r.", iut)
95-
iuts.remove(iut)
96-
else:
97-
iut.update(**deepcopy(stages))
98-
self.dataset.add("iuts", deepcopy(iuts))
99-
return iuts
102+
for result in results:
103+
success, iut = result.get()
104+
if not success:
105+
self.logger.error("Unable to prepare %r.", iut)
106+
iuts.remove(iut)
107+
else:
108+
iut.update(**deepcopy(stages))
109+
self.dataset.add("iuts", deepcopy(iuts))
110+
return iuts
111+
finally:
112+
# Re-add the config that was popped in __init__.
113+
self.dataset.add("config", self.config)

0 commit comments

Comments
 (0)