Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/distutils/file_util.py

    r2 r391  
    44"""
    55
    6 # This module should be kept compatible with Python 2.1.
    7 
    8 __revision__ = "$Id: file_util.py 37828 2004-11-10 22:23:15Z loewis $"
     6__revision__ = "$Id$"
    97
    108import os
     
    1311
    1412# for generating verbose output in 'copy_file()'
    15 _copy_action = { None:   'copying',
    16                  'hard': 'hard linking',
    17                  'sym':  'symbolically linking' }
    18 
    19 
    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
    25     regular files.
     13_copy_action = {None: 'copying',
     14                'hard': 'hard linking',
     15                'sym': 'symbolically linking'}
     16
     17
     18def _copy_file_contents(src, dst, buffer_size=16*1024):
     19    """Copy the file 'src' to 'dst'.
     20
     21    Both must be filenames. Any error opening either file, reading from
     22    'src', or writing to 'dst', raises DistutilsFileError.  Data is
     23    read/written in chunks of 'buffer_size' bytes (default 16k).  No attempt
     24    is made to handle anything apart from regular files.
    2625    """
    2726    # Stolen from shutil module in the standard library, but with
    2827    # custom error-handling added.
    29 
    3028    fsrc = None
    3129    fdst = None
     
    3432            fsrc = open(src, 'rb')
    3533        except os.error, (errno, errstr):
    36             raise DistutilsFileError, \
    37                   "could not open '%s': %s" % (src, errstr)
     34            raise DistutilsFileError("could not open '%s': %s" % (src, errstr))
    3835
    3936        if os.path.exists(dst):
     
    4138                os.unlink(dst)
    4239            except os.error, (errno, errstr):
    43                 raise DistutilsFileError, \
    44                       "could not delete '%s': %s" % (dst, errstr)
     40                raise DistutilsFileError(
     41                      "could not delete '%s': %s" % (dst, errstr))
    4542
    4643        try:
    4744            fdst = open(dst, 'wb')
    4845        except os.error, (errno, errstr):
    49             raise DistutilsFileError, \
    50                   "could not create '%s': %s" % (dst, errstr)
     46            raise DistutilsFileError(
     47                  "could not create '%s': %s" % (dst, errstr))
    5148
    5249        while 1:
     
    5451                buf = fsrc.read(buffer_size)
    5552            except os.error, (errno, errstr):
    56                 raise DistutilsFileError, \
    57                       "could not read from '%s': %s" % (src, errstr)
     53                raise DistutilsFileError(
     54                      "could not read from '%s': %s" % (src, errstr))
    5855
    5956            if not buf:
     
    6360                fdst.write(buf)
    6461            except os.error, (errno, errstr):
    65                 raise DistutilsFileError, \
    66                       "could not write to '%s': %s" % (dst, errstr)
     62                raise DistutilsFileError(
     63                      "could not write to '%s': %s" % (dst, errstr))
    6764
    6865    finally:
     
    7269            fsrc.close()
    7370
    74 # _copy_file_contents()
    75 
    76 def 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 
    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
    92     older than 'src'.
     71def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
     72              link=None, verbose=1, dry_run=0):
     73    """Copy a file 'src' to 'dst'.
     74
     75    If 'dst' is a directory, then 'src' is copied there with the same name;
     76    otherwise, it must be a filename.  (If the file exists, it will be
     77    ruthlessly clobbered.)  If 'preserve_mode' is true (the default),
     78    the file's mode (type and permission bits, or whatever is analogous on
     79    the current platform) is copied.  If 'preserve_times' is true (the
     80    default), the last-modified and last-access times are copied as well.
     81    If 'update' is true, 'src' will only be copied if 'dst' does not exist,
     82    or if 'dst' does exist but is older than 'src'.
    9383
    9484    'link' allows you to make hard links (os.link) or symbolic links
     
    116106
    117107    if not os.path.isfile(src):
    118         raise DistutilsFileError, \
    119               "can't copy '%s': doesn't exist or not a regular file" % src
     108        raise DistutilsFileError(
     109              "can't copy '%s': doesn't exist or not a regular file" % src)
    120110
    121111    if os.path.isdir(dst):
     
    126116
    127117    if update and not newer(src, dst):
    128         log.debug("not copying %s (output up-to-date)", src)
     118        if verbose >= 1:
     119            log.debug("not copying %s (output up-to-date)", src)
    129120        return dst, 0
    130121
     
    132123        action = _copy_action[link]
    133124    except KeyError:
    134         raise ValueError, \
    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)
    138     else:
    139         log.info("%s %s -> %s", action, src, dst)
     125        raise ValueError("invalid value '%s' for 'link' argument" % link)
     126
     127    if verbose >= 1:
     128        if os.path.basename(dst) == os.path.basename(src):
     129            log.info("%s %s -> %s", action, src, dir)
     130        else:
     131            log.info("%s %s -> %s", action, src, dst)
    140132
    141133    if dry_run:
    142134        return (dst, 1)
    143135
    144     # On Mac OS, use the native file copy routine
    145     if os.name == 'mac':
    146         import macostools
    147         try:
    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])
    152 
    153136    # If linking (hard or symbolic), use the appropriate system call
    154137    # (Unix only, of course, but that's the caller's responsibility)
    155     elif link == 'hard':
     138    if link == 'hard':
    156139        if not (os.path.exists(dst) and os.path.samefile(src, dst)):
    157140            os.link(src, dst)
     
    176159    return (dst, 1)
    177160
    178 # copy_file ()
    179 
    180 
    181161# XXX I suspect this is Unix-specific -- need porting help!
    182 def move_file (src, dst,
    183                verbose=0,
    184                dry_run=0):
    185 
    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.
     162def move_file (src, dst, verbose=1, dry_run=0):
     163    """Move a file 'src' to 'dst'.
     164
     165    If 'dst' is a directory, the file will be moved into it with the same
     166    name; otherwise, 'src' is just renamed to 'dst'.  Return the new
     167    full name of the file.
    189168
    190169    Handles cross-device moves on Unix using 'copy_file()'.  What about
     
    194173    import errno
    195174
    196     log.info("moving %s -> %s", src, dst)
     175    if verbose >= 1:
     176        log.info("moving %s -> %s", src, dst)
    197177
    198178    if dry_run:
     
    200180
    201181    if not isfile(src):
    202         raise DistutilsFileError, \
    203               "can't move '%s': not a regular file" % src
     182        raise DistutilsFileError("can't move '%s': not a regular file" % src)
    204183
    205184    if isdir(dst):
    206185        dst = os.path.join(dst, basename(src))
    207186    elif exists(dst):
    208         raise DistutilsFileError, \
    209               "can't move '%s': destination '%s' already exists" % \
    210               (src, dst)
     187        raise DistutilsFileError(
     188              "can't move '%s': destination '%s' already exists" %
     189              (src, dst))
    211190
    212191    if not isdir(dirname(dst)):
    213         raise DistutilsFileError, \
     192        raise DistutilsFileError(
    214193              "can't move '%s': destination '%s' not a valid path" % \
    215               (src, dst)
     194              (src, dst))
    216195
    217196    copy_it = 0
     
    222201            copy_it = 1
    223202        else:
    224             raise DistutilsFileError, \
    225                   "couldn't move '%s' to '%s': %s" % (src, dst, msg)
     203            raise DistutilsFileError(
     204                  "couldn't move '%s' to '%s': %s" % (src, dst, msg))
    226205
    227206    if copy_it:
    228         copy_file(src, dst)
     207        copy_file(src, dst, verbose=verbose)
    229208        try:
    230209            os.unlink(src)
     
    234213            except os.error:
    235214                pass
    236             raise DistutilsFileError, \
     215            raise DistutilsFileError(
    237216                  ("couldn't move '%s' to '%s' by copy/delete: " +
    238                    "delete '%s' failed: %s") % \
    239                   (src, dst, src, msg)
    240 
     217                   "delete '%s' failed: %s") %
     218                  (src, dst, src, msg))
    241219    return dst
    242 
    243 # move_file ()
    244220
    245221
     
    249225    """
    250226    f = open(filename, "w")
    251     for line in contents:
    252         f.write(line + "\n")
    253     f.close()
     227    try:
     228        for line in contents:
     229            f.write(line + "\n")
     230    finally:
     231        f.close()
Note: See TracChangeset for help on using the changeset viewer.