6 this file is part of the project scolasync
8 Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 import subprocess, threading, re, os, os.path, shutil, time, glob, shlex
43 return "ThreadRegister: %s" %self.
dico
51 def push(self, ud, thread):
52 if ud.owner
not in self.dico.keys():
53 self.
dico[ud.owner]=[thread]
55 self.
dico[ud.owner].append(thread)
63 def pop(self, ud, thread):
64 self.
dico[ud.owner].remove(thread)
73 if owner
in self.dico.keys():
74 return self.
dico[owner]
83 for o
in self.dico.keys():
84 for t
in self.
dico[o]:
95 def _sanitizePath(path):
96 pattern=re.compile(
".*([^/]+)")
97 m=pattern.match(str(path))
101 return str(path).replace(
'/',
'_')
112 name=
"th_%04d_%s" %(_threadNumber,_sanitizePath(ud.path))
122 return time.strftime(
"%Y/%m/%d-%H:%M:%S")
143 def __init__(self,ud, fileList, subdir, dest=None, logfile="/dev/null",
145 threading.Thread.__init__(self,target=self.
toDo,
146 args=(ud, fileList, subdir, dest, logfile),
147 name=_threadName(ud))
148 self.
cmd=
u"echo This is an abstract method, don't call it"
150 ud.threadRunning=
True
163 open(os.path.expanduser(self.
logfile),
"a").write(msg+
"\n")
179 def copytree(self,src, dst, symlinks=False, ignore=None, erase=False, errors=[]):
180 names = os.listdir(src)
181 if ignore
is not None:
182 ignored_names = ignore(src, names)
184 ignored_names = set()
191 if name
in ignored_names:
193 srcname = os.path.join(src, name)
194 dstname = os.path.join(dst, name)
196 if symlinks
and os.path.islink(srcname):
197 linkto = os.readlink(srcname)
198 os.symlink(linkto, dstname)
199 if not errors
and erase:
201 elif os.path.isdir(srcname):
202 errors=self.
copytree(srcname, dstname,
203 symlinks=symlinks, ignore=ignore,
204 erase=erase, errors=errors)
205 if not errors
and erase:
208 shutil.copy2(srcname, dstname)
209 if not errors
and erase:
212 except (IOError, os.error), why:
213 errors.append((srcname, dstname, str(why)))
216 except Exception, err:
217 errors.extend(err.args[0])
228 result+=
" ud = %s\n" %self.
ud
229 result+=
" fileList = %s\n" %self.
fileList
230 result+=
" subdir = %s\n" %self.
subdir
231 result+=
" dest = %s\n" %self.
dest
232 result+=
" logfile = %s\n" %self.
logfile
233 result+=
" cmd = %s\n" %self.
cmd
242 return "abstractThreadUSB"
253 def toDo(self, ud, fileList, subdir, dest, logfile):
261 class threadCopyToUSB(abstractThreadUSB):
273 def __init__(self,ud, fileList, subdir, logfile="/dev/null",
275 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=
None, logfile=logfile, parent=parent)
276 self.
cmd=
u'mkdir -p "{toDir}"; cp -R {fromFile} "{toDir}"'
283 return "threadCopyToUSB"
297 def toDo(self, ud, fileList, subdir, dest, logfile):
298 while subdir[0]==
'/':
300 destpath=os.path.join(ud.ensureMounted(),ud.visibleDir(),subdir)
302 cmd=
"copying %s to %s" %(f, destpath)
304 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
305 destpath1=os.path.join(destpath, os.path.basename(f))
311 shutil.copy2(f, destpath1)
312 except Exceptio, err:
313 errors.extend((f, destpath1, str(err)))
324 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
345 def __init__(self,ud, fileList, subdir=".", dest="/tmp",
346 rootPath=
"/", logfile=
"/dev/null", parent=
None):
347 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=dest,
348 logfile=logfile, parent=parent)
350 self.
cmd=
u'mkdir -p "{toPath}"; cp -R {fromPath} "{toPath}"'
363 def toDo(self, ud, fileList, subdir, dest, logfile):
367 fromPath=os.path.join(ud.ensureMounted(), f)
370 newName=
u"%s_%s" %(owner,os.path.dirname(f))
372 toPath=os.path.join(dest,newName)
373 cmd=
"copying %s to %s" %(fromPath, toPath)
375 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
376 destpath1=os.path.join(toPath, os.path.basename(f))
377 if os.path.isdir(fromPath):
378 errors=self.
copytree(fromPath, destpath1)
382 shutil.copy2(fromPath, destpath1)
383 except Exception, err:
384 errors.extend((fromPath, destpath1, str(err)))
395 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
416 def __init__(self,ud, fileList, subdir=".", dest="/tmp",
417 rootPath=
"/", logfile=
"/dev/null", parent=
None):
418 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=dest,
419 logfile=logfile, parent=parent)
421 self.
cmd=
u'mkdir -p "{toPath}"; cp -R {fromPath} "{toPath}" && rm -rf {fromPath}'
435 def toDo(self, ud, fileList, subdir, dest, logfile):
438 fromPath=os.path.join(ud.ensureMounted(), f)
441 newName=
u"%s_%s" %(owner,os.path.dirname(f))
443 toPath=os.path.join(dest,newName)
444 cmd=
"copying %s to %s" %(fromPath, toPath)
446 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
447 destpath1=os.path.join(toPath, os.path.basename(f))
448 if os.path.isdir(fromPath):
449 errors=self.
copytree(fromPath, destpath1, erase=
True)
452 except Exception, err:
453 errors.extend((fromPath, destpath1, str(err)))
457 shutil.copy2(fromPath, destpath1)
459 except Exception, err:
460 errors.extend((fromPath, destpath1, str(err)))
471 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
490 def __init__(self,ud, fileList, subdir, logfile="/dev/null",
492 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=
None,
493 logfile=logfile, parent=parent)
494 self.
cmd=
u'rm -rf {toDel}'
509 def toDo(self, ud, fileList, subdir, dest, logfile):
511 toDel=os.path.join(ud.ensureMounted(), f)
512 cmd=
"Deleting %s" %toDel
515 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
516 if os.path.isdir(toDel):
518 for root, dirs, files
in os.walk(toDel, topdown=
False):
520 os.remove(os.path.join(root, name))
522 os.rmdir(os.path.join(root, name))
524 except Exception, err:
525 errors.expand((toDel,str(err)))
529 except Exception, err:
530 errors.expand((toDel,str(err)))
540 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)