# This file was taken from the python standard library and has been # slightly modified to do a "yield" after every 16KB of copying """Utility functions for copying files and directory trees. XXX The functions here don't copy the resource fork or other metadata on Mac. """ import os import sys import stat from os.path import abspath __all__ = ["copyfileobj","copyfile","copystat","copy2","BLOCK_SIZE", "copytree","move","rmtree","Error", "SpecialFileError"] APPENDIX = '_' BLOCK_SIZE = 16 * 1024 class Error(EnvironmentError): pass class SpecialFileError(EnvironmentError): """Raised when trying to do a kind of operation (e.g. copying) which is not supported on a special file (e.g. a named pipe)""" try: WindowsError except NameError: WindowsError = None def copyfileobj(fsrc, fdst, length=BLOCK_SIZE): """copy data from file-like object fsrc to file-like object fdst""" done = 0 while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf) done += len(buf) yield done def _samefile(src, dst): # Macintosh, Unix. if hasattr(os.path,'samefile'): try: return os.path.samefile(src, dst) except OSError: return False # All other platforms: check for same pathname. return (os.path.normcase(abspath(src)) == os.path.normcase(abspath(dst))) def copyfile(src, dst): """Copy data from src to dst""" if _samefile(src, dst): raise Error("`%s` and `%s` are the same file" % (src, dst)) fsrc = None fdst = None for fn in [src, dst]: try: st = os.stat(fn) except OSError: # File most likely does not exist pass else: # XXX What about other special files? (sockets, devices...) if stat.S_ISFIFO(st.st_mode): raise SpecialFileError("`%s` is a named pipe" % fn) try: fsrc = open(src, 'rb') fdst = open(dst, 'wb') for done in copyfileobj(fsrc, fdst): yield done finally: if fdst: fdst.close() if fsrc: fsrc.close() def copystat(src, dst): """Copy all stat info (mode bits, atime, mtime, flags) from src to dst""" st = os.lstat(src) mode = stat.S_IMODE(st.st_mode) if hasattr(os, 'utime'): try: os.utime(dst, (st.st_atime, st.st_mtime)) except: pass if hasattr(os, 'chmod'): try: os.chmod(dst, mode) except: pass if hasattr(os, 'chflags') and hasattr(st, 'st_flags'): try: os.chflags(dst, st.st_flags) except: pass def copy2(src, dst, overwrite=False, symlinks=False): """Copy data and all stat info ("cp -p src dst"). The destination may be a directory. """ if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) if not overwrite: dst = get_safe_path(dst) if symlinks and os.path.islink(src): linkto = os.readlink(src) if overwrite and os.path.lexists(dst): os.unlink(dst) os.symlink(linkto, dst) else: for done in copyfile(src, dst): yield done copystat(src, dst) def get_safe_path(dst): if not os.path.exists(dst): return dst if not dst.endswith(APPENDIX): dst += APPENDIX if not os.path.exists(dst): return dst n = 0 test_dst = dst + str(n) while os.path.exists(test_dst): n += 1 test_dst = dst + str(n) return test_dst def copytree(src, dst, symlinks=False, ignore=None, overwrite=False): """Recursively copy a directory tree using copy2(). The destination directory must not already exist. If exception(s) occur, an Error is raised with a list of reasons. If the optional symlinks flag is true, symbolic links in the source tree result in symbolic links in the destination tree; if it is false, the contents of the files pointed to by symbolic links are copied. The opti
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module ranger.ext.waitpid_no_intr</title>
<meta http-equiv=