/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
1
# copy_in.py
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
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 CopyInWalker( Walker ):
28
	"""The copy-in walker traverses the repo, copying-in matching files from the
29
	home directory.  It will overwrite certin changes (modified files, files
30
	that have changed to symlinks), but barf at others.
31
32
	Walker source:       home dir
33
	Walker destination:  repo
34
	Walker traversing:   repo
35
	"""
36
37
	def __init__( self ):
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
38
		self.src_dir = the.full_home_dir
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
39
		self.dst_dir = the.repo.full_dir
40
		self.walk_list = self.generate_walk_list( the.repo.full_dir )
41
42
		self.changed = list()
43
44
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
45
	def process( self, rel_file, src, dst ):
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
46
47
		# directory (in repo)
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
48
		if dst.type == 'd':
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
49
50
			# if entity doesn't exist in home dir, delete directory in repo (and
51
			# don't recurse, obviously!)
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
52
			if src.type == '_':
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
53
				if the.verbose > 1: print "  _>d " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
54
				shutil.rmtree( dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
55
				return False
56
57
			# if entity is a directory in home dir, copy permissions to
58
			# diurectory in repo, as necessary, and recurse
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
59
			elif src.type == 'd':
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
60
				# TODO: should check permissions and only do as necessary
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
61
				if the.verbose > 1: print "  d>d " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
62
				shutil.copystat( src.file, dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
63
				return True
64
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
65
			# TODO: if entity is a directory symlink in the home direcotry,
66
			# silently accept it if it is explicitly matched by the accept list.
67
			# In which case, we'll also need to add this rel_file to a
68
			# not_deployed file list so that, after this walk, we can remove it
69
			# from the deployment's deploy_list.  If we don't, the directory in
70
			# the repo will get copied-out.
71
#			elif src.link_type == 'd' and self.accept_list.matches( rel_file ):
72
#				if the.verbose > 1: print "  d@d " + rel_file
73
#				return True
74
75
			# TODO: serious differences between ~/ and repo (e.g., files in one
76
			# that are directories in the other) should be ignored (e.g., not
77
			# copied-in).  And the stuff that is ignored during copy-in should
78
			# also be ignored during copy-out and must not be added to the
79
			# deployment's deploy_files list.  Since these ignored files and
80
			# directories are transparent to the user, they must be explicitly
81
			# permitted via an "ignore list" in ~/.stdhomerc.  If these serious
82
			# differences are not matched by the ignore list, an error should be
83
			# shown (which will require a separate "check" walk of the repo, as
84
			# is done in copy_out).
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
85
			else:
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
86
				self.changed.append( "%s (%s => %s)" % ( rel_file,
87
					dst.get_type_name(), src.get_type_name() ) )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
88
				return False
89
90
		# file (in repo)
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
91
		elif dst.type == 'f':
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
92
93
			# if entity doesn't exist in home dir, delete file in repo
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
94
			if src.type == '_':
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
95
				if the.verbose > 1: print "  _>f " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
96
				os.unlink( dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
97
98
			# if entity in home dir is a symlink, replace file in repo
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
99
			elif src.type == 'l':
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
100
				if the.verbose > 1: print "  l>f " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
101
				os.unlink( dst.file )
102
				os.symlink( os.readlink( src.file ), dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
103
104
			# if entity in home dir is a file, replace file in repo only if it
105
			# differs
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
106
			elif src.type == 'f':
107
				if not filecmp.cmp( src.file, dst.file ):
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
108
					if the.verbose > 1: print "  f>f " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
109
					os.unlink( dst.file )
110
					shutil.copy( src.file, dst.file )
111
					shutil.copystat( src.file, dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
112
				else:
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
113
					if the.verbose > 1: print "  f=f " + rel_file
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
114
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
115
			# TODO: serious differences between ~/ and repo (e.g., files in one
116
			# that are directories in the other) should be ignored (e.g., not
117
			# copied-in).  And the stuff that is ignored during copy-in should
118
			# also be ignored during copy-out and must not be added to the
119
			# deployment's deploy_files list.  Since these ignored files and
120
			# directories are transparent to the user, they must be explicitly
121
			# permitted via an "ignore list" in ~/.stdhomerc.  If these serious
122
			# differences are not matched by the ignore list, an error should be
123
			# show (which will require a separate "check" walk of the repo, as
124
			# is done in copy_out).
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
125
			else:
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
126
				self.changed.append( "%s (%s => %s)" % ( rel_file,
127
					dst.get_type_name(), src.get_type_name() ) )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
128
129
		# symlink (in repo)
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
130
		elif dst.type == 'l':
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
131
132
			# if entity doesn't exist in home dir, delete symlink in repo
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
133
			if src.type == '_':
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
134
				if the.verbose > 1: print "  _>l " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
135
				os.unlink( dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
136
137
			# if entity in home dir is a symlink, replace symlink in repo only
138
			# if it differs
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
139
			elif src.type == 'l':
140
				if os.readlink( src.file ) != os.readlink( dst.file ):
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
141
					if the.verbose > 1: print "  l>l " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
142
					os.unlink( dst.file )
143
					os.symlink( os.readlink( src.file ), dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
144
				else:
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
145
					if the.verbose > 1: print "  l=l " + rel_file
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
146
147
			# if entity in home dir is a file, replace symlink in repo
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
148
			elif src.type == 'f':
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
149
				if the.verbose > 1: print "  f>l " + rel_file
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
150
				os.unlink( dst.file )
151
				shutil.copy( src.file, dst.file )
152
				shutil.copystat( src.file, dst.file )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
153
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
154
			# TODO: serious differences between ~/ and repo (e.g., files in one
155
			# that are directories in the other) should be ignored (e.g., not
156
			# copied-in).  And the stuff that is ignored during copy-in should
157
			# also be ignored during copy-out and must not be added to the
158
			# deployment's deploy_files list.  Since these ignored files and
159
			# directories are transparent to the user, they must be explicitly
160
			# permitted via an "ignore list" in ~/.stdhomerc.  If these serious
161
			# differences are not matched by the ignore list, an error should be
162
			# show (which will require a separate "check" walk of the repo, as
163
			# is done in copy_out).
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
164
			else:
16 by Tim Marston
walker now passes Walker.File objects to process(), which includes file name,
165
				self.changed.append( "%s (%s => %s)" % ( rel_file,
166
					dst.get_type_name(). src.get_type_name() ) )
14 by Tim Marston
removed double return
167
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
168
		# can not recurse on a non-directory
169
		return False