don't crash when python-imaging not installed
[enigma2.git] / lib / python / Plugins / Extensions / DVDBurn / Process.py
index ec674ef21ec1639886f7bd3203bfe792038f9c65..b60cca2053929e8315074a28a768a535417a911e 100644 (file)
@@ -1,4 +1,4 @@
-from Components.Task import Task, Job, job_manager, DiskspacePrecondition, Condition
+from Components.Task import Task, Job, job_manager, DiskspacePrecondition, Condition, ToolExistsPrecondition
 from Screens.MessageBox import MessageBox
 
 class png2yuvTask(Task):
@@ -11,13 +11,11 @@ class png2yuvTask(Task):
 
        def run(self, callback, task_progress_changed):
                Task.run(self, callback, task_progress_changed)
+               self.container.stdoutAvail.get().remove(self.processStdout)
                self.container.dumpToFile(self.dumpFile)
 
        def processStderr(self, data):
-               print "[png2yuvTask]", data
-
-       def processStdout(self, data):
-               pass
+               print "[png2yuvTask]", data[:-1]
 
 class mpeg2encTask(Task):
        def __init__(self, job, inputfile, outputfile):
@@ -45,15 +43,13 @@ class spumuxTask(Task):
 
        def run(self, callback, task_progress_changed):
                Task.run(self, callback, task_progress_changed)
+               self.container.stdoutAvail.get().remove(self.processStdout)
                self.container.dumpToFile(self.dumpFile)
                self.container.readFromFile(self.inputFile)
 
        def processStderr(self, data):
                print "[spumuxTask]", data[:-1]
 
-       def processStdout(self, data):
-               pass
-
 class MakeFifoNode(Task):
        def __init__(self, job, number):
                Task.__init__(self, job, "Make FIFO nodes")
@@ -69,6 +65,19 @@ class LinkTS(Task):
                self.args += ["-s", sourcefile, link_name]
                self.weighting = 10
 
+class CopyMeta(Task):
+       def __init__(self, job, sourcefile):
+               Task.__init__(self, job, "Copy title meta files")
+               self.setTool("/bin/cp")
+               from os import listdir
+               path, filename = sourcefile.rstrip("/").rsplit("/",1)
+               tsfiles = listdir(path)
+               for file in tsfiles:
+                       if file.startswith(filename+"."):
+                               self.args += [path+'/'+file]
+               self.args += [self.job.workspace]
+               self.weighting = 10
+
 class DemuxTask(Task):
        def __init__(self, job, inputfile):
                Task.__init__(self, job, "Demux video into ES")
@@ -146,6 +155,8 @@ class DemuxTask(Task):
 
 class MplexTaskPostcondition(Condition):
        def check(self, task):
+               if task.error == task.ERROR_UNDERRUN:
+                       return True
                return task.error is None
 
        def getErrorMessage(self, task):
@@ -167,6 +178,12 @@ class MplexTask(Task):
                if inputfiles:
                        self.args += inputfiles
 
+       def setTool(self, tool):
+               self.cmd = tool
+               self.args = [tool]
+               self.global_preconditions.append(ToolExistsPrecondition())
+               # we don't want the ReturncodePostcondition in this case because for right now we're just gonna ignore the fact that mplex fails with a buffer underrun error on some streams (this always at the very end)
+
        def prepare(self):
                self.error = None                       
                if self.demux_task:
@@ -177,7 +194,6 @@ class MplexTask(Task):
                if line.startswith("**ERROR:"):
                        if line.find("Frame data under-runs detected") != -1:
                                self.error = self.ERROR_UNDERRUN
-                               print "BUFFER UNDERRUN ERROR !!!"
                        else:
                                self.error = self.ERROR_UNKNOWN
 
@@ -240,7 +256,7 @@ class BurnTaskPostcondition(Condition):
 
 class BurnTask(Task):
        ERROR_MEDIA, ERROR_SIZE, ERROR_WRITE_FAILED, ERROR_DVDROM, ERROR_ISOFS, ERROR_UNKNOWN = range(6)
-       def __init__(self, job):
+       def __init__(self, job, extra_args=[]):
                Task.__init__(self, job, "burn")
 
                self.weighting = 500
@@ -248,7 +264,8 @@ class BurnTask(Task):
                self.postconditions.append(BurnTaskPostcondition())
                self.setTool("/bin/growisofs")
                volName = self.getASCIIname(job.project.settings.name.getValue())
