6 def new_link_path (link, dir, r):
10 while i < len(d) and i < len(l) and l[i] == '..':
15 return '/'.join ([x for x in l if not r.match (x)])
17 def hardlink_tree (input_roots = [],
26 """Mirror trees for different targets by hardlinking files.
29 input_roots=DIRLIST use DIRLIST as input tree roots list
30 process_dir=PATTERN only process files in directories named PATTERN
31 strip_dir_names=PATTERN strip directories names matching PATTERN
32 (write their content to parent)
33 exclude_dir=PATTERN don't recurse into directories named PATTERN
34 process_files=PATTERN filters files which are hardlinked
35 find_files=PATTERN find files named PATTERN. The files list will be returned.
36 exclude_files=PATTERN exclude files named PATTERN
37 target_pattern=STRING use STRING as target root directory name pattern
38 targets=DIRLIST mkdir each directory in DIRLIST and mirrors the tree into each
40 process_files_re = re.compile (process_files)
41 find_files_re = re.compile (find_files)
42 exclude_dirs_re = re.compile (exclude_dirs)
43 exclude_files_re = re.compile (exclude_files)
44 process_dirs_re = re.compile (process_dirs)
45 strip_dir_names_re = re.compile (strip_dir_names)
46 do_strip_dir_names_re = re.compile ('/(?:' + strip_dir_names + ')')
50 if not '%s' in target_pattern:
51 target_pattern += '%s'
52 target_dirs = [target_pattern % s for s in targets]
54 map (os.mkdir, target_dirs)
57 for in_dir, dirs, files in os.walk(d):
58 out_dir = strip_dir_names_re.sub ('', in_dir)
61 if exclude_dirs_re.search (dirs[i]):
64 if os.path.islink (os.path.join (in_dir, dirs[i])):
65 files.append (dirs[i])
67 if not strip_dir_names_re.match (os.path.basename (in_dir)):
69 p = os.path.join (t, out_dir)
70 if not os.path.isdir (p):
72 if not process_dirs_re.search (in_dir):
75 if exclude_files_re.match (f):
77 in_file = os.path.join (in_dir, f)
78 if find_files_re.match (f):
79 found_files.append (in_file)
80 if os.path.islink (in_file): # all symlinks are assumed to be relative and to point to files in the input trees
81 link_path = new_link_path (os.path.normpath (os.readlink (in_file)), in_dir, do_strip_dir_names_re)
83 os.symlink (link_path, os.path.join (t, out_dir, f))
84 elif process_files_re.match (f):
86 os.link (in_file, os.path.join (t, out_dir, f))