bzr branch
http://bzr.ed.am/stdhome
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
1 |
# copy_base.py |
2 |
# |
|
3 |
# Copyright (C) 2013 to 2014 Tim Marston <tim@edm.am> |
|
4 |
# |
|
5 |
# This file is part of stdhome (hereafter referred to as "this program"). |
|
6 |
# See http://ed.am/dev/stdhome for more information. |
|
7 |
# |
|
8 |
# This program is free software: you can redistribute it and/or modify |
|
9 |
# it under the terms of the GNU General Public License as published by |
|
10 |
# the Free Software Foundation, either version 3 of the License, or |
|
11 |
# (at your option) any later version. |
|
12 |
# |
|
13 |
# This program is distributed in the hope that it will be useful, |
|
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
# GNU General Public License for more details. |
|
17 |
# |
|
18 |
# You should have received a copy of the GNU General Public License |
|
19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
||
21 |
||
22 |
import filecmp, os, shutil |
|
23 |
from walker import Walker |
|
24 |
import stdhome.the as the |
|
25 |
||
26 |
||
27 |
class CopyBaseWalker( Walker ): |
|
28 |
"""The copy-base walker traverses a walklist ruthlessly mirroring src to dst. |
|
29 |
It is designed to be the base class of both the copy-in and copy-out walker, |
|
30 |
both of which are specialisations of this purpose. See them for more |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
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). |
|
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
36 |
""" |
37 |
||
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
38 |
|
39 |
def __init__( self ): |
|
40 |
self.check_src_symlinks = False |
|
41 |
self.check_dst_symlinks = False |
|
42 |
self.check_dst_ignores = False |
|
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
43 |
|
44 |
||
45 |
def process( self, rel_file, src, dst ): |
|
46 |
||
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
47 |
# ignore? |
48 |
if self.check_dst_ignores and the.config.ignores.matches( rel_file ): |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
49 |
self.print_op( rel_file, src.type, '#', dst.type ) |
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
50 |
return True |
51 |
||
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
52 |
# src entity is directory |
53 |
if src.type == 'd': |
|
54 |
||
55 |
# if dst entity doesn't exist, create directory (no need to recurse, |
|
56 |
# since we're copying the whole directory) |
|
57 |
if dst.type == '_': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
58 |
self.print_op( rel_file, 'd', '*', '_' ) |
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
59 |
shutil.copytree( src.file, dst.file, True ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
60 |
|
61 |
# if dst entity is a directory, copy permissions, as required (and |
|
62 |
# recurse) |
|
63 |
elif dst.type == 'd': |
|
64 |
# TODO: should check permission and only do as necessary |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
65 |
self.print_op( rel_file, 'd', '*', 'd' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
66 |
shutil.copystat( src.file, dst.file ) |
67 |
return True |
|
68 |
||
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
69 |
# if dst entity is a symlink to a directory, and this is an |
70 |
# acceptable substitute, just recurse |
|
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
71 |
elif self.check_dst_symlinks and dst.link_type == 'd' and \ |
72 |
the.config.symlinks.matches( rel_file ): |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
73 |
self.print_op( rel_file, 'd', '@', 'd' ) |
23
by Tim Marston
implemented CopyInWalker in terms of CopyBaseWalker, changed implementation of |
74 |
return True |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
75 |
|
76 |
# if dst entity is a file or symlink in home dir, replace it with |
|
77 |
# directory (no need to recurse, since we're copying the whole |
|
78 |
# directory) |
|
79 |
elif dst.type == 'f' or dst.type == 'l': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
80 |
self.print_op( rel_file, 'd', '*', dst.type ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
81 |
os.unlink( dst.file ) |
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
82 |
shutil.copytree( src.file, dst.file, True ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
83 |
|
84 |
else: |
|
85 |
raise NotImplementedError() |
|
86 |
||
87 |
# src entity is file |
|
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
88 |
elif src.type == 'f': |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
89 |
|
90 |
# if dst entity doesn't exist, copy file |
|
91 |
if dst.type == '_': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
92 |
self.print_op( rel_file, 'f', '*', '_' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
93 |
shutil.copy( src.file, dst.file ) |
94 |
shutil.copystat( src.file, dst.file ) |
|
95 |
||
96 |
# if dst entity is a file, replace it only if it differs |
|
97 |
elif dst.type == 'f': |
|
98 |
if not filecmp.cmp( src.file, dst.file ): |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
99 |
self.print_op( rel_file, 'f', '*', 'f' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
100 |
os.unlink( dst.file ) |
101 |
shutil.copy( src.file, dst.file ) |
|
102 |
shutil.copystat( src.file, dst.file ) |
|
103 |
else: |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
104 |
self.print_op( rel_file, 'f', '=', 'f' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
105 |
|
106 |
# if dst entity is a directory, replace it with file |
|
107 |
elif dst.type == 'd': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
108 |
self.print_op( rel_file, 'f', '*', 'd' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
109 |
shutil.rmtree( dst.file ) |
110 |
shutil.copy( src.file, dst.file ) |
|
111 |
shutil.copystat( src.file, dst.file ) |
|
112 |
||
113 |
# if dst entity is a symlink, replace it with file |
|
114 |
elif dst.type == 'l': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
115 |
self.print_op( rel_file, 'f', '*', 'l' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
116 |
os.unlink( dst.file ) |
117 |
shutil.copy( src.file, dst.file ) |
|
118 |
shutil.copystat( src.file, dst.file ) |
|
119 |
||
120 |
else: |
|
121 |
raise NotImplementedError() |
|
122 |
||
123 |
# src entity is a symlink |
|
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
124 |
elif src.type == 'l': |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
125 |
|
126 |
# if dst entity doesn't exist, copy symlink |
|
127 |
if dst.type == '_': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
128 |
self.print_op( rel_file, 'l', '*', '_' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
129 |
os.symlink( os.readlink( src.file ), dst.file ) |
130 |
||
131 |
# if dst entity is a symlink, replace it only if it differs |
|
132 |
elif dst.type == 'l': |
|
133 |
if os.readlink( src.file ) != os.readlink( dst.file ): |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
134 |
self.print_op( rel_file, 'l', '*', 'l' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
135 |
os.unlink( dst.file ) |
136 |
os.symlink( os.readlink( src.file ), dst.file ) |
|
137 |
else: |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
138 |
self.print_op( rel_file, 'l', '=', 'l' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
139 |
|
140 |
# if dst entity is a file, replace it with symlink |
|
141 |
elif dst.type == 'f': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
142 |
self.print_op( rel_file, 'l', '*', 'f' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
143 |
os.unlink( dst.file ) |
144 |
os.symlink( os.readlink( src.file ), dst.file ) |
|
145 |
||
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
146 |
# if dst entity is a directory... |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
147 |
elif dst.type == 'd': |
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
148 |
|
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 ): |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
153 |
self.print_op( rel_file, 'd', '@', 'd' ) |
39
by Tim Marston
implemented ignore lists in copyer and made symlink substitution work only one |
154 |
return True |
155 |
||
156 |
# else replace it with a symlink |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
157 |
self.print_op( rel_file, 'l', '*', 'd' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
158 |
shutil.rmtree( dst.file ) |
159 |
os.symlink( os.readlink( src.file ), dst.file ) |
|
160 |
||
161 |
else: |
|
162 |
raise NotImplementedError() |
|
163 |
||
164 |
# src entity is missing |
|
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
165 |
elif src.type == '_': |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
166 |
|
167 |
# if dst entity doesn't exist, we're good |
|
168 |
if dst.type == '_': |
|
169 |
pass; |
|
170 |
||
171 |
# if dst entity is a file or symlink, delete it |
|
23
by Tim Marston
implemented CopyInWalker in terms of CopyBaseWalker, changed implementation of |
172 |
elif dst.type == 'f' or dst.type == 'l': |
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
173 |
self.print_op( rel_file, '_', '*', dst.type ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
174 |
os.unlink( dst.file ) |
175 |
||
176 |
# if dst entity is a directory, delete it |
|
177 |
elif dst.type == 'd': |
|
61
by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to |
178 |
self.print_op( rel_file, '_', '*', 'd' ) |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
179 |
shutil.rmtree( dst.file ) |
180 |
||
181 |
else: |
|
182 |
raise NotImplementedError() |
|
183 |
||
27
by Tim Marston
got symlink accept lists working; fixed some walker bugs |
184 |
# if we got here, we don't want to recurse... |
20
by Tim Marston
moved funcitonality of copy-out walker in to base class and made copy-out walker |
185 |
return False |