Task improvements, run fsck in background
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / CopyFiles.py
1 import os
2 import Components.Task
3 from twisted.internet import reactor, threads, task
4
5 class FailedPostcondition(Components.Task.Condition):
6         def __init__(self, exception):
7                 self.exception = exception
8         def getErrorMessage(self, task):
9                 return str(self.exception)
10         def check(self, task):
11                 return self.exception is None
12
13 class CopyFileTask(Components.Task.PythonTask):
14         def openFiles(self, fileList):
15                 self.callback = None
16                 self.fileList = fileList
17                 self.handles = [(open(fn[0], 'rb'), open(fn[1], 'wb')) for fn in fileList]
18                 self.end = 0
19                 for src,dst in fileList:
20                         try:
21                                 self.end += os.stat(src).st_size
22                         except:
23                                 print "Failed to stat", src
24                 if not self.end:
25                         self.end = 1
26                 print "[CopyFileTask] size:", self.end
27         def work(self):
28                 print "[CopyFileTask] handles ", len(self.handles)
29                 try:
30                         for src, dst in self.handles:
31                                 while 1:
32                                         if self.aborted:
33                                                 print "[CopyFileTask] aborting"
34                                                 raise Exception, "Aborted"
35                                         d = src.read(65536)
36                                         if not d:
37                                                 src.close()
38                                                 dst.close()
39                                                 # EOF
40                                                 break
41                                         dst.write(d)
42                                         self.pos += len(d)
43                 except:
44                         # In any event, close all handles
45                         for src, dst in self.handles:
46                                 src.close()
47                                 dst.close()
48                         for s,d in self.fileList:
49                                 # Remove incomplete data.
50                                 try:
51                                         os.unlink(d)
52                                 except:
53                                         pass
54                         raise
55
56
57 def copyFiles(fileList, name):
58         name = _("Copy") + " " + name
59         job = Components.Task.Job(name)
60         task = CopyFileTask(job, name)
61         task.openFiles(fileList)
62         Components.Task.job_manager.AddJob(job)
63