/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
# copy_in.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 CopyInWalker( Walker ):
	"""The copy-in walker traverses the repo, copying-in matching files from the
	home directory.  It will overwrite certin changes (modified files, files
	that have changed to symlinks), but barf at others.

	Walker source:       home dir
	Walker destination:  repo
	Walker traversing:   repo
	"""

	def __init__( self ):
		self.src_dir = the.full_home_dir
		self.dst_dir = the.repo.full_dir
		self.walk_list = self.generate_walk_list( the.repo.full_dir )

		self.changed = list()


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

		# directory (in repo)
		if dst.type == 'd':

			# if entity doesn't exist in home dir, delete directory in repo (and
			# don't recurse, obviously!)
			if src.type == '_':
				if the.verbose > 1: print "  _>d " + rel_file
				shutil.rmtree( dst.file )
				return False

			# if entity is a directory in home dir, copy permissions to
			# diurectory in repo, as necessary, and recurse
			elif src.type == 'd':
				# TODO: should check permissions and only do as necessary
				if the.verbose > 1: print "  d>d " + rel_file
				shutil.copystat( src.file, dst.file )
				return True

			# TODO: if entity is a directory symlink in the home direcotry,
			# silently accept it if it is explicitly matched by the accept list.
			# In which case, we'll also need to add this rel_file to a
			# not_deployed file list so that, after this walk, we can remove it
			# from the deployment's deploy_list.  If we don't, the directory in
			# the repo will get copied-out.
#			elif src.link_type == 'd' and self.accept_list.matches( rel_file ):
#				if the.verbose > 1: print "  d@d " + rel_file
#				return True

			# TODO: serious differences between ~/ and repo (e.g., files in one
			# that are directories in the other) should be ignored (e.g., not
			# copied-in).  And the stuff that is ignored during copy-in should
			# also be ignored during copy-out and must not be added to the
			# deployment's deploy_files list.  Since these ignored files and
			# directories are transparent to the user, they must be explicitly
			# permitted via an "ignore list" in ~/.stdhomerc.  If these serious
			# differences are not matched by the ignore list, an error should be
			# shown (which will require a separate "check" walk of the repo, as
			# is done in copy_out).
			else:
				self.changed.append( "%s (%s => %s)" % ( rel_file,
					dst.get_type_name(), src.get_type_name() ) )
				return False

		# file (in repo)
		elif dst.type == 'f':

			# if entity doesn't exist in home dir, delete file in repo
			if src.type == '_':
				if the.verbose > 1: print "  _>f " + rel_file
				os.unlink( dst.file )

			# if entity in home dir is a symlink, replace file in repo
			elif src.type == 'l':
				if the.verbose > 1: print "  l>f " + rel_file
				os.unlink( dst.file )
				os.symlink( os.readlink( src.file ), dst.file )

			# if entity in home dir is a file, replace file in repo only if it
			# differs
			elif src.type == 'f':
				if not filecmp.cmp( src.file, dst.file ):
					if the.verbose > 1: print "  f>f " + rel_file
					os.unlink( dst.file )
					shutil.copy( src.file, dst.file )
					shutil.copystat( src.file, dst.file )
				else:
					if the.verbose > 1: print "  f=f " + rel_file

			# TODO: serious differences between ~/ and repo (e.g., files in one
			# that are directories in the other) should be ignored (e.g., not
			# copied-in).  And the stuff that is ignored during copy-in should
			# also be ignored during copy-out and must not be added to the
			# deployment's deploy_files list.  Since these ignored files and
			# directories are transparent to the user, they must be explicitly
			# permitted via an "ignore list" in ~/.stdhomerc.  If these serious
			# differences are not matched by the ignore list, an error should be
			# show (which will require a separate "check" walk of the repo, as
			# is done in copy_out).
			else:
				self.changed.append( "%s (%s => %s)" % ( rel_file,
					dst.get_type_name(), src.get_type_name() ) )

		# symlink (in repo)
		elif dst.type == 'l':

			# if entity doesn't exist in home dir, delete symlink in repo
			if src.type == '_':
				if the.verbose > 1: print "  _>l " + rel_file
				os.unlink( dst.file )

			# if entity in home dir is a symlink, replace symlink in repo only
			# if it differs
			elif src.type == 'l':
				if os.readlink( src.file ) != os.readlink( dst.file ):
					if the.verbose > 1: print "  l>l " + rel_file
					os.unlink( dst.file )
					os.symlink( os.readlink( src.file ), dst.file )
				else:
					if the.verbose > 1: print "  l=l " + rel_file

			# if entity in home dir is a file, replace symlink in repo
			elif src.type == 'f':
				if the.verbose > 1: print "  f>l " + rel_file
				os.unlink( dst.file )
				shutil.copy( src.file, dst.file )
				shutil.copystat( src.file, dst.file )

			# TODO: serious differences between ~/ and repo (e.g., files in one
			# that are directories in the other) should be ignored (e.g., not
			# copied-in).  And the stuff that is ignored during copy-in should
			# also be ignored during copy-out and must not be added to the
			# deployment's deploy_files list.  Since these ignored files and
			# directories are transparent to the user, they must be explicitly
			# permitted via an "ignore list" in ~/.stdhomerc.  If these serious
			# differences are not matched by the ignore list, an error should be
			# show (which will require a separate "check" walk of the repo, as
			# is done in copy_out).
			else:
				self.changed.append( "%s (%s => %s)" % ( rel_file,
					dst.get_type_name(). src.get_type_name() ) )

		# can not recurse on a non-directory
		return False