home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / PythonLib / shutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-27  |  4.8 KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. """Utility functions for copying files.
  5.  
  6. XXX The functions here don't copy the data fork or other metadata on Mac.
  7.  
  8. """
  9. import os
  10. import stat
  11.  
  12. def copyfile(src, dst):
  13.     '''Copy data from src to dst'''
  14.     fsrc = None
  15.     fdst = None
  16.     
  17.     try:
  18.         fsrc = open(src, 'rb')
  19.         fdst = open(dst, 'wb')
  20.         while 1:
  21.             buf = fsrc.read(16 * 1024)
  22.             if not buf:
  23.                 break
  24.             
  25.             fdst.write(buf)
  26.     finally:
  27.         if fdst:
  28.             fdst.close()
  29.         
  30.         if fsrc:
  31.             fsrc.close()
  32.         
  33.  
  34.  
  35.  
  36. def copymode(src, dst):
  37.     '''Copy mode bits from src to dst'''
  38.     st = os.stat(src)
  39.     mode = stat.S_IMODE(st[stat.ST_MODE])
  40.     os.chmod(dst, mode)
  41.  
  42.  
  43. def copystat(src, dst):
  44.     '''Copy all stat info (mode bits, atime and mtime) from src to dst'''
  45.     st = os.stat(src)
  46.     mode = stat.S_IMODE(st[stat.ST_MODE])
  47.     os.chmod(dst, mode)
  48.     os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MODE]))
  49.  
  50.  
  51. def copy(src, dst):
  52.     '''Copy data and mode bits ("cp src dst").
  53.     
  54.     The destination may be a directory.
  55.  
  56.     '''
  57.     if os.path.isdir(dst):
  58.         dst = os.path.join(dst, os.path.basename(src))
  59.     
  60.     copyfile(src, dst)
  61.     copymode(src, dst)
  62.  
  63.  
  64. def copy2(src, dst):
  65.     '''Copy data and all stat info ("cp -p src dst").
  66.  
  67.     The destination may be a directory.
  68.  
  69.     '''
  70.     if os.path.isdir(dst):
  71.         dst = os.path.join(dst, os.path.basename(src))
  72.     
  73.     copyfile(src, dst)
  74.     copystat(src, dst)
  75.  
  76.  
  77. def copytree(src, dst, symlinks = 0):
  78.     '''Recursively copy a directory tree using copy2().
  79.  
  80.     The destination directory must not already exist.
  81.     Error are reported to standard output.
  82.  
  83.     If the optional symlinks flag is true, symbolic links in the
  84.     source tree result in symbolic links in the destination tree; if
  85.     it is false, the contents of the files pointed to by symbolic
  86.     links are copied.
  87.  
  88.     XXX Consider this example code rather than the ultimate tool.
  89.  
  90.     '''
  91.     names = os.listdir(src)
  92.     os.mkdir(dst)
  93.     for name in names:
  94.         srcname = os.path.join(src, name)
  95.         dstname = os.path.join(dst, name)
  96.         
  97.         try:
  98.             if symlinks and os.path.islink(srcname):
  99.                 linkto = os.readlink(srcname)
  100.                 os.symlink(linkto, dstname)
  101.             elif os.path.isdir(srcname):
  102.                 copytree(srcname, dstname)
  103.             else:
  104.                 copy2(srcname, dstname)
  105.         except (IOError, os.error):
  106.             0
  107.             why = 0
  108.             names
  109.             print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
  110.         except:
  111.             0
  112.  
  113.     
  114.  
  115.  
  116. def rmtree(path, ignore_errors = 0, onerror = None):
  117.     '''Recursively delete a directory tree.
  118.  
  119.     If ignore_errors is set, errors are ignored; otherwise, if
  120.     onerror is set, it is called to handle the error; otherwise, an
  121.     exception is raised.
  122.  
  123.     '''
  124.     cmdtuples = []
  125.     _build_cmdtuple(path, cmdtuples)
  126.     for cmd in cmdtuples:
  127.         
  128.         try:
  129.             apply(cmd[0], (cmd[1],))
  130.         except:
  131.             0
  132.             cmdtuples
  133.             exc = sys.exc_info()
  134.             if ignore_errors:
  135.                 pass
  136.             elif onerror:
  137.                 onerror(cmd[0], cmd[1], exc)
  138.             else:
  139.                 raise exc[0], (exc[1][0], exc[1][1] + ' removing ' + cmd[1])
  140.  
  141.     
  142.  
  143.  
  144. def _build_cmdtuple(path, cmdtuples):
  145.     for f in os.listdir(path):
  146.         real_f = os.path.join(path, f)
  147.     
  148.     cmdtuples.append(os.rmdir, path)
  149.  
  150.