6 this file is part of the project scolasync
8 Copyright (C) 2010-2012 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/>.
26 import subprocess, threading, re, os, os.path, shutil, time, glob, shlex
37 os.path.isdir(destpath)
or os.makedirs(destpath, mode=0o755)
54 return "ThreadRegister: %s" %self.
dico
62 def push(self, ud, thread):
63 if ud.owner
not in self.dico.keys():
64 self.
dico[ud.owner]=[thread]
66 self.
dico[ud.owner].append(thread)
74 def pop(self, ud, thread):
75 self.
dico[ud.owner].remove(thread)
84 if owner
in self.dico.keys():
85 return self.
dico[owner]
94 for o
in self.dico.keys():
95 for t
in self.
dico[o]:
106 def _sanitizePath(path):
107 pattern=re.compile(
".*([^/]+)")
108 m=pattern.match(str(path))
112 return str(path).replace(
'/',
'_')
123 name=
"th_%04d_%s" %(_threadNumber,_sanitizePath(ud.path))
133 return time.strftime(
"%Y/%m/%d-%H:%M:%S")
154 def __init__(self,ud, fileList, subdir, dest=None, logfile="/dev/null",
156 threading.Thread.__init__(self,target=self.
toDo,
157 args=(ud, fileList, subdir, dest, logfile),
158 name=_threadName(ud))
159 self.
cmd=
"echo This is an abstract method, don't call it"
161 ud.threadRunning=
True
174 open(os.path.expanduser(self.
logfile),
"a").write(msg+
"\n")
190 def copytree(self,src, dst, symlinks=False, ignore=None, erase=False, errors=[]):
191 names = os.listdir(src)
192 if ignore
is not None:
193 ignored_names = ignore(src, names)
195 ignored_names = set()
199 except OSError
as err:
202 if name
in ignored_names:
204 srcname = os.path.join(src, name)
205 dstname = os.path.join(dst, name)
207 if symlinks
and os.path.islink(srcname):
208 linkto = os.readlink(srcname)
209 os.symlink(linkto, dstname)
210 if not errors
and erase:
212 elif os.path.isdir(srcname):
213 errors=self.
copytree(srcname, dstname,
214 symlinks=symlinks, ignore=ignore,
215 erase=erase, errors=errors)
216 if not errors
and erase:
219 shutil.copy2(srcname, dstname)
220 if not errors
and erase:
223 except IOError
as why:
224 errors.append((srcname, dstname, str(why)))
227 except os.error
as why:
228 errors.append((srcname, dstname, str(why)))
231 except Exception
as err:
232 errors.extend(err.args[0])
243 result+=
" ud = %s\n" %self.
ud
244 result+=
" fileList = %s\n" %self.
fileList
245 result+=
" subdir = %s\n" %self.
subdir
246 result+=
" dest = %s\n" %self.
dest
247 result+=
" logfile = %s\n" %self.
logfile
248 result+=
" cmd = %s\n" %self.
cmd
257 return "abstractThreadUSB"
268 def toDo(self, ud, fileList, subdir, dest, logfile):
276 class threadCopyToUSB(abstractThreadUSB):
288 def __init__(self,ud, fileList, subdir, logfile="/dev/null",
290 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=
None, logfile=logfile, parent=parent)
291 self.
cmd=
'mkdir -p "{toDir}"; cp -R {fromFile} "{toDir}"'
298 return "threadCopyToUSB"
312 def toDo(self, ud, fileList, subdir, dest, logfile):
313 while subdir[0]==
'/':
315 destpath=os.path.join(ud.ensureMounted(),ud.visibleDir(),subdir)
319 cmd=
"copying %s to %s" %(f, destpath)
321 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
322 destpath1=os.path.join(destpath, os.path.basename(f))
329 shutil.copy2(f, destpath1)
330 except Exception
as err:
331 errors.extend((f, destpath1, str(err)))
342 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
363 def __init__(self,ud, fileList, subdir=".", dest="/tmp",
364 rootPath=
"/", logfile=
"/dev/null", parent=
None):
365 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=dest,
366 logfile=logfile, parent=parent)
380 def toDo(self, ud, fileList, subdir, dest, logfile):
383 fromPath=os.path.join(ud.ensureMounted(), f)
386 newName=
"%s_%s" %(owner,os.path.dirname(f))
388 toPath=os.path.join(dest,newName)
391 cmd=
"copying %s to %s" %(fromPath, toPath)
393 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
394 destpath1=os.path.join(toPath, os.path.basename(f))
395 if os.path.isdir(fromPath):
396 errors=self.
copytree(fromPath, destpath1)
400 shutil.copy2(fromPath, destpath1)
401 except Exception
as err:
402 errors.extend((fromPath, destpath1, str(err)))
413 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
434 def __init__(self,ud, fileList, subdir=".", dest="/tmp",
435 rootPath=
"/", logfile=
"/dev/null", parent=
None):
436 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=dest,
437 logfile=logfile, parent=parent)
452 def toDo(self, ud, fileList, subdir, dest, logfile):
455 fromPath=os.path.join(ud.ensureMounted(), f)
458 newName=
"%s_%s" %(owner,os.path.dirname(f))
460 toPath=os.path.join(dest,newName)
463 cmd=
"copying %s to %s" %(fromPath, toPath)
465 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
466 destpath1=os.path.join(toPath, os.path.basename(f))
467 if os.path.isdir(fromPath):
468 errors=self.
copytree(fromPath, destpath1, erase=
True)
471 except Exception
as err:
472 errors.extend((fromPath, destpath1, str(err)))
476 shutil.copy2(fromPath, destpath1)
478 except Exception
as err:
479 errors.extend((fromPath, destpath1, str(err)))
490 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
509 def __init__(self,ud, fileList, subdir, logfile="/dev/null",
511 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=
None,
512 logfile=logfile, parent=parent)
527 def toDo(self, ud, fileList, subdir, dest, logfile):
529 toDel=os.path.join(ud.ensureMounted(), f)
530 cmd=
"Deleting %s" %toDel
533 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
534 if os.path.isdir(toDel):
536 for root, dirs, files
in os.walk(toDel, topdown=
False):
538 os.remove(os.path.join(root, name))
540 os.rmdir(os.path.join(root, name))
542 except Exception
as err:
543 errors.expand((toDel,str(err)))
547 except Exception
as err:
548 errors.expand((toDel,str(err)))
558 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)