file_util.py 7.63 KB
Newer Older
1 2
"""distutils.file_util

Greg Ward's avatar
Greg Ward committed
3 4
Utility functions for operating on single files.
"""
5 6 7

import os
from distutils.errors import DistutilsFileError
8
from distutils import log
9 10

# for generating verbose output in 'copy_file()'
11 12 13
_copy_action = { None:   'copying',
                 'hard': 'hard linking',
                 'sym':  'symbolically linking' }
14 15


16
def _copy_file_contents(src, dst, buffer_size=16*1024):
17 18 19 20 21
    """Copy the file 'src' to 'dst'; both must be filenames.  Any error
    opening either file, reading from 'src', or writing to 'dst', raises
    DistutilsFileError.  Data is read/written in chunks of 'buffer_size'
    bytes (default 16k).  No attempt is made to handle anything apart from
    regular files.
Greg Ward's avatar
Greg Ward committed
22
    """
23 24 25 26 27 28 29
    # Stolen from shutil module in the standard library, but with
    # custom error-handling added.
    fsrc = None
    fdst = None
    try:
        try:
            fsrc = open(src, 'rb')
30
        except os.error as e:
31
            raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror))
Fred Drake's avatar
Fred Drake committed
32

33 34 35
        if os.path.exists(dst):
            try:
                os.unlink(dst)
36
            except os.error as e:
37
                raise DistutilsFileError(
38
                      "could not delete '%s': %s" % (dst, e.strerror))
39

40 41
        try:
            fdst = open(dst, 'wb')
42
        except os.error as e:
43
            raise DistutilsFileError(
44
                  "could not create '%s': %s" % (dst, e.strerror))
Fred Drake's avatar
Fred Drake committed
45

46
        while True:
47
            try:
Greg Ward's avatar
Greg Ward committed
48
                buf = fsrc.read(buffer_size)
49
            except os.error as e:
50
                raise DistutilsFileError(
51
                      "could not read from '%s': %s" % (src, e.strerror))
Fred Drake's avatar
Fred Drake committed
52

53 54 55 56 57
            if not buf:
                break

            try:
                fdst.write(buf)
58
            except os.error as e:
59
                raise DistutilsFileError(
60
                      "could not write to '%s': %s" % (dst, e.strerror))
61 62 63 64 65 66
    finally:
        if fdst:
            fdst.close()
        if fsrc:
            fsrc.close()

67
def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
68
              link=None, verbose=1, dry_run=0):
69 70 71 72 73 74 75 76 77
    """Copy a file 'src' to 'dst'.  If 'dst' is a directory, then 'src' is
    copied there with the same name; otherwise, it must be a filename.  (If
    the file exists, it will be ruthlessly clobbered.)  If 'preserve_mode'
    is true (the default), the file's mode (type and permission bits, or
    whatever is analogous on the current platform) is copied.  If
    'preserve_times' is true (the default), the last-modified and
    last-access times are copied as well.  If 'update' is true, 'src' will
    only be copied if 'dst' does not exist, or if 'dst' does exist but is
    older than 'src'.
Greg Ward's avatar
Greg Ward committed
78 79 80 81 82 83 84 85 86 87

    'link' allows you to make hard links (os.link) or symbolic links
    (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
    None (the default), files are copied.  Don't set 'link' on systems that
    don't support it: 'copy_file()' doesn't check if hard or symbolic
    linking is available.

    Under Mac OS, uses the native file copy function in macostools; on
    other systems, uses '_copy_file_contents()' to copy file contents.

88 89 90
    Return a tuple (dest_name, copied): 'dest_name' is the actual name of
    the output file, and 'copied' is true if the file was copied (or would
    have been copied, if 'dry_run' true).
Greg Ward's avatar
Greg Ward committed
91
    """
92 93 94 95 96 97 98 99
    # XXX if the destination file already exists, we clobber it if
    # copying, but blow up if linking.  Hmmm.  And I don't know what
    # macostools.copyfile() does.  Should definitely be consistent, and
    # should probably blow up if destination exists and we would be
    # changing it (ie. it's not already a hard/soft link to src OR
    # (not update) and (src newer than dst).

    from distutils.dep_util import newer
100
    from stat import ST_ATIME, ST_MTIME, ST_MODE, S_IMODE
101

Greg Ward's avatar
Greg Ward committed
102
    if not os.path.isfile(src):
103 104
        raise DistutilsFileError(
              "can't copy '%s': doesn't exist or not a regular file" % src)
105

Greg Ward's avatar
Greg Ward committed
106
    if os.path.isdir(dst):
107
        dir = dst
Greg Ward's avatar
Greg Ward committed
108
        dst = os.path.join(dst, os.path.basename(src))
109
    else:
Greg Ward's avatar
Greg Ward committed
110
        dir = os.path.dirname(dst)
111

Greg Ward's avatar
Greg Ward committed
112
    if update and not newer(src, dst):