-               self.args += ["-dvd-video", "-dvd-compat", "-Z", "/dev/cdroms/cdrom0", "-V", volName, "-P", "Dreambox", "-use-the-force-luke=dummy", self.job.workspace + "/dvd"]
+               self.args += ["-dvd-compat", "-Z", "/dev/cdroms/cdrom0", "-V", volName, "-P", "Dreambox", "-use-the-force-luke=dummy", self.job.workspace + "/dvd"]
+               self.args += extra_args
 
        def getASCIIname(self, name):
                ASCIIname = ""
@@ -383,7 +400,15 @@ def formatTitle(template, title, track):
        return template.decode("utf-8")
 
 def CreateMenus(job):
-       import os, Image, ImageDraw, ImageFont
+       try:
+               from ImageFont import truetype
+               import ImageDraw, Image, os
+       except ImportError:
+               job.project.session.open(MessageBox, ("missing python-imaging"), type = MessageBox.TYPE_ERROR)
+               if job.project.waitboxref:
+                       job.project.waitboxref.close()
+               return False
+
        imgwidth = 720
        imgheight = 576
        s = job.project.settings
@@ -394,9 +419,9 @@ def CreateMenus(job):
        fontsizes = s.font_size.getValue()
        fontface = s.font_face.getValue()
        
-       font0 = ImageFont.truetype(fontface, fontsizes[0])
-       font1 = ImageFont.truetype(fontface, fontsizes[1])
-       font2 = ImageFont.truetype(fontface, fontsizes[2])
+       font0 = truetype(fontface, fontsizes[0])
+       font1 = truetype(fontface, fontsizes[1])
+       font2 = truetype(fontface, fontsizes[2])
 
        color_headline = tuple(s.color_headline.getValue())
        color_button = tuple(s.color_button.getValue())
@@ -501,6 +526,7 @@ def CreateMenus(job):
                
                menuoutputfilename = job.workspace+"/dvdmenu"+str(menu_count)+".mpg"
                spumuxTask(job, spuxmlfilename, menubgmpgfilename, menuoutputfilename)
+       return True
                
 def CreateAuthoringXML(job):
        nr_titles = len(job.project.titles)
@@ -588,7 +614,8 @@ class DVDJob(Job):
 
        def conduct(self):
                if self.project.settings.authormode.getValue().startswith("menu") or self.menupreview:
-                       CreateMenus(self)
+                       if not CreateMenus(self):
+                               return
                CreateAuthoringXML(self)
 
                totalsize = 50*1024*1024 # require an extra safety 50 MB
@@ -616,7 +643,34 @@ class DVDJob(Job):
                                RemoveESFiles(self, demux)
                        WaitForResidentTasks(self)
                        PreviewTask(self)
-                       BurnTask(self)
+                       BurnTask(self,["-dvd-video"])
+               RemoveDVDFolder(self)
+
+class DVDdataJob(Job):
+       def __init__(self, project):
+               Job.__init__(self, "Data DVD Burn")
+               self.project = project
+               from time import strftime
+               from Tools.Directories import SCOPE_HDD, resolveFilename, createDir
+               new_workspace = resolveFilename(SCOPE_HDD) + "tmp/" + strftime("%Y%m%d%H%M%S") + "/dvd/"
+               createDir(new_workspace, True)
+               self.workspace = new_workspace
+               self.project.workspace = self.workspace
+               self.conduct()
+
+       def conduct(self):
+               diskSpaceNeeded = 50*1024*1024 # require an extra safety 50 MB
+               for title in self.project.titles:
+                       diskSpaceNeeded += title.filesize
+               nr_titles = len(self.project.titles)
+
+               for self.i in range(nr_titles):
+                       title = self.project.titles[self.i]
+                       filename = title.inputfile.rstrip("/").rsplit("/",1)[1]
+                       link_name =  self.workspace + filename
+                       LinkTS(self, title.inputfile, link_name)
+                       CopyMeta(self, title.inputfile)
+               BurnTask(self)
                RemoveDVDFolder(self)
 
 def Burn(session, project):
@@ -628,3 +682,8 @@ def PreviewMenu(session, project):
        j = DVDJob(project, menupreview=True)
        job_manager.AddJob(j)
        return j
+
+def BurnDataTS(session, project):
+       j = DVDdataJob(project)
+       job_manager.AddJob(j)
+       return j