| OLD | NEW |
| 1 """Utility functions for copying files and directory trees. | 1 """Utility functions for copying files and directory trees. |
| 2 | 2 |
| 3 XXX The functions here don't copy the resource fork or other metadata on Mac. | 3 XXX The functions here don't copy the resource fork or other metadata on Mac. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import sys | 8 import sys |
| 9 import stat | 9 import stat |
| 10 from os.path import abspath | 10 from os.path import abspath |
| 11 | 11 |
| 12 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", | 12 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", |
| 13 "copytree","move","rmtree","Error"] | 13 "copytree","move","rmtree","Error"] |
| 14 | 14 |
| 15 class Error(EnvironmentError): | 15 class Error(EnvironmentError): |
| 16 pass | 16 pass |
| 17 |
| 18 try: |
| 19 WindowsError |
| 20 except NameError: |
| 21 WindowsError = None |
| 17 | 22 |
| 18 def copyfileobj(fsrc, fdst, length=16*1024): | 23 def copyfileobj(fsrc, fdst, length=16*1024): |
| 19 """copy data from file-like object fsrc to file-like object fdst""" | 24 """copy data from file-like object fsrc to file-like object fdst""" |
| 20 while 1: | 25 while 1: |
| 21 buf = fsrc.read(length) | 26 buf = fsrc.read(length) |
| 22 if not buf: | 27 if not buf: |
| 23 break | 28 break |
| 24 fdst.write(buf) | 29 fdst.write(buf) |
| 25 | 30 |
| 26 def _samefile(src, dst): | 31 def _samefile(src, dst): |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 copy2(srcname, dstname) | 129 copy2(srcname, dstname) |
| 125 # XXX What about devices, sockets etc.? | 130 # XXX What about devices, sockets etc.? |
| 126 except (IOError, os.error), why: | 131 except (IOError, os.error), why: |
| 127 errors.append((srcname, dstname, str(why))) | 132 errors.append((srcname, dstname, str(why))) |
| 128 # catch the Error from the recursive copytree so that we can | 133 # catch the Error from the recursive copytree so that we can |
| 129 # continue with other files | 134 # continue with other files |
| 130 except Error, err: | 135 except Error, err: |
| 131 errors.extend(err.args[0]) | 136 errors.extend(err.args[0]) |
| 132 try: | 137 try: |
| 133 copystat(src, dst) | 138 copystat(src, dst) |
| 134 except WindowsError: | |
| 135 # can't copy file access times on Windows | |
| 136 pass | |
| 137 except OSError, why: | 139 except OSError, why: |
| 138 errors.extend((src, dst, str(why))) | 140 if WindowsError is not None and isinstance(why, WindowsError): |
| 141 # Copying file access times may fail on Windows |
| 142 pass |
| 143 else: |
| 144 errors.extend((src, dst, str(why))) |
| 139 if errors: | 145 if errors: |
| 140 raise Error, errors | 146 raise Error, errors |
| 141 | 147 |
| 142 def rmtree(path, ignore_errors=False, onerror=None): | 148 def rmtree(path, ignore_errors=False, onerror=None): |
| 143 """Recursively delete a directory tree. | 149 """Recursively delete a directory tree. |
| 144 | 150 |
| 145 If ignore_errors is set, errors are ignored; otherwise, if onerror | 151 If ignore_errors is set, errors are ignored; otherwise, if onerror |
| 146 is set, it is called to handle the error with arguments (func, | 152 is set, it is called to handle the error with arguments (func, |
| 147 path, exc_info) where func is os.listdir, os.remove, or os.rmdir; | 153 path, exc_info) where func is os.listdir, os.remove, or os.rmdir; |
| 148 path is the argument to that function that caused it to fail; and | 154 path is the argument to that function that caused it to fail; and |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 if destinsrc(src, dst): | 228 if destinsrc(src, dst): |
| 223 raise Error, "Cannot move a directory '%s' into itself '%s'." %
(src, dst) | 229 raise Error, "Cannot move a directory '%s' into itself '%s'." %
(src, dst) |
| 224 copytree(src, real_dst, symlinks=True) | 230 copytree(src, real_dst, symlinks=True) |
| 225 rmtree(src) | 231 rmtree(src) |
| 226 else: | 232 else: |
| 227 copy2(src, real_dst) | 233 copy2(src, real_dst) |
| 228 os.unlink(src) | 234 os.unlink(src) |
| 229 | 235 |
| 230 def destinsrc(src, dst): | 236 def destinsrc(src, dst): |
| 231 return abspath(dst).startswith(abspath(src)) | 237 return abspath(dst).startswith(abspath(src)) |
| OLD | NEW |