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/>.
24 import subprocess, threading, re, os, os.path, shutil
25 import time, glob, shlex, io
36 os.path.isdir(destpath)
or os.makedirs(destpath, mode=0o755)
53 return "ThreadRegister: %s" %self.
dico
61 def push(self, ud, thread):
62 if ud.owner
not in self.dico.keys():
63 self.
dico[ud.owner]=[thread]
65 self.
dico[ud.owner].append(thread)
73 def pop(self, ud, thread):
74 self.
dico[ud.owner].remove(thread)
83 if owner
in self.dico.keys():
84 return self.
dico[owner]
93 for o
in self.dico.keys():
94 for t
in self.
dico[o]:
105 def _sanitizePath(path):
106 pattern=re.compile(
".*([^/]+)")
107 m=pattern.match(str(path))
111 return str(path).replace(
'/',
'_')
122 if hasattr(ud,
"path"):
123 name=
"th_%04d_%s" %(_threadNumber,_sanitizePath(ud.path))
125 name=
"th_%04d_%s" %(_threadNumber,
"dummy")
135 return time.strftime(
"%Y/%m/%d-%H:%M:%S")
163 def __init__(self,ud, fileList, subdir, dest=None, logfile="/dev/null",
165 threading.Thread.__init__(self, name=_threadName(ud))
166 self.
_args=(ud, fileList, subdir, dest, logfile)
168 if hasattr(ud,
"threadRunning"): ud.threadRunning=
True
185 open(os.path.expanduser(self.
logfile),
"a").write(msg+
"\n")
201 def copytree(self,src, dst, symlinks=False, ignore=None, erase=False, errors=[]):
202 names = os.listdir(src)
203 if ignore
is not None:
204 ignored_names = ignore(src, names)
206 ignored_names = set()
210 except OSError
as err:
213 if name
in ignored_names:
215 srcname = os.path.join(src, name)
216 dstname = os.path.join(dst, name)
218 if symlinks
and os.path.islink(srcname):
219 linkto = os.readlink(srcname)
220 os.symlink(linkto, dstname)
221 if not errors
and erase:
223 elif os.path.isdir(srcname):
224 errors=self.
copytree(srcname, dstname,
225 symlinks=symlinks, ignore=ignore,
226 erase=erase, errors=errors)
227 if not errors
and erase:
230 shutil.copy2(srcname, dstname)
231 if not errors
and erase:
234 except IOError
as why:
235 errors.append((srcname, dstname, str(why)))
238 except os.error
as why:
239 errors.append((srcname, dstname, str(why)))
242 except Exception
as err:
243 errors.extend(err.args[0])
254 result+=
" ud = %s\n" %self.
ud
255 result+=
" fileList = %s\n" %self.
fileList
256 result+=
" subdir = %s\n" %self.
subdir
257 result+=
" dest = %s\n" %self.
dest
258 result+=
" logfile = %s\n" %self.
logfile
268 return "abstractThreadUSB"
279 def toDo(self, ud, fileList, subdir, dest, logfile):
287 class threadCopyToUSB(abstractThreadUSB):
299 def __init__(self,ud, fileList, subdir, logfile="/dev/null",
301 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=
None, logfile=logfile, parent=parent)
308 return "threadCopyToUSB"
322 def toDo(self, ud, fileList, subdir, dest, logfile):
323 while subdir[0]==
'/':
325 destpath=os.path.join(ud.ensureMounted(),ud.visibleDir(),subdir)
329 cmd=
"copying %s to %s" %(f, destpath)
331 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
332 destpath1=os.path.join(destpath, os.path.basename(f))
339 shutil.copy2(f, destpath1)
340 except Exception
as err:
341 errors.append([f, destpath1, str(err)])
343 print (
"GRRRR il faut lire le fichier TODO")
351 msg+=
" <%s>" %str(e)
353 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, cmd)
374 def __init__(self,ud, fileList, subdir=".", dest="/tmp",
375 rootPath=
"/", logfile=
"/dev/null", parent=
None):
376 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=dest,
377 logfile=logfile, parent=parent)
391 def toDo(self, ud, fileList, subdir, dest, logfile):
394 fromPath=os.path.join(ud.ensureMounted(), f)
397 newName=
"%s_%s" %(owner,os.path.dirname(f))
399 toPath=os.path.join(dest,newName)
402 cmd=
"copying %s to %s" %(fromPath, toPath)
404 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
405 destpath1=os.path.join(toPath, os.path.basename(f))
406 if os.path.isdir(fromPath):
407 errors=self.
copytree(fromPath, destpath1)
411 shutil.copy2(fromPath, destpath1)
412 except Exception
as err:
413 errors.extend((fromPath, destpath1, str(err)))
424 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
445 def __init__(self,ud, fileList, subdir=".", dest="/tmp",
446 rootPath=
"/", logfile=
"/dev/null", parent=
None):
447 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=dest,
448 logfile=logfile, parent=parent)
463 def toDo(self, ud, fileList, subdir, dest, logfile):
466 fromPath=os.path.join(ud.ensureMounted(), f)
469 newName=
"%s_%s" %(owner,os.path.dirname(f))
471 toPath=os.path.join(dest,newName)
474 cmd=
"copying %s to %s" %(fromPath, toPath)
476 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
477 destpath1=os.path.join(toPath, os.path.basename(f))
478 if os.path.isdir(fromPath):
479 errors=self.
copytree(fromPath, destpath1, erase=
True)
482 except Exception
as err:
483 errors.extend((fromPath, destpath1, str(err)))
487 shutil.copy2(fromPath, destpath1)
489 except Exception
as err:
490 errors.extend((fromPath, destpath1, str(err)))
501 self.parent.emit(SIGNAL(
"popCmd(QString, QString)"), ud.owner, msg)
520 def __init__(self,ud, fileList, subdir, logfile="/dev/null",
522 abstractThreadUSB.__init__(self,ud, fileList, subdir, dest=
None,
523 logfile=logfile, parent=parent)
538 def toDo(self, ud, fileList, subdir, dest, logfile):
540 toDel=os.path.join(ud.ensureMounted(), f)
541 cmd=
"Deleting %s" %toDel
544 self.parent.emit(SIGNAL(
"pushCmd(QString, QString)"), ud.owner, cmd)
545 if os.path.isdir(toDel):
547 for root, dirs, files
in os.walk(toDel, topdown=
False):
549 os.remove(os.path.join(root, name))
551 os.rmdir(os.path.join(root, name))
553 except Exception
as err:
554 errors.expand((toDel,str(err)))
558 except Exception
as err:
559 errors.expand((toDel,str(err)))
569 self.parent.emit(SIGNAL(
"popCmd(string, string)"), ud.owner, msg)
572 if __name__==
"__main__":
573 import sys, ownedUsbDisk, subprocess
579 if len(sys.argv) < 3:
580 print(
"Usage : %s répertoire_source répertoire_destination" %sys.argv[0])
581 print(
"Ça doit créer sous répertoire_destination la même arborescence que sous répertoire_source")
582 print(
"et ça crée répertoire_destination à la volée si nécessaire.")
584 errors=t.copytree(sys.argv[1],sys.argv[2])
585 print(
"Erreurs = %s" %errors)
586 subprocess.call (
"diff -ruN %s %s" %(sys.argv[1],sys.argv[2]), shell=
True)
587 print (
"Ne pas oublier d'effacer %s si nécessaire" %sys.argv[2])
595 if len(sys.argv) < 3:
596 print(
"Usage : %s fichier répertoire_destination" %sys.argv[0])
597 print(
"Ça doit créer sous répertoire_destination une copie du fichier")
598 print(
"et ça crée répertoire_destination à la volée si nécessaire.")
601 dstname=os.path.join(sys.argv[2],sys.argv[1])
602 shutil.copy2(srcname, dstname)
603 print (
"fin de la copie de %s vers %s, listing de %s" %(sys.argv[1],sys.argv[2],sys.argv[2]))
604 subprocess.call(
"ls %s" %sys.argv[2], shell=
True)