blob: e230ce587e26a5758d4250ec7fa4318ead8dc090 [file] [log] [blame]
Greg Wardaebf7062000-04-04 02:05:59 +00001"""distutils.file_util
2
Greg Ward449f5562000-09-26 02:03:34 +00003Utility functions for operating on single files.
4"""
Greg Wardaebf7062000-04-04 02:05:59 +00005
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00006# This module should be kept compatible with Python 1.5.2.
7
Greg Wardaebf7062000-04-04 02:05:59 +00008__revision__ = "$Id$"
9
10import os
11from distutils.errors import DistutilsFileError
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000012from distutils import log
Greg Wardaebf7062000-04-04 02:05:59 +000013
14# for generating verbose output in 'copy_file()'
15_copy_action = { None: 'copying',
16 'hard': 'hard linking',
17 'sym': 'symbolically linking' }
18
19
20def _copy_file_contents (src, dst, buffer_size=16*1024):
21 """Copy the file 'src' to 'dst'; both must be filenames. Any error
Greg Ward9e3dc4e2000-09-23 00:59:34 +000022 opening either file, reading from 'src', or writing to 'dst', raises
23 DistutilsFileError. Data is read/written in chunks of 'buffer_size'
24 bytes (default 16k). No attempt is made to handle anything apart from
25 regular files.
26 """
Greg Wardaebf7062000-04-04 02:05:59 +000027 # Stolen from shutil module in the standard library, but with
28 # custom error-handling added.
29
30 fsrc = None
31 fdst = None
32 try:
33 try:
34 fsrc = open(src, 'rb')
35 except os.error, (errno, errstr):
36 raise DistutilsFileError, \
37 "could not open '%s': %s" % (src, errstr)
Fred Drakeb94b8492001-12-06 20:51:35 +000038
Andrew M. Kuchling3b388ec2002-02-01 18:29:34 +000039 if os.path.exists(dst):
40 try:
41 os.unlink(dst)
42 except os.error, (errno, errstr):
43 raise DistutilsFileError, \
44 "could not delete '%s': %s" % (dst, errstr)
45
Greg Wardaebf7062000-04-04 02:05:59 +000046 try:
47 fdst = open(dst, 'wb')
48 except os.error, (errno, errstr):
49 raise DistutilsFileError, \
50 "could not create '%s': %s" % (dst, errstr)
Fred Drakeb94b8492001-12-06 20:51:35 +000051
Greg Wardaebf7062000-04-04 02:05:59 +000052 while 1:
53 try:
Greg Ward9e3dc4e2000-09-23 00:59:34 +000054 buf = fsrc.read(buffer_size)
Greg Wardaebf7062000-04-04 02:05:59 +000055 except os.error, (errno, errstr):
56 raise DistutilsFileError, \
57 "could not read from '%s': %s" % (src, errstr)
Fred Drakeb94b8492001-12-06 20:51:35 +000058
Greg Wardaebf7062000-04-04 02:05:59 +000059 if not buf:
60 break
61
62 try:
63 fdst.write(buf)
64 except os.error, (errno, errstr):
65 raise DistutilsFileError, \
66 "could not write to '%s': %s" % (dst, errstr)
Fred Drakeb94b8492001-12-06 20:51:35 +000067
Greg Wardaebf7062000-04-04 02:05:59 +000068 finally:
69 if fdst:
70 fdst.close()
71 if fsrc:
72 fsrc.close()
73
74# _copy_file_contents()
75
Greg Wardaebf7062000-04-04 02:05:59 +000076def copy_file (src, dst,
77 preserve_mode=1,
78 preserve_times=1,
79 update=0,
80 link=None,
81 verbose=0,
82 dry_run=0):
83
Greg Ward9e3dc4e2000-09-23 00:59:34 +000084 """Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is
85 copied there with the same name; otherwise, it must be a filename. (If
86 the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
87 is true (the default), the file's mode (type and permission bits, or
88 whatever is analogous on the current platform) is copied. If
89 'preserve_times' is true (the default), the last-modified and
90 last-access times are copied as well. If 'update' is true, 'src' will
91 only be copied if 'dst' does not exist, or if 'dst' does exist but is
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000092 older than 'src'.
Greg Wardaebf7062000-04-04 02:05:59 +000093
Greg Ward9e3dc4e2000-09-23 00:59:34 +000094 'link' allows you to make hard links (os.link) or symbolic links
95 (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
96 None (the default), files are copied. Don't set 'link' on systems that
97 don't support it: 'copy_file()' doesn't check if hard or symbolic
98 linking is available.
Greg Wardaebf7062000-04-04 02:05:59 +000099
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000100 Under Mac OS, uses the native file copy function in macostools; on
101 other systems, uses '_copy_file_contents()' to copy file contents.
Greg Wardaebf7062000-04-04 02:05:59 +0000102
Greg Ward0d4a8532000-09-30 17:29:35 +0000103 Return a tuple (dest_name, copied): 'dest_name' is the actual name of
104 the output file, and 'copied' is true if the file was copied (or would
105 have been copied, if 'dry_run' true).
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000106 """
Greg Wardaebf7062000-04-04 02:05:59 +0000107 # XXX if the destination file already exists, we clobber it if
108 # copying, but blow up if linking. Hmmm. And I don't know what
109 # macostools.copyfile() does. Should definitely be consistent, and
110 # should probably blow up if destination exists and we would be
111 # changing it (ie. it's not already a hard/soft link to src OR
112 # (not update) and (src newer than dst).
113
Greg Wardaebf7062000-04-04 02:05:59 +0000114 from distutils.dep_util import newer
Greg Warde628a2f2001-07-25 19:48:03 +0000115 from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE
Greg Wardaebf7062000-04-04 02:05:59 +0000116
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000117 if not os.path.isfile(src):
Greg Wardaebf7062000-04-04 02:05:59 +0000118 raise DistutilsFileError, \
119 "can't copy '%s': doesn't exist or not a regular file" % src
120
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000121 if os.path.isdir(dst):
Greg Wardaebf7062000-04-04 02:05:59 +0000122 dir = dst
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000123 dst = os.path.join(dst, os.path.basename(src))
Greg Wardaebf7062000-04-04 02:05:59 +0000124 else:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000125 dir = os.path.dirname(dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000126
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000127 if update and not newer(src, dst):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000128 log.debug("not copying %s (output up-to-date)", src)
129 return dst, 0
Greg Wardaebf7062000-04-04 02:05:59 +0000130
131 try:
132 action = _copy_action[link]
133 except KeyError:
134 raise ValueError, \
135 "invalid value '%s' for 'link' argument" % link
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000136 if os.path.basename(dst) == os.path.basename(src):
137 log.info("%s %s -> %s", action, src, dir)
138 else:
139 log.info("%s %s -> %s", action, src, dst)
Fred Drakeb94b8492001-12-06 20:51:35 +0000140
Greg Wardaebf7062000-04-04 02:05:59 +0000141 if dry_run:
Greg Ward0d4a8532000-09-30 17:29:35 +0000142 return (dst, 1)
Greg Wardaebf7062000-04-04 02:05:59 +0000143
Greg Ward0d4a8532000-09-30 17:29:35 +0000144 # On Mac OS, use the native file copy routine
Greg Wardaebf7062000-04-04 02:05:59 +0000145 if os.name == 'mac':
146 import macostools
147 try:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000148 macostools.copy(src, dst, 0, preserve_times)
Greg Ward3af07e92000-04-22 15:17:14 +0000149 except os.error, exc:
Greg Wardaebf7062000-04-04 02:05:59 +0000150 raise DistutilsFileError, \
151 "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])
Fred Drakeb94b8492001-12-06 20:51:35 +0000152
Greg Wardaebf7062000-04-04 02:05:59 +0000153 # If linking (hard or symbolic), use the appropriate system call
154 # (Unix only, of course, but that's the caller's responsibility)
155 elif link == 'hard':
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000156 if not (os.path.exists(dst) and os.path.samefile(src, dst)):
157 os.link(src, dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000158 elif link == 'sym':
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000159 if not (os.path.exists(dst) and os.path.samefile(src, dst)):
160 os.symlink(src, dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000161
162 # Otherwise (non-Mac, not linking), copy the file contents and
163 # (optionally) copy the times and mode.
164 else:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000165 _copy_file_contents(src, dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000166 if preserve_mode or preserve_times:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000167 st = os.stat(src)
Greg Wardaebf7062000-04-04 02:05:59 +0000168
169 # According to David Ascher <da@ski.org>, utime() should be done
170 # before chmod() (at least under NT).
171 if preserve_times:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000172 os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
Greg Wardaebf7062000-04-04 02:05:59 +0000173 if preserve_mode:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000174 os.chmod(dst, S_IMODE(st[ST_MODE]))
Greg Wardaebf7062000-04-04 02:05:59 +0000175
Greg Ward0d4a8532000-09-30 17:29:35 +0000176 return (dst, 1)
Greg Wardaebf7062000-04-04 02:05:59 +0000177
178# copy_file ()
179
180
181# XXX I suspect this is Unix-specific -- need porting help!
182def move_file (src, dst,
183 verbose=0,
184 dry_run=0):
185
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000186 """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will
187 be moved into it with the same name; otherwise, 'src' is just renamed
188 to 'dst'. Return the new full name of the file.
Greg Wardaebf7062000-04-04 02:05:59 +0000189
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000190 Handles cross-device moves on Unix using 'copy_file()'. What about
191 other systems???
192 """
Greg Wardaebf7062000-04-04 02:05:59 +0000193 from os.path import exists, isfile, isdir, basename, dirname
Andrew M. Kuchling106ffdb2001-08-09 20:59:53 +0000194 import errno
Fred Drakeb94b8492001-12-06 20:51:35 +0000195
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000196 log.info("moving %s -> %s", src, dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000197
198 if dry_run:
199 return dst
200
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000201 if not isfile(src):
Greg Wardaebf7062000-04-04 02:05:59 +0000202 raise DistutilsFileError, \
203 "can't move '%s': not a regular file" % src
204
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000205 if isdir(dst):
206 dst = os.path.join(dst, basename(src))
207 elif exists(dst):
Greg Wardaebf7062000-04-04 02:05:59 +0000208 raise DistutilsFileError, \
209 "can't move '%s': destination '%s' already exists" % \
210 (src, dst)
211
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000212 if not isdir(dirname(dst)):
Greg Wardaebf7062000-04-04 02:05:59 +0000213 raise DistutilsFileError, \
214 "can't move '%s': destination '%s' not a valid path" % \
215 (src, dst)
216
217 copy_it = 0
218 try:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000219 os.rename(src, dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000220 except os.error, (num, msg):
221 if num == errno.EXDEV:
222 copy_it = 1
223 else:
224 raise DistutilsFileError, \
225 "couldn't move '%s' to '%s': %s" % (src, dst, msg)
226
227 if copy_it:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000228 copy_file(src, dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000229 try:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000230 os.unlink(src)
Greg Wardaebf7062000-04-04 02:05:59 +0000231 except os.error, (num, msg):
232 try:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000233 os.unlink(dst)
Greg Wardaebf7062000-04-04 02:05:59 +0000234 except os.error:
235 pass
236 raise DistutilsFileError, \
Fred Drakeb94b8492001-12-06 20:51:35 +0000237 ("couldn't move '%s' to '%s' by copy/delete: " +
Greg Wardaebf7062000-04-04 02:05:59 +0000238 "delete '%s' failed: %s") % \
239 (src, dst, src, msg)
240
241 return dst
242
243# move_file ()
244
245
246def write_file (filename, contents):
247 """Create a file with the specified name and write 'contents' (a
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000248 sequence of strings without line terminators) to it.
249 """
250 f = open(filename, "w")
Greg Wardaebf7062000-04-04 02:05:59 +0000251 for line in contents:
Greg Ward9e3dc4e2000-09-23 00:59:34 +0000252 f.write(line + "\n")
253 f.close()