home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / shutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  5.6 KB  |  169 lines

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