# Modified shutils.py from python 2.3.3 source import glob,os,os.path,stat,string,wx,shutil def copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf) def copyfile(src, dst): """Copy data from src to dst""" fsrc = None fdst = None # check for same pathname; all platforms _src = os.path.normcase(os.path.abspath(src)) _dst = os.path.normcase(os.path.abspath(dst)) if _src == _dst: return try: fsrc = open(src, 'rb') fdst = open(dst, 'wb') copyfileobj(fsrc, fdst) finally: if fdst: fdst.close() if fsrc: fsrc.close() def copystat(src, dst): """Copy all stat info (mode bits, atime and mtime) from src to dst""" st = os.stat(src) mode = stat.S_IMODE(st.st_mode) if hasattr(os, 'utime'): os.utime(dst, (st.st_atime, st.st_mtime)) if hasattr(os, 'chmod'): os.chmod(dst, mode) def copy2(src, dst): # will sometime warn about stat writing error if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) try: copyfile(src, dst) except: return 1 try: copystat(src, dst) except: return 2 return 0 def StripList(List): while '' in List: List.remove('') return List def renf(src,dst): try: if not os.path.dirname(src)==os.path.dirname(dst): try: if not os.path.exists(os.path.dirname(dst)): os.makedirs(os.path.dirname(dst)) except: raise IOError,'Unable to create needed directories for file %s'%dst os.rename(src,dst) except OSError,e: if string.find(str(e),'Errno 18')!=-1: if copy2(src,dst)==1: raise IOError,'Unable to copy' try: os.remove(src) except: raise IOError,'Unable to remove' else: raise IOError,e def listfiles(junk,dirname,content): allobj.append(map(lambda part: os.path.join(dirname,part),content)) alldirs.append(dirname) def rendir(src,dst): if os.path.exists(dst) and not os.path.isdir(dst): raise IOError,'%s exists and is not directory'%dst try: os.rename(src,dst) except OSError,e: if string.find(str(e),'Errno 18')!=-1 or string.find(str(e),'Errno 2'): try: os.path.walk(src,listfiles,None) if not os.path.exists(dst): os.makedirs(dst) while [] in allobj: allobj.remove([]) calldirs=map(lambda name:name[len(src)+1:],alldirs) while '' in calldirs: calldirs.remove('') for dname in calldirs: if not os.path.isdir(os.path.join(dst,dname)): os.mkdir(os.path.join(dst,dname)) fopdir={} check=0 while check!=len(allobj): for fname in allobj[check]: if not fname in alldirs: fopdir[fname]=os.path.join(dst,fname[len(src)+1:]) check+=1 for fname in fopdir: renf(fname,fopdir[fname]) except: raise IOError,'Can not rename directory or copy source directory to destination directory' try: # IMPORTANT: if src==dst[:len(src)] we would delete dst as well if not src==dst[:len(src)]: shutil.rmtree(src) except: raise IOError,'Files copied, but failed to delete source directory' def renobj(SrcName='',DstName='',DelDir=0,wxwindow=None): # nefungujou navratovy kody u renamedir, vraci to uplne do jinyho adresare global allobj,alldirs,g allobj=[] alldirs=[] fl=[] SrcName=os.path.abspath(SrcName) DstName=os.path.abspath(DstName) if os.path.isfile(SrcName): renf(SrcName,DstName) if os.path.isdir(SrcName): rendir(SrcName,DstName) # for compatibility with rename dir tool def CopyTree(src, dst): names = os.listdir(src) for name in names: srcname = os.path.join(src, name) dstname = os.path.join(dst, name) if os.path.isdir(srcname): CopyTree(srcname, dstname) else: if copy2(srcname, dstname)==1: return 1 return 0