/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# copy_base.py
#
# Copyright (C) 2013 to 2014 Tim Marston <tim@edm.am>
#
# This file is part of stdhome (hereafter referred to as "this program").
# See http://ed.am/dev/stdhome for more information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import filecmp, os, shutil
from walker import Walker
import stdhome.the as the


class CopyBaseWalker( Walker ):
	"""The copy-base walker traverses a walklist ruthlessly mirroring src to dst.
	It is designed to be the base class of both the copy-in and copy-out walker,
	both of which are specialisations of this purpose.  See them for more
	information.  The print_op method, derived in those classes, takes, in
	addition to the relative filename, a source file type, an operation, and a
	sestination file type.  Valid file types are f (file), l (symlink), d
	(directory) and _ (non-existant).  Valid operations are * (modify), = (skip:
	same), @ (skip: symlink substitute) and # (skip: ignored).
	"""


	def __init__( self ):
		self.check_src_symlinks = False
		self.check_dst_symlinks = False
		self.check_dst_ignores = False


	def process( self, rel_file, src, dst ):

		# ignore?
		if self.check_dst_ignores and the.config.ignores.matches( rel_file ):
			self.print_op( rel_file, src.type, '#', dst.type )
			return True

		# src entity is directory
		if src.type == 'd':

			# if dst entity doesn't exist, create directory (no need to recurse,
			# since we're copying the whole directory)
			if dst.type == '_':
				self.print_op( rel_file, 'd', '*', '_' )
				shutil.copytree( src.file, dst.file, True )

			# if dst entity is a directory, copy permissions, as required (and
			# recurse)
			elif dst.type == 'd':
				# TODO: should check permission and only do as necessary
				self.print_op( rel_file, 'd', '*', 'd' )
				shutil.copystat( src.file, dst.file )
				return True

			# if dst entity is a symlink to a directory, and this is an
			# acceptable substitute, just recurse
			elif self.check_dst_symlinks and dst.link_type == 'd' and \
					the.config.symlinks.matches( rel_file ):
				self.print_op( rel_file, 'd', '@', 'd' )
				return True

			# if dst entity is a file or symlink in home dir, replace it with
			# directory (no need to recurse, since we're copying the whole
			# directory)
			elif dst.type == 'f' or dst.type == 'l':
				self.print_op( rel_file, 'd', '*', dst.type )
				os.unlink( dst.file )
				shutil.copytree( src.file, dst.file, True )

			else:
				raise NotImplementedError()

		# src entity is file
		elif src.type == 'f':

			# if dst entity doesn't exist, copy file
			if dst.type == '_':
				self.print_op( rel_file, 'f', '*', '_' )
				shutil.copy( src.file, dst.file )
				shutil.copystat( src.file, dst.file )

			# if dst entity is a file, replace it only if it differs
			elif dst.type == 'f':
				if not filecmp.cmp( src.file, dst.file ):
					self.print_op( rel_file, 'f', '*', 'f' )
					os.unlink( dst.file )
					shutil.copy( src.file, dst.file )
					shutil.copystat( src.file, dst.file )
				else:
					self.print_op( rel_file, 'f', '=', 'f' )

			# if dst entity is a directory, replace it with file
			elif dst.type == 'd':
				self.print_op( rel_file, 'f', '*', 'd' )
				shutil.rmtree( dst.file )
				shutil.copy( src.file, dst.file )
				shutil.copystat( src.file, dst.file )

			# if dst entity is a symlink, replace it with file
			elif dst.type == 'l':
				self.print_op( rel_file, 'f', '*', 'l' )
				os.unlink( dst.file )
				shutil.copy( src.file, dst.file )
				shutil.copystat( src.file, dst.file )

			else:
				raise NotImplementedError()

		# src entity is a symlink
		elif src.type == 'l':

			# if dst entity doesn't exist, copy symlink
			if dst.type == '_':
				self.print_op( rel_file, 'l', '*', '_' )
				os.symlink( os.readlink( src.file ), dst.file )

			# if dst entity is a symlink, replace it only if it differs
			elif dst.type == 'l':
				if os.readlink( src.file ) != os.readlink( dst.file ):
					self.print_op( rel_file, 'l', '*', 'l' )
					os.unlink( dst.file )
					os.symlink( os.readlink( src.file ), dst.file )
				else:
					self.print_op( rel_file, 'l', '=', 'l' )

			# if dst entity is a file, replace it with symlink
			elif dst.type == 'f':
				self.print_op( rel_file, 'l', '*', 'f' )
				os.unlink( dst.file )
				os.symlink( os.readlink( src.file ), dst.file )

			# if dst entity is a directory...
			elif dst.type == 'd':

				# if src entity is a symlink to a directory, and this is an
				# acceptable substitute, just recurse
				if self.check_src_symlinks and src.link_type == 'd' and \
						the.config.symlinks.matches( rel_file ):
					self.print_op( rel_file, 'd', '@', 'd' )
					return True

				# else replace it with a symlink
				self.print_op( rel_file, 'l', '*', 'd' )
				shutil.rmtree( dst.file )
				os.symlink( os.readlink( src.file ), dst.file )

			else:
				raise NotImplementedError()

		# src entity is missing
		elif src.type == '_':

			# if dst entity doesn't exist, we're good
			if dst.type == '_':
				pass;

			# if dst entity is a file or symlink, delete it
			elif dst.type == 'f' or dst.type == 'l':
				self.print_op( rel_file, '_', '*', dst.type )
				os.unlink( dst.file )

			# if dst entity is a directory, delete it
			elif dst.type == 'd':
				self.print_op( rel_file, '_', '*', 'd' )
				shutil.rmtree( dst.file )

			else:
				raise NotImplementedError()

		# if we got here, we don't want to recurse...
		return False