/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
# 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_file, src_type, dst_file, dst_type ):

		# 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: serious differences in 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 deploy_files list.  Since these ignored files/directories are
			# transparent to the user, they should have to explicitly permit
			# them via an ignore file (e.g., ~/.stdhome/.ignore, akin to bzr's
			# .bzrignore file).  If these serious differences are not matched by
			# the ignore file, an error should show (which will requie a
			# separate "check" walk of the repo, as is done in copy_out).
			else:
				self.changed.append( "%s (now %s)" % (
					rel_file, self.name_of_type( src_type ) ) )
				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 in 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 deploy_files list.  Since these ignored files/directories are
			# transparent to the user, they should have to explicitly permit
			# them via an ignore file (e.g., ~/.stdhome/.ignore, akin to bzr's
			# .bzrignore file).  If these serious differences are not matched by
			# the ignore file, an error should show (which will requie a
			# separate "check" walk of the repo, as is done in copy_out).
			else:
				self.changed.append( "%s (now %s)" % (
					rel_file, self.name_of_type( src_type ) ) )

		# 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 in 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 deploy_files list.  Since these ignored files/directories are
			# transparent to the user, they should have to explicitly permit
			# them via an ignore file (e.g., ~/.stdhome/.ignore, akin to bzr's
			# .bzrignore file).  If these serious differences are not matched by
			# the ignore file, an error should show (which will requie a
			# separate "check" walk of the repo, as is done in copy_out).
			else:
				self.changed.append( "%s (now %s)" % (
					rel_file, self.name_of_type( src_type ) ) )
		# can not recurse on a non-directory
		return False
		return False