PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/lib/python/indra/util/shutil2.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 84 lines | 75 code | 0 blank | 9 comment | 0 complexity | 5152779fa677d378a1a614567fce5a3a MD5 | raw file
 1'''
 2@file shutil2.py
 3@brief a better shutil.copytree replacement
 4
 5$LicenseInfo:firstyear=2007&license=mit$
 6
 7Copyright (c) 2007-2009, Linden Research, Inc.
 8
 9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26$/LicenseInfo$
27'''
28
29#
30# shutil2.py
31# Taken from http://www.scons.org/wiki/AccumulateBuilder
32# the stock copytree sucks because it insists that the
33# target dir not exist
34#
35
36import os.path
37import shutil
38
39def copytree(src, dest, symlinks=False):
40    """My own copyTree which does not fail if the directory exists.
41    
42    Recursively copy a directory tree using copy2().
43
44    If the optional symlinks flag is true, symbolic links in the
45    source tree result in symbolic links in the destination tree; if
46    it is false, the contents of the files pointed to by symbolic
47    links are copied.
48    
49    Behavior is meant to be identical to GNU 'cp -R'.    
50    """
51    def copyItems(src, dest, symlinks=False):
52        """Function that does all the work.
53        
54        It is necessary to handle the two 'cp' cases:
55        - destination does exist
56        - destination does not exist
57        
58        See 'cp -R' documentation for more details
59        """
60        for item in os.listdir(src):
61           srcPath = os.path.join(src, item)
62           if os.path.isdir(srcPath):
63               srcBasename = os.path.basename(srcPath)
64               destDirPath = os.path.join(dest, srcBasename)
65               if not os.path.exists(destDirPath):
66                   os.makedirs(destDirPath)
67               copyItems(srcPath, destDirPath)
68           elif os.path.islink(item) and symlinks:
69               linkto = os.readlink(item)
70               os.symlink(linkto, dest)
71           else:
72               shutil.copy2(srcPath, dest)
73
74    # case 'cp -R src/ dest/' where dest/ already exists
75    if os.path.exists(dest):
76       destPath = os.path.join(dest, os.path.basename(src))
77       if not os.path.exists(destPath):
78           os.makedirs(destPath)
79    # case 'cp -R src/ dest/' where dest/ does not exist
80    else:
81       os.makedirs(dest)
82       destPath = dest
83    # actually copy the files
84    copyItems(src, destPath)