diff options
Diffstat (limited to 'ick2/buildgraph.py')
-rw-r--r-- | ick2/buildgraph.py | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/ick2/buildgraph.py b/ick2/buildgraph.py index ee52e46..0fd8db1 100644 --- a/ick2/buildgraph.py +++ b/ick2/buildgraph.py @@ -17,6 +17,21 @@ import copy +ACTION_BLOCKED = 'blocked' +ACTION_READY = 'ready' +ACTION_BUILDING = 'building' +ACTION_DONE = 'done' +ACTION_FAILED = 'failed' + +action_states = [ + ACTION_BLOCKED, + ACTION_READY, + ACTION_BUILDING, + ACTION_DONE, + ACTION_FAILED, +] + + class BuildGraph: def __init__(self, graph=None): @@ -37,26 +52,26 @@ class BuildGraph: return self.actions[action_id]['status'] def set_action_status(self, action_id, status): + assert status in action_states self.actions[action_id]['status'] = status self.trigger_observer() def has_more_to_do(self): return ( - self.find_actions('ready') or - self.find_actions('building') or - self.find_actions('blocked') + self.find_actions(ACTION_READY) or + self.find_actions(ACTION_BUILDING) ) def unblock(self): - blocked_ids = self.find_actions('blocked') + blocked_ids = self.find_actions(ACTION_BLOCKED) for blocked_id in blocked_ids: blocked = self.actions[blocked_id] if self.is_unblockable(blocked): - self.set_action_status(blocked_id, 'ready') + self.set_action_status(blocked_id, ACTION_READY) def is_unblockable(self, action): - return all( - self.get_action_status(dep) == 'done' + return action['status'] == ACTION_BLOCKED and all( + self.get_action_status(dep) == ACTION_DONE for dep in action['depends'] ) @@ -64,7 +79,7 @@ class BuildGraph: if self.observer is not None: self.observer() - def append_action(self, action): + def append_action(self, action, status=None, depends=None): prev_id, action_id = self.idgen.next_id() graph_node = { @@ -72,11 +87,11 @@ class BuildGraph: } if not self.actions: - graph_node['status'] = 'ready' - graph_node['depends'] = [] + graph_node['status'] = ACTION_READY if status is None else status + graph_node['depends'] = [] if depends is None else depends else: - graph_node['status'] = 'blocked' - graph_node['depends'] = [prev_id] + graph_node['status'] = ACTION_BLOCKED if status is None else status + graph_node['depends'] = [prev_id] if depends is None else depends self.actions[action_id] = graph_node self.trigger_observer() |