113
        if verbose >= 1:
114
            log.debug("not copying %s (output up-to-date)", src)
115
        return (dst, 0)
116 117 118 119

    try:
        action = _copy_action[link]
    except KeyError:
120
        raise ValueError("invalid value '%s' for 'link' argument" % link)
121

122
    if verbose >= 1:
123 124 125 126
        if os.path.basename(dst) == os.path.basename(src):
            log.info("%s %s -> %s", action, src, dir)
        else:
            log.info("%s %s -> %s", action, src, dst)
Fred Drake's avatar
Fred Drake committed
127

128
    if dry_run:
129
        return (dst, 1)
130 131 132 133

    # If linking (hard or symbolic), use the appropriate system call
    # (Unix only, of course, but that's the caller's responsibility)
    elif link == 'hard':
Greg Ward's avatar
Greg Ward committed
134 135
        if not (os.path.exists(dst) and os.path.samefile(src, dst)):
            os.link(src, dst)
136
    elif link == 'sym':
Greg Ward's avatar
Greg Ward committed
137 138
        if not (os.path.exists(dst) and os.path.samefile(src, dst)):
            os.symlink(src, dst)
139 140 141 142

    # Otherwise (non-Mac, not linking), copy the file contents and
    # (optionally) copy the times and mode.
    else:
Greg Ward's avatar
Greg Ward committed
143
        _copy_file_contents(src, dst)
144
        if preserve_mode or preserve_times:
Greg Ward's avatar
Greg Ward committed
145
            st = os.stat(src)
146 147 148 149

            # According to David Ascher <da@ski.org>, utime() should be done
            # before chmod() (at least under NT).
            if preserve_times:
Greg Ward's avatar
Greg Ward committed
150
                os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
151
            if preserve_mode:
Greg Ward's avatar
Greg Ward committed
152
                os.chmod(dst, S_IMODE(st[ST_MODE]))
153

154
    return (dst, 1)
155 156 157


# XXX I suspect this is Unix-specific -- need porting help!
158 159 160
def move_file (src, dst,
               verbose=1,
               dry_run=0):
161

162 163 164
    """Move a file 'src' to 'dst'.  If 'dst' is a directory, the file will
    be moved into it with the same name; otherwise, 'src' is just renamed
    to 'dst'.  Return the new full name of the file.
165

Greg Ward's avatar
Greg Ward committed
166 167 168
    Handles cross-device moves on Unix using 'copy_file()'.  What about
    other systems???
    """
169
    from os.path import exists, isfile, isdir, basename, dirname
Andrew M. Kuchling's avatar
Andrew M. Kuchling committed
170
    import errno
Fred Drake's avatar
Fred Drake committed
171

172
    if verbose >= 1:
173
        log.info("moving %s -> %s", src, dst)
174 175 176 177

    if dry_run:
        return dst

Greg Ward's avatar
Greg Ward committed
178
    if not isfile(src):
179
        raise DistutilsFileError("can't move '%s': not a regular file" % src)
180

Greg Ward's avatar
Greg Ward committed
181 182 183
    if isdir(dst):
        dst = os.path.join(dst, basename(src))
    elif exists(dst):
184 185 186
        raise DistutilsFileError(
              "can't move '%s': destination '%s' already exists" %
              (src, dst))
187

Greg Ward's avatar
Greg Ward committed
188
    if not isdir(dirname(dst)):
189 190 191
        raise DistutilsFileError(
              "can't move '%s': destination '%s' not a valid path" %
              (src, dst))
192

193
    copy_it = False
194
    try:
Greg Ward's avatar
Greg Ward committed
195
        os.rename(src, dst)
196 197
    except os.error as e:
        (num, msg) = e
198
        if num == errno.EXDEV:
199
            copy_it = True
200
        else:
201 202
            raise DistutilsFileError(
                  "couldn't move '%s' to '%s': %s" % (src, dst, msg))
203 204

    if copy_it:
205
        copy_file(src, dst, verbose=verbose)
206
        try:
Greg Ward's avatar
Greg Ward committed
207
            os.unlink(src)
208 209
        except os.error as e:
            (num, msg) = e
210
            try:
Greg Ward's avatar
Greg Ward committed
211
                os.unlink(dst)
212 213
            except os.error:
                pass
214 215 216 217
            raise DistutilsFileError(
                  "couldn't move '%s' to '%s' by copy/delete: "
                  "delete '%s' failed: %s"
                  % (src, dst, src, msg))
218 219 220
    return dst


221
def write_file (filename, contents):
222
    """Create a file with the specified name and write 'contents' (a
Greg Ward's avatar
Greg Ward committed
223 224 225
    sequence of strings without line terminators) to it.
    """
    f = open(filename, "w")
226 227 228 229 230
    try:
        for line in contents:
            f.write(line + "\n")
    finally:
        f.close()