[Python-checkins] python/dist/src/Lib shutil.py,1.23,1.24

loewis@users.sourceforge.net loewis@users.sourceforge.net
Mon, 07 Oct 2002 06:23:26 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv21395/Lib

Modified Files:
	shutil.py 
Log Message:
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.


Index: shutil.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/shutil.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** shutil.py	8 Sep 2002 20:43:59 -0000	1.23
--- shutil.py	7 Oct 2002 13:23:24 -0000	1.24
***************
*** 8,14 ****
  import sys
  import stat
  
  __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
!            "copytree","rmtree"]
  
  def copyfileobj(fsrc, fdst, length=16*1024):
--- 8,18 ----
  import sys
  import stat
+ import exceptions
  
  __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
!            "copytree","rmtree","Error"]
! 
! class Error(exceptions.EnvironmentError):
!     pass
  
  def copyfileobj(fsrc, fdst, length=16*1024):
***************
*** 96,99 ****
--- 100,104 ----
      names = os.listdir(src)
      os.mkdir(dst)
+     errors = []
      for name in names:
          srcname = os.path.join(src, name)
***************
*** 109,113 ****
              # XXX What about devices, sockets etc.?
          except (IOError, os.error), why:
!             print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
  
  def rmtree(path, ignore_errors=0, onerror=None):
--- 114,120 ----
              # XXX What about devices, sockets etc.?
          except (IOError, os.error), why:
!             errors.append((srcname, dstname, why))
!     if errors:
!         raise Error, errors
  
  def rmtree(path, ignore_errors=0, onerror=None):
***************
*** 142,143 ****
--- 149,171 ----
              cmdtuples.append((os.remove, real_f))
      cmdtuples.append((os.rmdir, path))
+ 
+ 
+ def move(src, dst):
+     """Recursively move a file or directory to another location.
+ 
+     If the destination is on our current filesystem, then simply use
+     rename.  Otherwise, copy src to the dst and then remove src.
+     A lot more could be done here...  A look at a mv.c shows a lot of
+     the issues this implementation glosses over.
+ 
+     """
+ 
+     try:
+         os.rename(src, dst)
+     except OSError:
+         if os.path.isdir(src):
+             copytree(src, dst, symlinks=1)
+             rmtree(src)
+         else:
+             copy2(src,dst)
+             os.unlink(src)