Correct the bug of the copy of svn files
[bertos.git] / wizard / copytree.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13 import fnmatch
14 from shutil import *
15
16 del copytree
17
18 def copytree(src, dst, symlinks=False, ignore_list=[]):
19     """
20     Reimplementation of the shutil.copytree function that use ignore_list.
21     ignore_list is a list containing patterns to ignore during the copy.
22     """
23     names = os.listdir(src)
24     os.makedirs(dst)
25     errors = []
26     for name in names:
27         srcname = os.path.join(src, name)
28         dstname = os.path.join(dst, name)
29         try:
30             ignored = False
31             for ignore in ignore_list:
32                 if fnmatch.fnmatch(name, ignore):
33                     ignored = True
34                     break
35             if ignored:
36                 continue
37             if symlinks and os.path.islink(srcname):
38                 linkto = os.readlink(srcname)
39                 os.symlink(linkto, dstname)
40             elif os.path.isdir(srcname):
41                 copytree(srcname, dstname, symlinks, ignore_list)
42             else:
43                 copy2(srcname, dstname)
44             # XXX What about devices, sockets etc.?
45         except (IOError, os.error), why:
46             errors.append((srcname, dstname, str(why)))
47         # catch the Error from the recursive copytree so that we can
48         # continue with other files
49         except Error, err:
50             errors.extend(err.args[0])
51     try:
52         copystat(src, dst)
53     except WindowsError:
54         # can't copy file access times on Windows
55         pass
56     except OSError, why:
57         errors.extend((src, dst, str(why)))
58     if errors:
59         raise Error, errors