/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/walker/walker.py

  • Committer: Tim Marston
  • Date: 2014-04-04 22:28:49 UTC
  • Revision ID: tim@ed.am-20140404222849-32apy1i949qaq2na
walker now passes Walker.File objects to process(), which includes file name,
type and the type of file it points to when it's a symlink

Show diffs side-by-side

added added

removed removed

 
1
# walker.py
 
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 os
 
23
 
 
24
 
 
25
class Walker:
 
26
        """Classes which derive from the Walker class are intended to walk through
 
27
        (traverse) a single list of filenames in two locations.  They must provide
 
28
        the two locations (src_dir and dst_dir) and a list of relative filenames
 
29
        (walk_list).  For each files in the walk list, the process() method is
 
30
        called, allowing the deriving class to perform some kind of processing.  See
 
31
        process() for more information.
 
32
        """
 
33
 
 
34
        def walk( self ):
 
35
                """Iterates over self.walk_list, calling process() for each entry in turn.  For
 
36
                directory entries, where process() returns False, subsequent entries in
 
37
                walk_list that fall under the directory are skipped.
 
38
                """
 
39
 
 
40
                skip = ''
 
41
 
 
42
                for rel_file in self.walk_list:
 
43
 
 
44
                        # if we're skipping, skip entries in subdirectories, or turn off
 
45
                        # skipping if it no longer matches
 
46
                        if skip:
 
47
                                if skip == rel_file[ : len( skip ) ]:
 
48
                                        continue
 
49
                                else:
 
50
                                        skip = ''
 
51
 
 
52
                        src = Walker.File( os.path.join( self.src_dir, rel_file ) )
 
53
                        dst = Walker.File( os.path.join( self.dst_dir, rel_file ) )
 
54
 
 
55
                        # process ths entry
 
56
                        recurse = self.process( rel_file, src, dst )
 
57
 
 
58
                        # Set up skipping, as required.  Note that we don't check to see if
 
59
                        # we're dealing with a directory here.  We can't, because we've no
 
60
                        # way of knowing what to check.  It could be src_type or dst_type
 
61
                        # (if src_dir or dst_dir was used to generate the walk list) or it
 
62
                        # could be neither (if the walk list came from somewhere else).  But
 
63
                        # it shouldn't matter.  We adding an os.pathset to the end of the
 
64
                        # filename, so it wuill only match files that are descendents of a
 
65
                        # directory with the name of this file.
 
66
                        if not recurse: skip = rel_file + os.pathsep
 
67
 
 
68
 
 
69
        class File:
 
70
 
 
71
                def __init__( self, full_file ):
 
72
                        self.file = full_file
 
73
                        if not os.path.exists( self.file ):
 
74
                                self.type = '_'
 
75
                        elif os.path.isfile( self.file ):
 
76
                                self.type = 'f'
 
77
                        elif os.path.isdir( self.file ):
 
78
                                self.type = 'd'
 
79
                        else:
 
80
                                self.type = '?'
 
81
                        if os.path.islink( self.file ):
 
82
                                self.link_type = type
 
83
                                self.type = 'l'
 
84
                        else:
 
85
                                self.link_type = False
 
86
 
 
87
                def get_type_name( self ):
 
88
                        if self.type == 'l': return 'symlink'
 
89
                        elif self.type == 'f': return 'file'
 
90
                        elif self.type == 'd': return 'directory'
 
91
                        elif self.type == '_': return 'missing'
 
92
                        else: return 'unknown'
 
93
 
 
94
 
 
95
        @staticmethod
 
96
        def generate_walk_list( full_dir, rel_file = '' ):
 
97
                """Returns a list of files and directories in full_dir, specified as relative
 
98
                files (relative to full_dir), breadth first.
 
99
                """
 
100
 
 
101
                # ignore some files
 
102
                if rel_file in { '.bzr', '.bzrignore', '.stdhome', '.stdhomerc' }:
 
103
                        return list()
 
104
 
 
105
                full_file = os.path.join( full_dir, rel_file )
 
106
 
 
107
                # files and links are returned
 
108
                if os.path.isfile( full_file ) or os.path.islink( full_file ):
 
109
                        return [ rel_file ]
 
110
 
 
111
                # directories are returned and recursed in to
 
112
                elif os.path.isdir( full_file ):
 
113
                        ret = [ rel_file ] if rel_file != '' else []
 
114
                        for file in os.listdir( full_file ):
 
115
                                ret.extend( Walker.generate_walk_list(
 
116
                                        full_dir, os.path.join( rel_file, file ) ) )
 
117
                        return sorted( ret )
 
118
 
 
119
                # other kinds are invalid
 
120
                else:
 
121
                        raise RuntimeError(
 
122
                                'unknown/exotic file: %s' % full_file )