29
28
"""The copy-base walker traverses a walklist ruthlessly mirroring src to dst.
30
29
It is designed to be the base class of both the copy-in and copy-out walker,
31
30
both of which are specialisations of this purpose. See them for more
31
information. The print_op method, derived in those classes, takes, in
32
addition to the relative filename, a source file type, an operation, and a
33
sestination file type. Valid file types are f (file), l (symlink), d
34
(directory) and _ (non-existant). Valid operations are * (modify), = (skip:
35
same), @ (skip: symlink substitute) and # (skip: ignored).
35
def __init__( self, updated_files = None ):
36
self.accept_list = FileMatcher()
40
self.check_src_symlinks = False
41
self.check_dst_symlinks = False
42
self.check_dst_ignores = False
39
45
def process( self, rel_file, src, dst ):
48
if self.check_dst_ignores and the.config.ignores.matches( rel_file ):
49
self.print_op( rel_file, src.type, '#', dst.type )
41
52
# src entity is directory
42
53
if src.type == 'd':
44
55
# if dst entity doesn't exist, create directory (no need to recurse,
45
56
# since we're copying the whole directory)
46
57
if dst.type == '_':
47
if the.verbose > 1: self.print_cp( rel_file, 'd', '_' )
49
shutil.copystat( src.file, dst.file )
58
self.print_op( rel_file, 'd', '*', '_' )
59
shutil.copytree( src.file, dst.file, True )
52
61
# if dst entity is a directory, copy permissions, as required (and
54
63
elif dst.type == 'd':
55
64
# TODO: should check permission and only do as necessary
56
if the.verbose > 1: self.print_cp( rel_file, 'd', 'd' )
65
self.print_op( rel_file, 'd', '*', 'd' )
57
66
shutil.copystat( src.file, dst.file )
60
# TODO: if dst entity is a symlink to a directory, and rel_file is
61
# matched by the acceptable symlinks file matcher, just recurse
62
# elif dst.link_type == 'd' and self.accept_list.match( rel_file ):
63
# if the.verbose > 1: self.print_cp( rel_file, 'd', 'd', '@' )
69
# if dst entity is a symlink to a directory, and this is an
70
# acceptable substitute, just recurse
71
elif self.check_dst_symlinks and dst.link_type == 'd' and \
72
the.config.symlinks.matches( rel_file ):
73
self.print_op( rel_file, 'd', '@', 'd' )
66
76
# if dst entity is a file or symlink in home dir, replace it with
67
77
# directory (no need to recurse, since we're copying the whole
69
79
elif dst.type == 'f' or dst.type == 'l':
70
if the.verbose > 1: self.print_cp( rel_file, 'd', dst.type )
80
self.print_op( rel_file, 'd', '*', dst.type )
71
81
os.unlink( dst.file )
73
shutil.copystat( src.file, dst.file )
82
shutil.copytree( src.file, dst.file, True )
77
85
raise NotImplementedError()
79
87
# src entity is file
82
90
# if dst entity doesn't exist, copy file
83
91
if dst.type == '_':
84
if the.verbose > 1: self.print_cp( rel_file, 'f', '_' )
92
self.print_op( rel_file, 'f', '*', '_' )
85
93
shutil.copy( src.file, dst.file )
86
94
shutil.copystat( src.file, dst.file )
88
96
# if dst entity is a file, replace it only if it differs
89
97
elif dst.type == 'f':
90
98
if not filecmp.cmp( src.file, dst.file ):
91
if the.verbose > 1: self.print_cp( rel_file, 'f', 'f' )
99
self.print_op( rel_file, 'f', '*', 'f' )
92
100
os.unlink( dst.file )
93
101
shutil.copy( src.file, dst.file )
94
102
shutil.copystat( src.file, dst.file )
96
if the.verbose > 1: self.print_cp( rel_file, 'f', 'f', '=' )
104
self.print_op( rel_file, 'f', '=', 'f' )
98
106
# if dst entity is a directory, replace it with file
99
107
elif dst.type == 'd':
100
if the.verbose > 1: self.print_cp( rel_file, 'f', 'd' )
108
self.print_op( rel_file, 'f', '*', 'd' )
101
109
shutil.rmtree( dst.file )
102
110
shutil.copy( src.file, dst.file )
103
111
shutil.copystat( src.file, dst.file )
105
113
# if dst entity is a symlink, replace it with file
106
114
elif dst.type == 'l':
107
if the.verbose > 1: self.print_cp( rel_file, 'f', 'l' )
115
self.print_op( rel_file, 'f', '*', 'l' )
108
116
os.unlink( dst.file )
109
117
shutil.copy( src.file, dst.file )
110
118
shutil.copystat( src.file, dst.file )
113
121
raise NotImplementedError()
115
123
# src entity is a symlink
124
elif src.type == 'l':
118
126
# if dst entity doesn't exist, copy symlink
119
127
if dst.type == '_':
120
if the.verbose > 1: self.print_cp( rel_file, 'l', '_' )
128
self.print_op( rel_file, 'l', '*', '_' )
121
129
os.symlink( os.readlink( src.file ), dst.file )
123
131
# if dst entity is a symlink, replace it only if it differs
124
132
elif dst.type == 'l':
125
133
if os.readlink( src.file ) != os.readlink( dst.file ):
126
if the.verbose > 1: self.print_cp( rel_file, 'l', 'l' )
134
self.print_op( rel_file, 'l', '*', 'l' )
127
135
os.unlink( dst.file )
128
136
os.symlink( os.readlink( src.file ), dst.file )
130
if the.verbose > 1: self.print_cp( rel_file, 'l', 'l', '=' )
138
self.print_op( rel_file, 'l', '=', 'l' )
132
140
# if dst entity is a file, replace it with symlink
133
141
elif dst.type == 'f':
134
if the.verbose > 1: self.print_cp( rel_file, 'l', 'f' )
142
self.print_op( rel_file, 'l', '*', 'f' )
135
143
os.unlink( dst.file )
136
144
os.symlink( os.readlink( src.file ), dst.file )
138
# if dst entity is a directory, replace it with symlink
146
# if dst entity is a directory...
139
147
elif dst.type == 'd':
140
if the.verbose > 1: self.print_cp( rel_file, 'l', 'd' )
149
# if src entity is a symlink to a directory, and this is an
150
# acceptable substitute, just recurse
151
if self.check_src_symlinks and src.link_type == 'd' and \
152
the.config.symlinks.matches( rel_file ):
153
self.print_op( rel_file, 'd', '@', 'd' )
156
# else replace it with a symlink
157
self.print_op( rel_file, 'l', '*', 'd' )
141
158
shutil.rmtree( dst.file )
142
159
os.symlink( os.readlink( src.file ), dst.file )
145
162
raise NotImplementedError()
147
164
# src entity is missing
165
elif src.type == '_':
150
167
# if dst entity doesn't exist, we're good
151
168
if dst.type == '_':
154
171
# if dst entity is a file or symlink, delete it
155
if dst.type == 'f' or dst.type == 'l':
156
if the.verbose > 1: self.print_cp( rel_file, '_', dst.type )
172
elif dst.type == 'f' or dst.type == 'l':
173
self.print_op( rel_file, '_', '*', dst.type )
157
174
os.unlink( dst.file )
159
176
# if dst entity is a directory, delete it
160
177
elif dst.type == 'd':
161
if the.verbose > 1: self.print_cp( rel_file, '_', 'd' )
178
self.print_op( rel_file, '_', '*', 'd' )
162
179
shutil.rmtree( dst.file )
165
182
raise NotImplementedError()
167
# non-directories can not be recursed in to
184
# if we got here, we don't want to recurse...