-#
-#class DiskspacePrecondition:
-# def __init__(self, diskspace_required):
-# self.diskspace_required = diskspace_required
-#
-# def check(self, task):
-# return getFreeDiskspace(task.workspace) >= self.diskspace_required
-#
-#class ToolExistsPrecondition:
-# def check(self, task):
-# import os
-# return os.access(task.cmd, os.X_OK)
-#
-#class ReturncodePostcondition:
-# def check(self, task):
-# return task.returncode == 0
-#
+
+class Condition:
+ RECOVERABLE = False
+
+ def getErrorMessage(self, task):
+ return _("An unknown error occured!") + " (%s @ task %s)" % (self.__class__.__name__, task.__class__.__name__)
+
+class WorkspaceExistsPrecondition(Condition):
+ def check(self, task):
+ return os.access(task.job.workspace, os.W_OK)
+
+class DiskspacePrecondition(Condition):
+ def __init__(self, diskspace_required):
+ self.diskspace_required = diskspace_required
+ self.diskspace_available = 0
+
+ def check(self, task):
+ import os
+ try:
+ s = os.statvfs(task.job.workspace)
+ self.diskspace_available = s.f_bsize * s.f_bavail
+ return self.diskspace_available >= self.diskspace_required
+ except OSError:
+ return False
+
+ def getErrorMessage(self, task):
+ return _("Not enough diskspace. Please free up some diskspace and try again. (%d MB required, %d MB available)") % (self.diskspace_required / 1024 / 1024, self.diskspace_available / 1024 / 1024)
+
+class ToolExistsPrecondition(Condition):
+ def check(self, task):
+ import os
+
+ if task.cmd[0]=='/':
+ self.realpath = task.cmd
+ print "[Task.py][ToolExistsPrecondition] WARNING: usage of absolute paths for tasks should be avoided!"
+ return os.access(self.realpath, os.X_OK)
+ else:
+ self.realpath = task.cmd
+ path = os.environ.get('PATH', '').split(os.pathsep)
+ path.append(task.cwd + '/')
+ absolutes = filter(lambda file: os.access(file, os.X_OK), map(lambda directory, file = task.cmd: os.path.join(directory, file), path))
+ if len(absolutes) > 0:
+ self.realpath = task.cmd[0]
+ return True
+ return False
+
+ def getErrorMessage(self, task):
+ return _("A required tool (%s) was not found.") % (self.realpath)
+
+class AbortedPostcondition(Condition):
+ def getErrorMessage(self, task):
+ return "Cancelled upon user request"
+
+class ReturncodePostcondition(Condition):
+ def check(self, task):
+ return task.returncode == 0
+