1 """Utility functions for copying files and directory trees.
3 XXX The functions here don't copy the resource fork or other metadata on Mac.
12 __all__
= ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
13 "copytree","move","rmtree","Error"]
15 class Error(exceptions
.EnvironmentError):
18 def copyfileobj(fsrc
, fdst
, length
=16*1024):
19 """copy data from file-like object fsrc to file-like object fdst"""
21 buf
= fsrc
.read(length
)
27 def copyfile(src
, dst
):
28 """Copy data from src to dst"""
31 # check for same pathname; all platforms
32 _src
= os
.path
.normcase(os
.path
.abspath(src
))
33 _dst
= os
.path
.normcase(os
.path
.abspath(dst
))
37 fsrc
= open(src
, 'rb')
38 fdst
= open(dst
, 'wb')
39 copyfileobj(fsrc
, fdst
)
46 def copymode(src
, dst
):
47 """Copy mode bits from src to dst"""
48 if hasattr(os
, 'chmod'):
50 mode
= stat
.S_IMODE(st
.st_mode
)
53 def copystat(src
, dst
):
54 """Copy all stat info (mode bits, atime and mtime) from src to dst"""
56 mode
= stat
.S_IMODE(st
.st_mode
)
57 if hasattr(os
, 'utime'):
58 os
.utime(dst
, (st
.st_atime
, st
.st_mtime
))
59 if hasattr(os
, 'chmod'):
64 """Copy data and mode bits ("cp src dst").
66 The destination may be a directory.
69 if os
.path
.isdir(dst
):
70 dst
= os
.path
.join(dst
, os
.path
.basename(src
))
75 """Copy data and all stat info ("cp -p src dst").
77 The destination may be a directory.
80 if os
.path
.isdir(dst
):
81 dst
= os
.path
.join(dst
, os
.path
.basename(src
))
86 def copytree(src
, dst
, symlinks
=False):
87 """Recursively copy a directory tree using copy2().
89 The destination directory must not already exist.
90 If exception(s) occur, an Error is raised with a list of reasons.
92 If the optional symlinks flag is true, symbolic links in the
93 source tree result in symbolic links in the destination tree; if
94 it is false, the contents of the files pointed to by symbolic
97 XXX Consider this example code rather than the ultimate tool.
100 names
= os
.listdir(src
)
104 srcname
= os
.path
.join(src
, name
)
105 dstname
= os
.path
.join(dst
, name
)
107 if symlinks
and os
.path
.islink(srcname
):
108 linkto
= os
.readlink(srcname
)
109 os
.symlink(linkto
, dstname
)
110 elif os
.path
.isdir(srcname
):
111 copytree(srcname
, dstname
, symlinks
)
113 copy2(srcname
, dstname
)
114 # XXX What about devices, sockets etc.?
115 except (IOError, os
.error
), why
:
116 errors
.append((srcname
, dstname
, why
))
120 def rmtree(path
, ignore_errors
=False, onerror
=None):
121 """Recursively delete a directory tree.
123 If ignore_errors is set, errors are ignored; otherwise, if
124 onerror is set, it is called to handle the error; otherwise, an
130 _build_cmdtuple(path
, cmdtuples
)
131 for func
, arg
in cmdtuples
:
137 elif onerror
is not None:
138 onerror(func
, arg
, exc
)
140 raise exc
[0], (exc
[1][0], exc
[1][1] + ' removing '+arg
)
142 # Helper for rmtree()
143 def _build_cmdtuple(path
, cmdtuples
):
144 for f
in os
.listdir(path
):
145 real_f
= os
.path
.join(path
,f
)
146 if os
.path
.isdir(real_f
) and not os
.path
.islink(real_f
):
147 _build_cmdtuple(real_f
, cmdtuples
)
149 cmdtuples
.append((os
.remove
, real_f
))
150 cmdtuples
.append((os
.rmdir
, path
))
154 """Recursively move a file or directory to another location.
156 If the destination is on our current filesystem, then simply use
157 rename. Otherwise, copy src to the dst and then remove src.
158 A lot more could be done here... A look at a mv.c shows a lot of
159 the issues this implementation glosses over.
166 if os
.path
.isdir(src
):
167 copytree(src
, dst
, symlinks
=True)