/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-03-19 20:02:10 UTC
  • Revision ID: tim@ed.am-20140319200210-b6nm63rpktfmw0l3
changed working of output

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
53
49
                                else:
54
50
                                        skip = ''
55
51
 
56
 
                        src = Walker.File( os.path.join( self.src_dir, rel_file ) )
57
 
                        dst = Walker.File( os.path.join( self.dst_dir, rel_file ) )
58
 
 
59
 
                        # process ths entry
60
 
                        recurse = self.process( rel_file, src, dst )
 
52
                        src_file = os.path.join( self.src_dir, rel_file )
 
53
                        dst_file = os.path.join( self.dst_dir, rel_file )
 
54
 
 
55
                        src_type = Walker.get_file_type( src_file )
 
56
                        dst_type = Walker.get_file_type( dst_file )
 
57
 
 
58
                        recurse = self.process(
 
59
                                rel_file, src_file, src_type, dst_file, dst_type )
61
60
 
62
61
                        # Set up skipping, as required.  Note that we don't check to see if
63
62
                        # we're dealing with a directory here.  We can't, because we've no
64
63
                        # way of knowing what to check.  It could be src_type or dst_type
65
64
                        # (if src_dir or dst_dir was used to generate the walk list) or it
66
65
                        # 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
71
 
 
72
 
 
73
 
        class File:
74
 
 
75
 
                def __init__( self, full_file ):
76
 
                        self.file = full_file
77
 
                        if not os.path.exists( self.file ):
78
 
                                self.type = '_'
79
 
                        elif os.path.isfile( self.file ):
80
 
                                self.type = 'f'
81
 
                        elif os.path.isdir( self.file ):
82
 
                                self.type = 'd'
83
 
                        else:
84
 
                                self.type = '?'
85
 
                        if os.path.islink( self.file ):
86
 
                                self.link_type = self.type
87
 
                                self.type = 'l'
88
 
                        else:
89
 
                                self.link_type = False
90
 
 
91
 
                def get_type_name( self ):
92
 
                        if self.type == 'l': return 'symlink'
93
 
                        elif self.type == 'f': return 'file'
94
 
                        elif self.type == 'd': return 'directory'
95
 
                        elif self.type == '_': return 'missing'
96
 
                        else: return 'unknown'
97
 
 
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
 
 
104
 
        @staticmethod
105
 
        def generate_walk_list( full_dir, rel_file = '', recurse = True ):
 
66
                        # it shouldn't matter.  We adding an os.pathset to the end of the
 
67
                        # filename, so it wuill only match files that are descendents of a
 
68
                        # directory with the name of this file.
 
69
                        if not recurse: skip = rel_file + os.pathsep
 
70
 
 
71
 
 
72
        @staticmethod
 
73
        def get_file_type( full_file ):
 
74
                """Returns the type of a given file, at the time of calling.  Types are 'd' for
 
75
                directory, 'f' for file, 'l' for symlink, '_' for missing and '?' for
 
76
                anything else.
 
77
                """
 
78
 
 
79
                if not os.path.lexists( full_file ):
 
80
                        return '_'
 
81
                elif os.path.islink( full_file ):
 
82
                        return 'l'
 
83
                elif os.path.isfile( full_file ):
 
84
                        return 'f'
 
85
                elif os.path.isdir( full_file ):
 
86
                        return 'd'
 
87
                else:
 
88
                        return '?'
 
89
 
 
90
 
 
91
        @staticmethod
 
92
        def generate_walk_list( full_dir, rel_file = '' ):
106
93
                """Returns a list of files and directories in full_dir, specified as relative
107
94
                files (relative to full_dir), breadth first.
108
 
 
109
95
                """
110
96
 
111
97
                # ignore some files
112
 
                static_ignores = [ '.stdhome', '.stdhomerc' ] + \
113
 
                                                 the.repo.vcs.ignored_files
114
 
                if rel_file in static_ignores:
115
 
                        return list()
 
98
                if rel_file in { '.bzr', '.stdhome' }: return list()
116
99
 
117
100
                full_file = os.path.join( full_dir, rel_file )
118
101
 
123
106
                # directories are returned and recursed in to
124
107
                elif os.path.isdir( full_file ):
125
108
                        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 ) ) )
 
109
                        for file in os.listdir( full_file ):
 
110
                                ret.extend( Walker.generate_walk_list(
 
111
                                        full_dir, os.path.join( rel_file, file ) ) )
130
112
                        return sorted( ret )
131
113
 
132
114
                # other kinds are invalid
133
115
                else:
134
116
                        raise RuntimeError(
135
117
                                'unknown/exotic file: %s' % full_file )
 
118
 
 
119
 
 
120
        @staticmethod
 
121
        def name_of_type( type ):
 
122
                if type == 'd': return 'a directory'
 
123
                elif type == 'f': return 'a file'
 
124
                elif type == 'l': return 'a symlink'
 
125
                elif type == '_': return 'missing'
 
126
                else: return 'something exotic'