1 # A Job consists of many "Tasks".
2 # A task is the run of an external tool, with proper methods for failure handling
4 from Tools.CList import CList
7 NOT_STARTED, IN_PROGRESS, FINISHED, FAILED = range(4)
8 def __init__(self, name):
10 self.workspace = "/tmp"
19 self.state_changed = CList()
21 self.status = self.NOT_STARTED
23 # description is a dict
24 def fromDescription(self, description):
27 def createDescription(self):
30 def getProgress(self):
31 if self.current_task == len(self.tasks):
33 t = self.tasks[self.current_task]
34 jobprogress = t.weighting * t.progress / float(t.end) + sum([task.weighting for task in self.tasks[:self.current_task]])
35 return int(jobprogress*self.weightScale)
37 progress = property(getProgress)
39 def task_progress_changed_CB(self):
42 def addTask(self, task):
44 self.tasks.append(task)
46 def start(self, callback):
47 assert self.callback is None
48 self.callback = callback
49 self.status = self.IN_PROGRESS
52 sumTaskWeightings = sum([t.weighting for t in self.tasks])
53 self.weightScale = (self.end+1) / float(sumTaskWeightings)
56 if self.current_task == len(self.tasks):
57 self.callback(self, [])
58 self.status = self.FINISHED
61 self.tasks[self.current_task].run(self.taskCallback,self.task_progress_changed_CB)
64 def taskCallback(self, res):
66 print ">>> Error:", res
67 self.status = self.FAILED
69 self.callback(self, res)
72 self.current_task += 1
76 if self.current_task < len(self.tasks):
77 self.tasks[self.current_task].abort()
80 # some Jobs might have a better idea of how to cancel a job
84 def __init__(self, job, name):
86 self.immediate_preconditions = [ ]
87 self.global_preconditions = [ ]
88 self.postconditions = [ ]
89 self.returncode = None
90 self.initial_input = None
99 self.task_progress_changed = None
102 def setCommandline(self, cmd, args):
106 def setTool(self, tool):
109 self.global_preconditions.append(ToolExistsPrecondition())
110 self.postconditions.append(ReturncodePostcondition())
112 def checkPreconditions(self, immediate = False):
115 preconditions = self.immediate_preconditions
117 preconditions = self.global_preconditions
118 for precondition in preconditions:
119 if not precondition.check(self):
120 not_met.append(precondition)
123 def run(self, callback, task_progress_changed):
124 failed_preconditions = self.checkPreconditions(True) + self.checkPreconditions(False)
125 if len(failed_preconditions):
126 callback(failed_preconditions)
130 self.callback = callback
131 self.task_progress_changed = task_progress_changed
132 from enigma import eConsoleAppContainer
133 self.container = eConsoleAppContainer()
134 self.container.appClosed.get().append(self.processFinished)
135 self.container.dataAvail.get().append(self.processOutput)
137 assert self.cmd is not None
138 assert len(self.args) >= 1
140 if self.cwd is not None:
141 self.container.setCWD(self.cwd)
143 print "execute:", self.container.execute(self.cmd, self.args), self.cmd, self.args
144 if self.initial_input:
145 self.writeInput(self.initial_input)
150 def cleanup(self, failed):
153 def processOutput(self, data):
156 def processFinished(self, returncode):
157 self.returncode = returncode
161 self.container.kill()
162 self.finish(aborted = True)
164 def finish(self, aborted = False):
168 not_met.append(AbortedPostcondition())
170 for postcondition in self.postconditions:
171 if not postcondition.check(self):
172 not_met.append(postcondition)
174 self.callback(not_met)
179 def writeInput(self, input):
180 self.container.write(input)
182 def getProgress(self):
183 return self.__progress
185 def setProgress(self, progress):
186 print "progress now", progress
187 self.__progress = progress
188 self.task_progress_changed()
190 progress = property(getProgress, setProgress)
194 self.active_jobs = [ ]
195 self.failed_jobs = [ ]
196 self.job_classes = [ ]
197 self.active_job = None
199 def AddJob(self, job):
200 self.active_jobs.append(job)
204 if self.active_job is None:
205 if len(self.active_jobs):
206 self.active_job = self.active_jobs.pop(0)
207 self.active_job.start(self.jobDone)
209 def jobDone(self, job, problems):
210 print "job", job, "completed with", problems
212 self.failed_jobs.append(self.active_job)
214 self.active_job = None
218 #class PartitionExistsPostcondition:
219 # def __init__(self, device):
220 # self.device = device
222 # def check(self, task):
224 # return os.access(self.device + "part1", os.F_OK)
226 #class CreatePartitionTask(Task):
227 # def __init__(self, device):
228 # Task.__init__(self, _("Create Partition"))
229 # self.device = device
230 # self.setTool("/sbin/sfdisk")
231 # self.args += ["-f", self.device + "disc"]
232 # self.initial_input = "0,\n;\n;\n;\ny\n"
233 # self.postconditions.append(PartitionExistsPostcondition(self.device))
235 #class CreateFilesystemTask(Task):
236 # def __init__(self, device, partition = 1, largefile = True):
237 # Task.__init__(self, _("Create Filesystem"))
238 # self.setTool("/sbin/mkfs.ext")
240 # self.args += ["-T", "largefile"]
241 # self.args.append("-m0")
242 # self.args.append(device + "part%d" % partition)
244 #class FilesystemMountTask(Task):
245 # def __init__(self, device, partition = 1, filesystem = "ext3"):
246 # Task.__init__(self, _("Mounting Filesystem"))
247 # self.setTool("/bin/mount")
248 # if filesystem is not None:
249 # self.args += ["-t", filesystem]
250 # self.args.append(device + "part%d" % partition)
252 #class DiskspacePrecondition:
253 # def __init__(self, diskspace_required):
254 # self.diskspace_required = diskspace_required
256 # def check(self, task):
257 # return getFreeDiskspace(task.workspace) >= self.diskspace_required
260 class ToolExistsPrecondition:
261 def check(self, task):
266 realpath = self.cwd + '/' + self.cmd
267 return os.access(realpath, os.X_OK)
269 class AbortedPostcondition:
272 class ReturncodePostcondition:
273 def check(self, task):
274 return task.returncode == 0
276 #class HDDInitJob(Job):
277 # def __init__(self, device):
278 # Job.__init__(self, _("Initialize Harddisk"))
279 # self.device = device
280 # self.fromDescription(self.createDescription())
282 # def fromDescription(self, description):
283 # self.device = description["device"]
284 # self.addTask(CreatePartitionTask(self.device))
285 # self.addTask(CreateFilesystemTask(self.device))
286 # self.addTask(FilesystemMountTask(self.device))
288 # def createDescription(self):
289 # return {"device": self.device}
291 job_manager = JobManager()