00001 '''
00002 @file shutil2.py
00003 @brief a better shutil.copytree replacement
00004
00005 $LicenseInfo:firstyear=2007&license=mit$
00006
00007 Copyright (c) 2007-2008, Linden Research, Inc.
00008
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 $/LicenseInfo$
00027 '''
00028
00029
00030
00031
00032
00033
00034
00035
00036 import os.path
00037 import shutil
00038
00039 def copytree(src, dest, symlinks=False):
00040 """My own copyTree which does not fail if the directory exists.
00041
00042 Recursively copy a directory tree using copy2().
00043
00044 If the optional symlinks flag is true, symbolic links in the
00045 source tree result in symbolic links in the destination tree; if
00046 it is false, the contents of the files pointed to by symbolic
00047 links are copied.
00048
00049 Behavior is meant to be identical to GNU 'cp -R'.
00050 """
00051 def copyItems(src, dest, symlinks=False):
00052 """Function that does all the work.
00053
00054 It is necessary to handle the two 'cp' cases:
00055 - destination does exist
00056 - destination does not exist
00057
00058 See 'cp -R' documentation for more details
00059 """
00060 for item in os.listdir(src):
00061 srcPath = os.path.join(src, item)
00062 if os.path.isdir(srcPath):
00063 srcBasename = os.path.basename(srcPath)
00064 destDirPath = os.path.join(dest, srcBasename)
00065 if not os.path.exists(destDirPath):
00066 os.makedirs(destDirPath)
00067 copyItems(srcPath, destDirPath)
00068 elif os.path.islink(item) and symlinks:
00069 linkto = os.readlink(item)
00070 os.symlink(linkto, dest)
00071 else:
00072 shutil.copy2(srcPath, dest)
00073
00074
00075 if os.path.exists(dest):
00076 destPath = os.path.join(dest, os.path.basename(src))
00077 if not os.path.exists(destPath):
00078 os.makedirs(destPath)
00079
00080 else:
00081 os.makedirs(dest)
00082 destPath = dest
00083
00084 copyItems(src, destPath)