|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import os, shutil |
|
18 |
|
19 def _filter_list(lst, filters): |
|
20 """ |
|
21 Filter a list in-place. |
|
22 |
|
23 @param lst: The list to filter. |
|
24 @param ignore_functions: List of functions used to check whether an entry |
|
25 should be filtered or not. Each function is given a list entry and |
|
26 should return True or False. |
|
27 """ |
|
28 del_entries = [] |
|
29 for entry in lst: |
|
30 for func in filters: |
|
31 if func(entry): |
|
32 del_entries.append(entry) |
|
33 break |
|
34 for entry in del_entries: |
|
35 lst.remove(entry) |
|
36 |
|
37 def copy_dir(source_dir, target_dir, dir_ignore_functions=[], file_ignore_functions=[]): |
|
38 """ |
|
39 Copy a directory tree with the possibility of ignoring certain files or directories. |
|
40 |
|
41 @param source_dir: The source directory to copy. |
|
42 @param target_dir: The target directory. If the directory already exists, already |
|
43 files will be overwritten. |
|
44 @param dir_ignore_functions: List of filter functions applied on dirs. |
|
45 @param file_ignore_functions: List of filter functions applied on files. |
|
46 """ |
|
47 if not os.path.exists(source_dir): |
|
48 raise RuntimeError("Source dir '%s' does not exist!!" % source_dir) |
|
49 |
|
50 source_parts_num = len(source_dir.replace('\\', '/').split('/')) |
|
51 target_parts = target_dir.replace('\\', '/').split('/') |
|
52 |
|
53 for root, dirs, files in os.walk(source_dir, topdown=True): |
|
54 _filter_list(dirs, dir_ignore_functions) |
|
55 _filter_list(files, file_ignore_functions) |
|
56 |
|
57 # Form the target root path |
|
58 current_dir_parts = root.replace('\\', '/').split('/') |
|
59 current_dir_parts = current_dir_parts[source_parts_num:] |
|
60 target_root_parts = [x for x in target_parts] |
|
61 target_root_parts.extend(current_dir_parts) |
|
62 |
|
63 # Create the target directory if necessary |
|
64 target_root = '/'.join(target_root_parts) |
|
65 if not os.path.exists(target_root): |
|
66 os.makedirs(target_root) |
|
67 elif not os.path.isdir(target_root): |
|
68 os.remove(target_root) |
|
69 os.makedirs(target_root) |
|
70 |
|
71 # Copy files |
|
72 for file in files: |
|
73 src_file = os.path.join(root, file) |
|
74 dst_file = os.path.join(target_root, file) |
|
75 |
|
76 # Remove if exists |
|
77 if os.path.exists(dst_file): |
|
78 if os.path.isdir(dst_file): shutil.rmtree(dst_file) |
|
79 else: os.remove(dst_file) |
|
80 |
|
81 shutil.copy2(src_file, dst_file) |