]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/distutils/file_util.py
3 Utility functions for operating on single files.
6 # This module should be kept compatible with Python 1.5.2.
11 from distutils
.errors
import DistutilsFileError
12 from distutils
import log
14 # for generating verbose output in 'copy_file()'
15 _copy_action
= { None: 'copying',
16 'hard': 'hard linking',
17 'sym': 'symbolically linking' }
20 def _copy_file_contents (src
, dst
, buffer_size
=16*1024):
21 """Copy the file 'src' to 'dst'; both must be filenames. Any error
22 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
27 # Stolen from shutil module in the standard library, but with
28 # custom error-handling added.
34 fsrc
= open(src
, 'rb')
35 except os
.error
, (errno
, errstr
):
36 raise DistutilsFileError
, \
37 "could not open '%s': %s" % (src
, errstr
)
39 if os
.path
.exists(dst
):
42 except os
.error
, (errno
, errstr
):
43 raise DistutilsFileError
, \
44 "could not delete '%s': %s" % (dst
, errstr
)
47 fdst
= open(dst
, 'wb')
48 except os
.error
, (errno
, errstr
):
49 raise DistutilsFileError
, \
50 "could not create '%s': %s" % (dst
, errstr
)
54 buf
= fsrc
.read(buffer_size
)
55 except os
.error
, (errno
, errstr
):
56 raise DistutilsFileError
, \
57 "could not read from '%s': %s" % (src
, errstr
)
64 except os
.error
, (errno
, errstr
):
65 raise DistutilsFileError
, \
66 "could not write to '%s': %s" % (dst
, errstr
)
74 # _copy_file_contents()
76 def copy_file (src
, dst
,
84 """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
94 '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
100 Under Mac OS, uses the native file copy function in macostools; on
101 other systems, uses '_copy_file_contents()' to copy file contents.
103 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).
107 # 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).
114 from distutils
.dep_util
import newer
115 from stat
import ST_ATIME
, ST_MTIME
, ST_MODE
, S_IMODE
117 if not os
.path
.isfile(src
):
118 raise DistutilsFileError
, \
119 "can't copy '%s': doesn't exist or not a regular file" % src
121 if os
.path
.isdir(dst
):
123 dst
= os
.path
.join(dst
, os
.path
.basename(src
))
125 dir = os
.path
.dirname(dst
)
127 if update
and not newer(src
, dst
):
128 log
.debug("not copying %s (output up-to-date)", src
)
132 action
= _copy_action
[link
]
135 "invalid value '%s' for 'link' argument" % link
136 if os
.path
.basename(dst
) == os
.path
.basename(src
):
137 log
.info("%s %s -> %s", action
, src
, dir)
139 log
.info("%s %s -> %s", action
, src
, dst
)
144 # On Mac OS, use the native file copy routine
148 macostools
.copy(src
, dst
, 0, preserve_times
)
149 except os
.error
, exc
:
150 raise DistutilsFileError
, \
151 "could not copy '%s' to '%s': %s" % (src
, dst
, exc
[-1])
153 # If linking (hard or symbolic), use the appropriate system call
154 # (Unix only, of course, but that's the caller's responsibility)
156 if not (os
.path
.exists(dst
) and os
.path
.samefile(src
, dst
)):
159 if not (os
.path
.exists(dst
) and os
.path
.samefile(src
, dst
)):
162 # Otherwise (non-Mac, not linking), copy the file contents and
163 # (optionally) copy the times and mode.
165 _copy_file_contents(src
, dst
)
166 if preserve_mode
or preserve_times
:
169 # According to David Ascher <da@ski.org>, utime() should be done
170 # before chmod() (at least under NT).
172 os
.utime(dst
, (st
[ST_ATIME
], st
[ST_MTIME
]))
174 os
.chmod(dst
, S_IMODE(st
[ST_MODE
]))
181 # XXX I suspect this is Unix-specific -- need porting help!
182 def move_file (src
, dst
,
186 """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.
190 Handles cross-device moves on Unix using 'copy_file()'. What about
193 from os
.path
import exists
, isfile
, isdir
, basename
, dirname
196 log
.info("moving %s -> %s", src
, dst
)
202 raise DistutilsFileError
, \
203 "can't move '%s': not a regular file" % src
206 dst
= os
.path
.join(dst
, basename(src
))
208 raise DistutilsFileError
, \
209 "can't move '%s': destination '%s' already exists" % \
212 if not isdir(dirname(dst
)):
213 raise DistutilsFileError
, \
214 "can't move '%s': destination '%s' not a valid path" % \
220 except os
.error
, (num
, msg
):
221 if num
== errno
.EXDEV
:
224 raise DistutilsFileError
, \
225 "couldn't move '%s' to '%s': %s" % (src
, dst
, msg
)
231 except os
.error
, (num
, msg
):
236 raise DistutilsFileError
, \
237 ("couldn't move '%s' to '%s' by copy/delete: " +
238 "delete '%s' failed: %s") % \
246 def write_file (filename
, contents
):
247 """Create a file with the specified name and write 'contents' (a
248 sequence of strings without line terminators) to it.
250 f
= open(filename
, "w")
251 for line
in contents
: