/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

20
20
 
21
21
 
22
22
import os
23
 
import stdhome.the as the
24
23
 
25
24
 
26
25
class Walker:
32
31
        process() for more information.
33
32
        """
34
33
 
35
 
 
36
34
        def walk( self ):
37
35
                """Iterates over self.walk_list, calling process() for each entry in turn.  For
38
36
                directory entries, where process() returns False, subsequent entries in
39
37
                walk_list that fall under the directory are skipped.
40
38
                """
41
39
 
42
 
                if the.verbose >= 3:
43
 
                        print "walking [%s]" % self.__class__.__name__
44
 
 
45
40
                skip = ''
 
41
 
46
42
                for rel_file in self.walk_list:
47
43
 
48
44
                        # if we're skipping, skip entries in subdirectories, or turn off
64
60
                        # way of knowing what to check.  It could be src_type or dst_type
65
61
                        # (if src_dir or dst_dir was used to generate the walk list) or it
66
62
                        # could be neither (if the walk list came from somewhere else).  But
67
 
                        # it shouldn't matter: we add a path seperateor (os.sep) to the end
68
 
                        # of the filename, so it wuill only match files that are descendents
69
 
                        # of a directory with the name of this file.
70
 
                        if not recurse: skip = rel_file + os.sep
 
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
71
67
 
72
68
 
73
69
        class File:
83
79
                        else:
84
80
                                self.type = '?'
85
81
                        if os.path.islink( self.file ):
86
 
                                self.link_type = self.type
 
82
                                self.link_type = type
87
83
                                self.type = 'l'
88
84
                        else:
89
85
                                self.link_type = False
95
91
                        elif self.type == '_': return 'missing'
96
92
                        else: return 'unknown'
97
93
 
98
 
                def __str__( self ):
99
 
                        type = self.type
100
 
                        if( self.link_type ): type += '/' + self.link_type
101
 
                        return 'File( %s (%s) )' % ( self.file, type )
102
 
 
103
94
 
104
95
        @staticmethod
105
 
        def generate_walk_list( full_dir, rel_file = '', recurse = True ):
 
96
        def generate_walk_list( full_dir, rel_file = '' ):
106
97
                """Returns a list of files and directories in full_dir, specified as relative
107
98
                files (relative to full_dir), breadth first.
108
 
 
109
99
                """
110
100
 
111
101
                # ignore some files
112
 
                static_ignores = [ '.stdhome', '.stdhomerc' ] + \
113
 
                                                 the.repo.vcs.ignored_files
114
 
                if rel_file in static_ignores:
 
102
                if rel_file in { '.bzr', '.bzrignore', '.stdhome', '.stdhomerc' }:
115
103
                        return list()
116
104
 
117
105
                full_file = os.path.join( full_dir, rel_file )
123
111
                # directories are returned and recursed in to
124
112
                elif os.path.isdir( full_file ):
125
113
                        ret = [ rel_file ] if rel_file != '' else []
126
 
                        if recurse:
127
 
                                for file in os.listdir( full_file ):
128
 
                                        ret.extend( Walker.generate_walk_list(
129
 
                                                full_dir, os.path.join( rel_file, file ) ) )
 
114
                        for file in os.listdir( full_file ):
 
115
                                ret.extend( Walker.generate_walk_list(
 
116
                                        full_dir, os.path.join( rel_file, file ) ) )
130
117
                        return sorted( ret )
131
118
 
132
119
                # other kinds are invalid