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/dir_util.py

    r2 r391  
    33Utility functions for manipulating directories and directory trees."""
    44
    5 # This module should be kept compatible with Python 2.1.
    6 
    7 __revision__ = "$Id: dir_util.py 60923 2008-02-21 18:18:37Z guido.van.rossum $"
    8 
    9 import os, sys
    10 from types import *
     5__revision__ = "$Id$"
     6
     7import os
     8import errno
    119from distutils.errors import DistutilsFileError, DistutilsInternalError
    1210from distutils import log
     
    1917# b) it blows up if the directory already exists (I want to silently
    2018# succeed in that case).
    21 def mkpath (name, mode=0777, verbose=0, dry_run=0):
    22     """Create a directory and any missing ancestor directories.  If the
    23        directory already exists (or if 'name' is the empty string, which
    24        means the current directory, which of course exists), then do
    25        nothing.  Raise DistutilsFileError if unable to create some
    26        directory along the way (eg. some sub-path exists, but is a file
    27        rather than a directory).  If 'verbose' is true, print a one-line
    28        summary of each mkdir to stdout.  Return the list of directories
    29        actually created."""
     19def mkpath(name, mode=0777, verbose=1, dry_run=0):
     20    """Create a directory and any missing ancestor directories.
     21
     22    If the directory already exists (or if 'name' is the empty string, which
     23    means the current directory, which of course exists), then do nothing.
     24    Raise DistutilsFileError if unable to create some directory along the way
     25    (eg. some sub-path exists, but is a file rather than a directory).
     26    If 'verbose' is true, print a one-line summary of each mkdir to stdout.
     27    Return the list of directories actually created.
     28    """
    3029
    3130    global _path_created
    3231
    3332    # Detect a common bug -- name is None
    34     if not isinstance(name, StringTypes):
     33    if not isinstance(name, basestring):
    3534        raise DistutilsInternalError, \
    3635              "mkpath: 'name' must be a string (got %r)" % (name,)
     
    5251
    5352    while head and tail and not os.path.isdir(head):
    54         #print "splitting '%s': " % head,
    5553        (head, tail) = os.path.split(head)
    56         #print "to ('%s','%s')" % (head, tail)
    5754        tails.insert(0, tail)          # push next higher dir onto stack
    58 
    59     #print "stack of tails:", tails
    6055
    6156    # now 'head' contains the deepest directory that already exists
     
    7065            continue
    7166
    72         log.info("creating %s", head)
     67        if verbose >= 1:
     68            log.info("creating %s", head)
    7369
    7470        if not dry_run:
    7571            try:
    76                 os.mkdir(head)
    77                 created_dirs.append(head)
     72                os.mkdir(head, mode)
    7873            except OSError, exc:
    79                 raise DistutilsFileError, \
    80                       "could not create '%s': %s" % (head, exc[-1])
     74                if not (exc.errno == errno.EEXIST and os.path.isdir(head)):
     75                    raise DistutilsFileError(
     76                          "could not create '%s': %s" % (head, exc.args[-1]))
     77            created_dirs.append(head)
    8178
    8279        _path_created[abs_head] = 1
    8380    return created_dirs
    8481
    85 # mkpath ()
    86 
    87 
    88 def create_tree (base_dir, files, mode=0777, verbose=0, dry_run=0):
    89 
    90     """Create all the empty directories under 'base_dir' needed to
    91        put 'files' there.  'base_dir' is just the a name of a directory
    92        which doesn't necessarily exist yet; 'files' is a list of filenames
    93        to be interpreted relative to 'base_dir'.  'base_dir' + the
    94        directory portion of every file in 'files' will be created if it
    95        doesn't already exist.  'mode', 'verbose' and 'dry_run' flags are as
    96        for 'mkpath()'."""
    97 
     82def create_tree(base_dir, files, mode=0777, verbose=1, dry_run=0):
     83    """Create all the empty directories under 'base_dir' needed to put 'files'
     84    there.
     85
     86    'base_dir' is just the a name of a directory which doesn't necessarily
     87    exist yet; 'files' is a list of filenames to be interpreted relative to
     88    'base_dir'.  'base_dir' + the directory portion of every file in 'files'
     89    will be created if it doesn't already exist.  'mode', 'verbose' and
     90    'dry_run' flags are as for 'mkpath()'.
     91    """
    9892    # First get the list of directories to create
    9993    need_dir = {}
     
    10599    # Now create them
    106100    for dir in need_dirs:
    107         mkpath(dir, mode, dry_run=dry_run)
    108 
    109 # create_tree ()
    110 
    111 
    112 def copy_tree (src, dst,
    113                preserve_mode=1,
    114                preserve_times=1,
    115                preserve_symlinks=0,
    116                update=0,
    117                verbose=0,
    118                dry_run=0):
    119 
    120     """Copy an entire directory tree 'src' to a new location 'dst'.  Both
    121        'src' and 'dst' must be directory names.  If 'src' is not a
    122        directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    123        created with 'mkpath()'.  The end result of the copy is that every
    124        file in 'src' is copied to 'dst', and directories under 'src' are
    125        recursively copied to 'dst'.  Return the list of files that were
    126        copied or might have been copied, using their output name.  The
    127        return value is unaffected by 'update' or 'dry_run': it is simply
    128        the list of all files under 'src', with the names changed to be
    129        under 'dst'.
    130 
    131        'preserve_mode' and 'preserve_times' are the same as for
    132        'copy_file'; note that they only apply to regular files, not to
    133        directories.  If 'preserve_symlinks' is true, symlinks will be
    134        copied as symlinks (on platforms that support them!); otherwise
    135        (the default), the destination of the symlink will be copied.
    136        'update' and 'verbose' are the same as for 'copy_file'."""
    137 
     101        mkpath(dir, mode, verbose=verbose, dry_run=dry_run)
     102
     103def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
     104              preserve_symlinks=0, update=0, verbose=1, dry_run=0):
     105    """Copy an entire directory tree 'src' to a new location 'dst'.
     106
     107    Both 'src' and 'dst' must be directory names.  If 'src' is not a
     108    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
     109    created with 'mkpath()'.  The end result of the copy is that every
     110    file in 'src' is copied to 'dst', and directories under 'src' are
     111    recursively copied to 'dst'.  Return the list of files that were
     112    copied or might have been copied, using their output name.  The
     113    return value is unaffected by 'update' or 'dry_run': it is simply
     114    the list of all files under 'src', with the names changed to be
     115    under 'dst'.
     116
     117    'preserve_mode' and 'preserve_times' are the same as for
     118    'copy_file'; note that they only apply to regular files, not to
     119    directories.  If 'preserve_symlinks' is true, symlinks will be
     120    copied as symlinks (on platforms that support them!); otherwise
     121    (the default), the destination of the symlink will be copied.
     122    'update' and 'verbose' are the same as for 'copy_file'.
     123    """
    138124    from distutils.file_util import copy_file
    139125
     
    151137
    152138    if not dry_run:
    153         mkpath(dst)
     139        mkpath(dst, verbose=verbose)
    154140
    155141    outputs = []
     
    159145        dst_name = os.path.join(dst, n)
    160146
     147        if n.startswith('.nfs'):
     148            # skip NFS rename files
     149            continue
     150
    161151        if preserve_symlinks and os.path.islink(src_name):
    162152            link_dest = os.readlink(src_name)
    163             log.info("linking %s -> %s", dst_name, link_dest)
     153            if verbose >= 1:
     154                log.info("linking %s -> %s", dst_name, link_dest)
    164155            if not dry_run:
    165156                os.symlink(link_dest, dst_name)
     
    170161                copy_tree(src_name, dst_name, preserve_mode,
    171162                          preserve_times, preserve_symlinks, update,
    172                           dry_run=dry_run))
     163                          verbose=verbose, dry_run=dry_run))
    173164        else:
    174165            copy_file(src_name, dst_name, preserve_mode,
    175                       preserve_times, update, dry_run=dry_run)
     166                      preserve_times, update, verbose=verbose,
     167                      dry_run=dry_run)
    176168            outputs.append(dst_name)
    177169
    178170    return outputs
    179171
    180 # copy_tree ()
    181 
    182 # Helper for remove_tree()
    183172def _build_cmdtuple(path, cmdtuples):
     173    """Helper for remove_tree()."""
    184174    for f in os.listdir(path):
    185175        real_f = os.path.join(path,f)
     
    190180    cmdtuples.append((os.rmdir, path))
    191181
    192 
    193 def remove_tree (directory, verbose=0, dry_run=0):
    194     """Recursively remove an entire directory tree.  Any errors are ignored
    195     (apart from being reported to stdout if 'verbose' is true).
     182def remove_tree(directory, verbose=1, dry_run=0):
     183    """Recursively remove an entire directory tree.
     184
     185    Any errors are ignored (apart from being reported to stdout if 'verbose'
     186    is true).
    196187    """
    197188    from distutils.util import grok_environment_error
    198189    global _path_created
    199190
    200     log.info("removing '%s' (and everything under it)", directory)
     191    if verbose >= 1:
     192        log.info("removing '%s' (and everything under it)", directory)
    201193    if dry_run:
    202194        return
     
    205197    for cmd in cmdtuples:
    206198        try:
    207             apply(cmd[0], (cmd[1],))
     199            cmd[0](cmd[1])
    208200            # remove dir from cache if it's already there
    209201            abspath = os.path.abspath(cmd[1])
     
    214206                    exc, "error removing %s: " % directory))
    215207
    216 
    217 def ensure_relative (path):
    218     """Take the full path 'path', and make it a relative path so
    219     it can be the second argument to os.path.join().
     208def ensure_relative(path):
     209    """Take the full path 'path', and make it a relative path.
     210
     211    This is useful to make 'path' the second argument to os.path.join().
    220212    """
    221213    drive, path = os.path.splitdrive(path)
    222     if sys.platform == 'mac':
    223         return os.sep + path
    224     else:
    225         if path[0:1] == os.sep:
    226             path = drive + path[1:]
    227         return path
     214    if path[0:1] == os.sep:
     215        path = drive + path[1:]
     216    return path
Note: See TracChangeset for help on using the changeset viewer.