/Lib/shutil.py

http://unladen-swallow.googlecode.com/ · Python · 274 lines · 175 code · 19 blank · 80 comment · 14 complexity · 5c3c293bbca07f973b9659195f718377 MD5 · raw file

  1. """Utility functions for copying files and directory trees.
  2. XXX The functions here don't copy the resource fork or other metadata on Mac.
  3. """
  4. import os
  5. import sys
  6. import stat
  7. from os.path import abspath
  8. import fnmatch
  9. __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
  10. "copytree","move","rmtree","Error"]
  11. class Error(EnvironmentError):
  12. pass
  13. try:
  14. WindowsError
  15. except NameError:
  16. WindowsError = None
  17. def copyfileobj(fsrc, fdst, length=16*1024):
  18. """copy data from file-like object fsrc to file-like object fdst"""
  19. while 1:
  20. buf = fsrc.read(length)
  21. if not buf:
  22. break
  23. fdst.write(buf)
  24. def _samefile(src, dst):
  25. # Macintosh, Unix.
  26. if hasattr(os.path,'samefile'):
  27. try:
  28. return os.path.samefile(src, dst)
  29. except OSError:
  30. return False
  31. # All other platforms: check for same pathname.
  32. return (os.path.normcase(os.path.abspath(src)) ==
  33. os.path.normcase(os.path.abspath(dst)))
  34. def copyfile(src, dst):
  35. """Copy data from src to dst"""
  36. if _samefile(src, dst):
  37. raise Error, "`%s` and `%s` are the same file" % (src, dst)
  38. fsrc = None
  39. fdst = None
  40. try:
  41. fsrc = open(src, 'rb')
  42. fdst = open(dst, 'wb')
  43. copyfileobj(fsrc, fdst)
  44. finally:
  45. if fdst:
  46. fdst.close()
  47. if fsrc:
  48. fsrc.close()
  49. def copymode(src, dst):
  50. """Copy mode bits from src to dst"""
  51. if hasattr(os, 'chmod'):
  52. st = os.stat(src)
  53. mode = stat.S_IMODE(st.st_mode)
  54. os.chmod(dst, mode)
  55. def copystat(src, dst):
  56. """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
  57. st = os.stat(src)
  58. mode = stat.S_IMODE(st.st_mode)
  59. if hasattr(os, 'utime'):
  60. os.utime(dst, (st.st_atime, st.st_mtime))
  61. if hasattr(os, 'chmod'):
  62. os.chmod(dst, mode)
  63. if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
  64. os.chflags(dst, st.st_flags)
  65. def copy(src, dst):
  66. """Copy data and mode bits ("cp src dst").
  67. The destination may be a directory.
  68. """
  69. if os.path.isdir(dst):
  70. dst = os.path.join(dst, os.path.basename(src))
  71. copyfile(src, dst)
  72. copymode(src, dst)
  73. def copy2(src, dst):
  74. """Copy data and all stat info ("cp -p src dst").
  75. The destination may be a directory.
  76. """
  77. if os.path.isdir(dst):
  78. dst = os.path.join(dst, os.path.basename(src))
  79. copyfile(src, dst)
  80. copystat(src, dst)
  81. def ignore_patterns(*patterns):
  82. """Function that can be used as copytree() ignore parameter.
  83. Patterns is a sequence of glob-style patterns
  84. that are used to exclude files"""
  85. def _ignore_patterns(path, names):
  86. ignored_names = []
  87. for pattern in patterns:
  88. ignored_names.extend(fnmatch.filter(names, pattern))
  89. return set(ignored_names)
  90. return _ignore_patterns
  91. def copytree(src, dst, symlinks=False, ignore=None):
  92. """Recursively copy a directory tree using copy2().
  93. The destination directory must not already exist.
  94. If exception(s) occur, an Error is raised with a list of reasons.
  95. If the optional symlinks flag is true, symbolic links in the
  96. source tree result in symbolic links in the destination tree; if
  97. it is false, the contents of the files pointed to by symbolic
  98. links are copied.
  99. The optional ignore argument is a callable. If given, it
  100. is called with the `src` parameter, which is the directory
  101. being visited by copytree(), and `names` which is the list of
  102. `src` contents, as returned by os.listdir():
  103. callable(src, names) -> ignored_names
  104. Since copytree() is called recursively, the callable will be
  105. called once for each directory that is copied. It returns a
  106. list of names relative to the `src` directory that should
  107. not be copied.
  108. XXX Consider this example code rather than the ultimate tool.
  109. """
  110. names = os.listdir(src)
  111. if ignore is not None:
  112. ignored_names = ignore(src, names)
  113. else:
  114. ignored_names = set()
  115. os.makedirs(dst)
  116. errors = []
  117. for name in names:
  118. if name in ignored_names:
  119. continue
  120. srcname = os.path.join(src, name)
  121. dstname = os.path.join(dst, name)
  122. try:
  123. if symlinks and os.path.islink(srcname):
  124. linkto = os.readlink(srcname)
  125. os.symlink(linkto, dstname)
  126. elif os.path.isdir(srcname):
  127. copytree(srcname, dstname, symlinks, ignore)
  128. else:
  129. copy2(srcname, dstname)
  130. # XXX What about devices, sockets etc.?
  131. except (IOError, os.error), why:
  132. errors.append((srcname, dstname, str(why)))
  133. # catch the Error from the recursive copytree so that we can
  134. # continue with other files
  135. except Error, err:
  136. errors.extend(err.args[0])
  137. try:
  138. copystat(src, dst)
  139. except OSError, 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)))
  145. if errors:
  146. raise Error, errors
  147. def rmtree(path, ignore_errors=False, onerror=None):
  148. """Recursively delete a directory tree.
  149. If ignore_errors is set, errors are ignored; otherwise, if onerror
  150. is set, it is called to handle the error with arguments (func,
  151. path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
  152. path is the argument to that function that caused it to fail; and
  153. exc_info is a tuple returned by sys.exc_info(). If ignore_errors
  154. is false and onerror is None, an exception is raised.
  155. """
  156. if ignore_errors:
  157. def onerror(*args):
  158. pass
  159. elif onerror is None:
  160. def onerror(*args):
  161. raise
  162. try:
  163. if os.path.islink(path):
  164. # symlinks to directories are forbidden, see bug #1669
  165. raise OSError("Cannot call rmtree on a symbolic link")
  166. except OSError:
  167. onerror(os.path.islink, path, sys.exc_info())
  168. # can't continue even if onerror hook returns
  169. return
  170. names = []
  171. try:
  172. names = os.listdir(path)
  173. except os.error, err:
  174. onerror(os.listdir, path, sys.exc_info())
  175. for name in names:
  176. fullname = os.path.join(path, name)
  177. try:
  178. mode = os.lstat(fullname).st_mode
  179. except os.error:
  180. mode = 0
  181. if stat.S_ISDIR(mode):
  182. rmtree(fullname, ignore_errors, onerror)
  183. else:
  184. try:
  185. os.remove(fullname)
  186. except os.error, err:
  187. onerror(os.remove, fullname, sys.exc_info())
  188. try:
  189. os.rmdir(path)
  190. except os.error:
  191. onerror(os.rmdir, path, sys.exc_info())
  192. def _basename(path):
  193. # A basename() variant which first strips the trailing slash, if present.
  194. # Thus we always get the last component of the path, even for directories.
  195. return os.path.basename(path.rstrip(os.path.sep))
  196. def move(src, dst):
  197. """Recursively move a file or directory to another location. This is
  198. similar to the Unix "mv" command.
  199. If the destination is a directory or a symlink to a directory, the source
  200. is moved inside the directory. The destination path must not already
  201. exist.
  202. If the destination already exists but is not a directory, it may be
  203. overwritten depending on os.rename() semantics.
  204. If the destination is on our current filesystem, then rename() is used.
  205. Otherwise, src is copied to the destination and then removed.
  206. A lot more could be done here... A look at a mv.c shows a lot of
  207. the issues this implementation glosses over.
  208. """
  209. real_dst = dst
  210. if os.path.isdir(dst):
  211. real_dst = os.path.join(dst, _basename(src))
  212. if os.path.exists(real_dst):
  213. raise Error, "Destination path '%s' already exists" % real_dst
  214. try:
  215. os.rename(src, real_dst)
  216. except OSError:
  217. if os.path.isdir(src):
  218. if destinsrc(src, dst):
  219. raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
  220. copytree(src, real_dst, symlinks=True)
  221. rmtree(src)
  222. else:
  223. copy2(src, real_dst)
  224. os.unlink(src)
  225. def destinsrc(src, dst):
  226. src = abspath(src)
  227. dst = abspath(dst)
  228. if not src.endswith(os.path.sep):
  229. src += os.path.sep
  230. if not dst.endswith(os.path.sep):
  231. dst += os.path.sep
  232. return dst.startswith(src)