+class ImageDownloadJob(Job):
+ def __init__(self, url, filename, device=None, mountpoint="/"):
+ Job.__init__(self, _("Download .NFI-Files for USB-Flasher"))
+ if device:
+ if isMount(mountpoint):
+ UmountTask(self, mountpoint)
+ MountTask(self, device, mountpoint)
+ ImageDownloadTask(self, url, mountpoint+filename)
+ ImageDownloadTask(self, url[:-4]+".nfo", mountpoint+filename[:-4]+".nfo")
+ if device:
+ UmountTask(self, mountpoint)
+
+ def retry(self):
+ self.tasks[0].args += self.tasks[0].retryargs
+ Job.retry(self)
+
+class MountTask(Task):
+ def __init__(self, job, device, mountpoint):
+ Task.__init__(self, job, ("mount"))
+ self.setTool("mount")
+ options = "rw,sync"
+ self.mountpoint = mountpoint
+ self.args += [ device, mountpoint, "-o"+options ]
+ self.weighting = 1
+
+ def processOutput(self, data):
+ print "[MountTask] output:", data
+
+class UmountTask(Task):
+ def __init__(self, job, mountpoint):
+ Task.__init__(self, job, ("mount"))
+ self.setTool("umount")
+ self.args += [mountpoint]
+ self.weighting = 1
+
+class DownloaderPostcondition(Condition):
+ def check(self, task):
+ return task.returncode == 0
+
+ def getErrorMessage(self, task):
+ return self.error_message
+
+class ImageDownloadTask(Task):
+ def __init__(self, job, url, path):
+ Task.__init__(self, job, _("Downloading"))
+ self.postconditions.append(DownloaderPostcondition())
+ self.job = job
+ self.url = url
+ self.path = path
+ self.error_message = ""
+ self.last_recvbytes = 0
+ self.error_message = None
+
+ def run(self, callback):
+ self.callback = callback
+ self.download = downloadWithProgress(self.url,self.path)
+ self.download.addProgress(self.download_progress)
+ self.download.start().addCallback(self.download_finished).addErrback(self.download_failed)
+ print "[ImageDownloadTask] downloading", self.url, "to", self.path
+
+ def download_progress(self, recvbytes, totalbytes):
+ #print "[update_progress] recvbytes=%d, totalbytes=%d" % (recvbytes, totalbytes)
+ if ( recvbytes - self.last_recvbytes ) > 10000: # anti-flicker
+ self.progress = int(100*(float(recvbytes)/float(totalbytes)))
+ self.name = _("Downloading") + ' ' + "%d of %d kBytes" % (recvbytes/1024, totalbytes/1024)
+ self.last_recvbytes = recvbytes
+
+ def download_failed(self, failure_instance=None, error_message=""):
+ self.error_message = error_message
+ if error_message == "" and failure_instance is not None:
+ self.error_message = failure_instance.getErrorMessage()
+ print "[download_failed]", self.error_message
+ Task.processFinished(self, 1)
+
+ def download_finished(self, string=""):
+ print "[download_finished]", string
+ Task.processFinished(self, 0)
+
+class StickWizardJob(Job):
+ def __init__(self, path):
+ Job.__init__(self, _("USB stick wizard"))
+ self.path = path
+ self.device = path
+ while self.device[-1:] == "/" or self.device[-1:].isdigit():
+ self.device = self.device[:-1]
+
+ box = HardwareInfo().get_device_name()
+ url = "http://www.dreamboxupdate.com/download/opendreambox/dreambox-nfiflasher-%s.tar.bz2" % box
+ self.downloadfilename = "/tmp/dreambox-nfiflasher-%s.tar.bz2" % box
+ self.imagefilename = "/tmp/nfiflash_%s.img" % box
+ #UmountTask(self, device)
+ PartitionTask(self)
+ ImageDownloadTask(self, url, self.downloadfilename)
+ UnpackTask(self)
+ CopyTask(self)
+
+class PartitionTaskPostcondition(Condition):
+ def check(self, task):
+ return task.returncode == 0
+
+ def getErrorMessage(self, task):
+ return {
+ task.ERROR_BLKRRPART: ("Device or resource busy"),
+ task.ERROR_UNKNOWN: (task.errormsg)
+ }[task.error]
+
+class PartitionTask(Task):
+ ERROR_UNKNOWN, ERROR_BLKRRPART = range(2)
+ def __init__(self, job):
+ Task.__init__(self, job, ("partitioning"))
+ self.postconditions.append(PartitionTaskPostcondition())
+ self.job = job
+ self.setTool("sfdisk")
+ self.args += [self.job.device]
+ self.weighting = 10
+ self.initial_input = "0 - 0x6 *\n;\n;\n;\ny"
+ self.errormsg = ""
+
+ def run(self, callback):
+ Task.run(self, callback)
+
+ def processOutput(self, data):
+ print "[PartitionTask] output:", data
+ if data.startswith("BLKRRPART:"):
+ self.error = self.ERROR_BLKRRPART