/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
# walker.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 os
import stdhome.the


class Walker:
	"""Classes which derive from the Walker class are intended to walk through
	(traverse) a single list of filenames in two locations.  They must provide
	the two locations (src_dir and dst_dir) and a list of relative filenames
	(walk_list).  For each files in the walk list, the process() method is
	called, allowing the deriving class to perform some kind of processing.  See
	process() for more information.
	"""

	def walk( self ):
		"""Iterates over self.walk_list, calling process() for each entry in turn.  For
		directory entries, where process() returns False, subsequent entries in
		walk_list that fall under the directory are skipped.
		"""

		skip = ''

		for rel_file in self.walk_list:

			# if we're skipping, skip entries in subdirectories, or turn off
			# skipping if it no longer matches
			if skip:
				if skip == rel_file[ : len( skip ) ]:
					continue
				else:
					skip = ''

			src_file = os.path.join( self.src_dir, rel_file )
			dst_file = os.path.join( self.dst_dir, rel_file )

			src_type = Walker.get_file_type( src_file )
			dst_type = Walker.get_file_type( dst_file )

			recurse = self.process(
				rel_file, src_file, src_type, dst_file, dst_type )

			# Set up skipping, as required.  Note that we don't check to see if
			# we're dealing with a directory here.  We can't, because we've no
			# way of knowing what to check.  It could be src_type or dst_type
			# (if src_dir or dst_dir was used to generate the walk list) or it
			# could be neither (if the walk list came from somewhere else).  But
			# it shouldn't matter.  We adding an os.pathset to the end of the
			# filename, so it wuill only match files that are descendents of a
			# directory with the name of this file.
			if not recurse: skip = rel_file + os.pathsep


	@staticmethod
	def get_file_type( full_file ):
		"""Returns the type of a given file, at the time of calling.  Types are 'd' for
		directory, 'f' for file, 'l' for symlink, '_' for missing and '?' for
		anything else.
		"""

		if not os.path.lexists( full_file ):
			return '_'
		elif os.path.islink( full_file ):
			return 'l'
		elif os.path.isfile( full_file ):
			return 'f'
		elif os.path.isdir( full_file ):
			return 'd'
		else:
			return '?'


	@staticmethod
	def generate_walk_list( full_dir, rel_dir = '' ):
		"""Returns a list of files and directories in full_dir, specified as relative
		files (relative to full_dir), breadth first.
		"""

		ret = list()

		for file in os.listdir( os.path.join( full_dir, rel_dir ) ):

			rel_file = os.path.join( rel_dir, file )
			if rel_file == '.bzr': continue

			full_file = os.path.join( full_dir, rel_file )

			if os.path.isfile( full_file ) or os.path.islink( full_file ):
				ret.append( rel_file )
			elif os.path.isdir( full_file ):
				ret.append( rel_file )
				ret.extend( generate_file_list( full_dir, rel_file ) )
			else:
				raise RuntimeError(
					'unknown/exotic file: %s' % full_file )

		return ret


	@staticmethod
	def name_of_type( type ):
		if type == 'd': return 'a directory'
		elif type == 'f': return 'a file'
		elif type == 'l': return 'a symlink'
		elif type == '_': return 'missing'
		else: return 'something exotic'