clamp progress to 0..end, add possibility to receive whole lines only in processOutpu...
[enigma2.git] / lib / python / Components / Task.py
index aebca7324903e6b6cf2b96854367eb32c42c345c..c59086e132cfbc4503044f956e7d78fbff1a1fc4 100644 (file)
@@ -97,6 +97,7 @@ class Task(object)    :
                self.cwd = "/tmp"
                self.args = [ ]
                self.task_progress_changed = None
+               self.output_line = ""
                job.addTask(self)
 
        def setCommandline(self, cmd, args):
@@ -151,6 +152,15 @@ class Task(object) :
                pass
 
        def processOutput(self, data):
+               self.output_line += data
+               while True:
+                       i = self.output_line.find('\n')
+                       if i == -1:
+                               break
+                       self.processOutputLine(self.output_line[:i+1])
+                       self.output_line = self.output_line[i+1:]
+
+       def processOutputLine(self, line):
                pass
 
        def processFinished(self, returncode):
@@ -171,6 +181,7 @@ class Task(object)  :
                                if not postcondition.check(self):
                                        not_met.append(postcondition)
 
+               self.cleanup(not_met)
                self.callback(not_met)
 
        def afterRun(self):
@@ -183,6 +194,10 @@ class Task(object) :
                return self.__progress
 
        def setProgress(self, progress):
+               if progress > self.end:
+                       progress = self.end
+               if progress < 0:
+                       progress = 0
                print "progress now", progress
                self.__progress = progress
                self.task_progress_changed()
@@ -248,14 +263,22 @@ class JobManager:
 #              if filesystem is not None:
 #                      self.args += ["-t", filesystem]
 #              self.args.append(device + "part%d" % partition)
-#
-#class DiskspacePrecondition:
-#      def __init__(self, diskspace_required):
-#              self.diskspace_required = diskspace_required
-#
-#      def check(self, task):
-#              return getFreeDiskspace(task.workspace) >= self.diskspace_required
-#
+
+class WorkspaceExistsPrecondition:
+       def check(self, task):
+               return os.access(task.job.workspace, os.W_OK)
+
+class DiskspacePrecondition:
+       def __init__(self, diskspace_required):
+               self.diskspace_required = diskspace_required
+
+       def check(self, task):
+               import os
+               try:
+                       s = os.statvfs(task.job.workspace)
+                       return s.f_bsize * s.f_bavail >= self.diskspace_required
+               except OSError:
+                       return False
 
 class ToolExistsPrecondition:
        def check(self, task):