28
28
"""The copy-base walker traverses a walklist ruthlessly mirroring src to dst.
29
29
It is designed to be the base class of both the copy-in and copy-out walker,
30
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).
48
44
if self.check_dst_ignores and the.config.ignores.matches( rel_file ):
49
self.print_op( rel_file, src.type, '#', dst.type )
46
self.print_op( rel_file, '%s#%s' % ( src.type, dst.type ) )
52
49
# src entity is directory
55
52
# if dst entity doesn't exist, create directory (no need to recurse,
56
53
# since we're copying the whole directory)
57
54
if dst.type == '_':
58
self.print_op( rel_file, 'd', '*', '_' )
55
if the.verbose >= 2: self.print_op( rel_file, 'd>_' )
59
56
shutil.copytree( src.file, dst.file, True )
61
58
# if dst entity is a directory, copy permissions, as required (and
63
60
elif dst.type == 'd':
64
61
# TODO: should check permission and only do as necessary
65
self.print_op( rel_file, 'd', '*', 'd' )
62
if the.verbose >= 2: self.print_op( rel_file, 'd>d' )
66
63
shutil.copystat( src.file, dst.file )
70
67
# acceptable substitute, just recurse
71
68
elif self.check_dst_symlinks and dst.link_type == 'd' and \
72
69
the.config.symlinks.matches( rel_file ):
73
self.print_op( rel_file, 'd', '@', 'd' )
70
if the.verbose >= 2: self.print_op( rel_file, 'd@d' )
76
73
# if dst entity is a file or symlink in home dir, replace it with
77
74
# directory (no need to recurse, since we're copying the whole
79
76
elif dst.type == 'f' or dst.type == 'l':
80
self.print_op( rel_file, 'd', '*', dst.type )
77
if the.verbose >= 2: self.print_op( rel_file, 'd>' + dst.type )
81
78
os.unlink( dst.file )
82
79
shutil.copytree( src.file, dst.file, True )
90
87
# if dst entity doesn't exist, copy file
91
88
if dst.type == '_':
92
self.print_op( rel_file, 'f', '*', '_' )
89
if the.verbose >= 2: self.print_op( rel_file, 'f>_' )
93
90
shutil.copy( src.file, dst.file )
94
91
shutil.copystat( src.file, dst.file )
96
93
# if dst entity is a file, replace it only if it differs
97
94
elif dst.type == 'f':
98
95
if not filecmp.cmp( src.file, dst.file ):
99
self.print_op( rel_file, 'f', '*', 'f' )
96
if the.verbose >= 2: self.print_op( rel_file, 'f>f' )
100
97
os.unlink( dst.file )
101
98
shutil.copy( src.file, dst.file )
102
99
shutil.copystat( src.file, dst.file )
104
self.print_op( rel_file, 'f', '=', 'f' )
101
if the.verbose >= 2: self.print_op( rel_file, 'f=f' )
106
103
# if dst entity is a directory, replace it with file
107
104
elif dst.type == 'd':
108
self.print_op( rel_file, 'f', '*', 'd' )
105
if the.verbose >= 2: self.print_op( rel_file, 'f>d' )
109
106
shutil.rmtree( dst.file )
110
107
shutil.copy( src.file, dst.file )
111
108
shutil.copystat( src.file, dst.file )
113
110
# if dst entity is a symlink, replace it with file
114
111
elif dst.type == 'l':
115
self.print_op( rel_file, 'f', '*', 'l' )
112
if the.verbose >= 2: self.print_op( rel_file, 'f>l' )
116
113
os.unlink( dst.file )
117
114
shutil.copy( src.file, dst.file )
118
115
shutil.copystat( src.file, dst.file )
126
123
# if dst entity doesn't exist, copy symlink
127
124
if dst.type == '_':
128
self.print_op( rel_file, 'l', '*', '_' )
125
if the.verbose >= 2: self.print_op( rel_file, 'l>_' )
129
126
os.symlink( os.readlink( src.file ), dst.file )
131
128
# if dst entity is a symlink, replace it only if it differs
132
129
elif dst.type == 'l':
133
130
if os.readlink( src.file ) != os.readlink( dst.file ):
134
self.print_op( rel_file, 'l', '*', 'l' )
131
if the.verbose >= 2: self.print_op( rel_file, 'l>l' )
135
132
os.unlink( dst.file )
136
133
os.symlink( os.readlink( src.file ), dst.file )
138
self.print_op( rel_file, 'l', '=', 'l' )
135
if the.verbose >= 2: self.print_op( rel_file, 'l=l' )
140
137
# if dst entity is a file, replace it with symlink
141
138
elif dst.type == 'f':
142
self.print_op( rel_file, 'l', '*', 'f' )
139
if the.verbose >= 2: self.print_op( rel_file, 'l>f' )
143
140
os.unlink( dst.file )
144
141
os.symlink( os.readlink( src.file ), dst.file )
150
147
# acceptable substitute, just recurse
151
148
if self.check_src_symlinks and src.link_type == 'd' and \
152
149
the.config.symlinks.matches( rel_file ):
153
self.print_op( rel_file, 'd', '@', 'd' )
150
if the.verbose >= 2: self.print_op( rel_file, 'd@d' )
156
153
# else replace it with a symlink
157
self.print_op( rel_file, 'l', '*', 'd' )
154
if the.verbose >= 2: self.print_op( rel_file, 'l>d' )
158
155
shutil.rmtree( dst.file )
159
156
os.symlink( os.readlink( src.file ), dst.file )
171
168
# if dst entity is a file or symlink, delete it
172
169
elif dst.type == 'f' or dst.type == 'l':
173
self.print_op( rel_file, '_', '*', dst.type )
170
if the.verbose >= 2: self.print_op( rel_file, '_>' + dst.type )
174
171
os.unlink( dst.file )
176
173
# if dst entity is a directory, delete it
177
174
elif dst.type == 'd':
178
self.print_op( rel_file, '_', '*', 'd' )
175
if the.verbose >= 2: self.print_op( rel_file, '_>d' )
179
176
shutil.rmtree( dst.file )