2022-04-08 19:24:43 +00:00
|
|
|
""" Scheduler for action to make """
|
|
|
|
|
|
|
|
|
2022-04-09 04:58:44 +00:00
|
|
|
from bopytex.tasks import Task
|
2022-04-09 04:56:57 +00:00
|
|
|
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
class Scheduler:
|
2022-04-09 19:46:24 +00:00
|
|
|
"""Scheduler is responsible of getting tasks (the tasks) and yield those that can be done"""
|
|
|
|
|
|
|
|
def __init__(self, done: list[str] = None):
|
2022-04-09 04:56:57 +00:00
|
|
|
|
|
|
|
if done is None:
|
|
|
|
self._done = []
|
|
|
|
else:
|
|
|
|
self._done = done
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
self._tasks = []
|
|
|
|
|
|
|
|
@property
|
2022-04-09 19:23:28 +00:00
|
|
|
def tasks(self) -> list[Task]:
|
2022-04-09 19:46:24 +00:00
|
|
|
"""List all the tasks todo"""
|
2022-04-08 19:24:43 +00:00
|
|
|
return self._tasks
|
|
|
|
|
2022-04-09 15:08:05 +00:00
|
|
|
@property
|
2022-04-09 19:23:28 +00:00
|
|
|
def doable_tasks(self) -> list[Task]:
|
2022-04-09 19:46:24 +00:00
|
|
|
"""List all doable tasks"""
|
2022-04-09 15:08:05 +00:00
|
|
|
return [
|
|
|
|
task
|
|
|
|
for task in self.tasks
|
|
|
|
if not task.deps or all([d in self.done for d in task.deps])
|
|
|
|
]
|
|
|
|
|
2022-04-09 04:56:57 +00:00
|
|
|
@property
|
2022-04-09 19:23:28 +00:00
|
|
|
def all_deps(self) -> list[str]:
|
2022-04-09 19:46:24 +00:00
|
|
|
"""List dependencies of all tasks"""
|
2022-04-09 04:56:57 +00:00
|
|
|
return {d for task in self.tasks for d in task.deps}
|
|
|
|
|
|
|
|
@property
|
2022-04-09 19:23:28 +00:00
|
|
|
def all_output(self) -> list[str]:
|
2022-04-09 19:46:24 +00:00
|
|
|
"""List ouput of all tasks"""
|
2022-04-09 04:56:57 +00:00
|
|
|
return {task.output for task in self.tasks}
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
@property
|
2022-04-09 19:23:28 +00:00
|
|
|
def done(self) -> list[str]:
|
2022-04-08 19:24:43 +00:00
|
|
|
return self._done
|
|
|
|
|
2022-04-09 04:56:57 +00:00
|
|
|
def append(self, tasks: list[Task]):
|
2022-04-08 19:24:43 +00:00
|
|
|
self._tasks += tasks
|
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
def is_finishable(self):
|
|
|
|
return self.all_deps.issubset(self.all_output)
|
2022-04-08 20:04:47 +00:00
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
def next_task(self):
|
2022-04-08 20:04:47 +00:00
|
|
|
try:
|
2022-04-09 19:23:28 +00:00
|
|
|
task = self.doable_tasks[0]
|
2022-04-08 20:04:47 +00:00
|
|
|
except IndexError:
|
|
|
|
raise StopIteration
|
|
|
|
|
2022-04-09 04:32:56 +00:00
|
|
|
self._done.append(task.output)
|
2022-04-09 19:23:28 +00:00
|
|
|
self._tasks.remove(task)
|
2022-04-09 19:46:24 +00:00
|
|
|
return task
|
2022-04-09 04:56:57 +00:00
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
def backlog(self):
|
|
|
|
""" Yield tasks sorted according to dependencies """
|
|
|
|
while self.doable_tasks:
|
|
|
|
yield self.next_task()
|