file_util.py 7.98 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 8 9 10

# created 2000/04/03, Greg Ward (extracted from util.py)

__revision__ = "$Id$"

import os
11
from stat import *
12 13 14 15 16 17 18 19 20 21 22
from distutils.errors import DistutilsFileError


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


def _copy_file_contents (src, dst, buffer_size=16*1024):
    """Copy the file 'src' to 'dst'; both must be filenames.  Any error
Greg Ward's avatar
Greg Ward committed
23 24 25 26 27
    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.
    """
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    # Stolen from shutil module in the standard library, but with
    # custom error-handling added.

    fsrc = None
    fdst = None
    try:
        try:
            fsrc = open(src, 'rb')
        except os.error, (errno, errstr):
            raise DistutilsFileError, \
                  "could not open '%s': %s" % (src, errstr)
        
        try:
            fdst = open(dst, 'wb')
        except os.error, (errno, errstr):
            raise DistutilsFileError, \
                  "could not create '%s': %s" % (dst, errstr)
        
        while 1:
            try:
Greg Ward's avatar
Greg Ward committed
48
                buf = fsrc.read(buffer_size)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
            except os.error, (errno, errstr):
                raise DistutilsFileError, \
                      "could not read from '%s': %s" % (src, errstr)
            
            if not buf:
                break

            try:
                fdst.write(buf)
            except os.error, (errno, errstr):
                raise DistutilsFileError, \
                      "could not write to '%s': %s" % (dst, errstr)
            
    finally:
        if fdst:
            fdst.close()
        if fsrc:
            fsrc.close()

# _copy_file_contents()


def copy_file (src, dst,
               preserve_mode=1,
               preserve_times=1,
               update=0,
               link=None,
               verbose=0,
               dry_run=0):

Greg Ward's avatar
Greg Ward committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    """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'.  If 'verbose' is true, then a one-line summary of the
    copy will be printed to stdout.

    '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.

99 100 101
    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
102
    """
103 104 105 106 107 108 109 110 111
    # 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

Greg Ward's avatar
Greg Ward committed
112
    if not os.path.isfile(src):
113 114 115
        raise DistutilsFileError, \
              "can't copy '%s': doesn't exist or not a regular file" % src

Greg Ward's avatar
Greg Ward committed
116
    if os.path.isdir(dst):
117
        dir = dst
Greg Ward's avatar
Greg Ward committed
118
        dst = os.path.join(dst, os.path.basename(src))
119
    else:
Greg Ward's avatar
Greg Ward committed
120
        dir = os.path.dirname(dst)
121

Greg Ward's avatar
Greg Ward committed
122
    if update and not newer(src, dst):
123 124
        if verbose:
            print "not copying %s (output up-to-date)" % src
125
        return (dst, 0)
126 127 128 129 130 131 132

    try:
        action = _copy_action[link]
    except KeyError:
        raise ValueError, \
              "invalid value '%s' for 'link' argument" % link
    if verbose:
133 134 135 136 137
        if os.path.basename(dst) == os.path.basename(src):
            print "%s %s -> %s" % (action, src, dir)
        else:
            print "%s %s -> %s" % (action, src, dst)
            
138
    if dry_run:
139
        return (dst, 1)
140

141
    # On Mac OS, use the native file copy routine
142 143 144
    if os.name == 'mac':
        import macostools
        try:
Greg Ward's avatar
Greg Ward committed
145
            macostools.copy(src, dst, 0, preserve_times)
146
        except os.error, exc:
147 148 149 150 151 152
            raise DistutilsFileError, \
                  "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])
    
    # 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
153 154
        if not (os.path.exists(dst) and os.path.samefile(src, dst)):
            os.link(src, dst)
155
    elif link == 'sym':
Greg Ward's avatar
Greg Ward committed
156 157
        if not (os.path.exists(dst) and os.path.samefile(src, dst)):
            os.symlink(src, dst)
158 159 160 161

    # Otherwise (non-Mac, not linking), copy the file contents and
    # (optionally) copy the times and mode.
    else:
Greg Ward's avatar
Greg Ward committed
162
        _copy_file_contents(src, dst)
163
        if preserve_mode or preserve_times:
Greg Ward's avatar
Greg Ward committed
164
            st = os.stat(src)
165 166 167 168

            # 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
169
                os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
170
            if preserve_mode:
Greg Ward's avatar
Greg Ward committed
171
                os.chmod(dst, S_IMODE(st[ST_MODE]))
172

173
    return (dst, 1)
174 175 176 177 178 179 180 181 182

# copy_file ()


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

Greg Ward's avatar
Greg Ward committed
183 184 185
    """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.
186

Greg Ward's avatar
Greg Ward committed
187 188 189
    Handles cross-device moves on Unix using 'copy_file()'.  What about
    other systems???
    """
190 191 192 193 194 195 196 197
    from os.path import exists, isfile, isdir, basename, dirname

    if verbose:
        print "moving %s -> %s" % (src, dst)

    if dry_run:
        return dst

Greg Ward's avatar
Greg Ward committed
198
    if not isfile(src):
199 200 201
        raise DistutilsFileError, \
              "can't move '%s': not a regular file" % src

Greg Ward's avatar
Greg Ward committed
202 203 204
    if isdir(dst):
        dst = os.path.join(dst, basename(src))
    elif exists(dst):
205 206 207 208
        raise DistutilsFileError, \
              "can't move '%s': destination '%s' already exists" % \
              (src, dst)

Greg Ward's avatar
Greg Ward committed
209
    if not isdir(dirname(dst)):
210 211 212 213 214 215
        raise DistutilsFileError, \
              "can't move '%s': destination '%s' not a valid path" % \
              (src, dst)

    copy_it = 0
    try:
Greg Ward's avatar
Greg Ward committed
216
        os.rename(src, dst)
217 218 219 220 221 222 223 224
    except os.error, (num, msg):
        if num == errno.EXDEV:
            copy_it = 1
        else:
            raise DistutilsFileError, \
                  "couldn't move '%s' to '%s': %s" % (src, dst, msg)

    if copy_it:
Greg Ward's avatar
Greg Ward committed
225
        copy_file(src, dst)
226
        try:
Greg Ward's avatar
Greg Ward committed
227
            os.unlink(src)
228 229
        except os.error, (num, msg):
            try:
Greg Ward's avatar
Greg Ward committed
230
                os.unlink(dst)
231 232 233 234 235 236 237 238 239 240 241 242 243 244
            except os.error:
                pass
            raise DistutilsFileError, \
                  ("couldn't move '%s' to '%s' by copy/delete: " + 
                   "delete '%s' failed: %s") % \
                  (src, dst, src, msg)

    return dst

# move_file ()


def write_file (filename, contents):
    """Create a file with the specified name and write 'contents' (a
Greg Ward's avatar
Greg Ward committed
245 246 247
    sequence of strings without line terminators) to it.
    """
    f = open(filename, "w")
248
    for line in contents:
Greg Ward's avatar
Greg Ward committed
249 250
        f.write(line + "\n")
    f.close()