/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: 2016-05-22 16:40:51 UTC
  • Revision ID: tim@ed.am-20160522164051-h2n54oaxg25c7x7c
updated todo

Show diffs side-by-side

added added

removed removed

20
20
 
21
21
 
22
22
import os
 
23
import stdhome.the as the
23
24
 
24
25
 
25
26
class Walker:
37
38
                walk_list that fall under the directory are skipped.
38
39
                """
39
40
 
 
41
                if the.verbose >= 3: print "walking [%s]" % self.__class__.__name__
 
42
 
40
43
                skip = ''
41
 
 
42
44
                for rel_file in self.walk_list:
43
45
 
44
46
                        # if we're skipping, skip entries in subdirectories, or turn off
49
51
                                else:
50
52
                                        skip = ''
51
53
 
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 )
 
54
                        src = Walker.File( os.path.join( self.src_dir, rel_file ) )
 
55
                        dst = Walker.File( os.path.join( self.dst_dir, rel_file ) )
 
56
 
 
57
                        # process ths entry
 
58
                        recurse = self.process( rel_file, src, dst )
60
59
 
61
60
                        # Set up skipping, as required.  Note that we don't check to see if
62
61
                        # we're dealing with a directory here.  We can't, because we've no
63
62
                        # way of knowing what to check.  It could be src_type or dst_type
64
63
                        # (if src_dir or dst_dir was used to generate the walk list) or it
65
64
                        # could be neither (if the walk list came from somewhere else).  But
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 = '' ):
 
65
                        # it shouldn't matter: we add a path seperateor (os.sep) to the end
 
66
                        # of the filename, so it wuill only match files that are descendents
 
67
                        # of a directory with the name of this file.
 
68
                        if not recurse: skip = rel_file + os.sep
 
69
 
 
70
 
 
71
        class File:
 
72
 
 
73
                def __init__( self, full_file ):
 
74
                        self.file = full_file
 
75
                        if not os.path.exists( self.file ):
 
76
                                self.type = '_'
 
77
                        elif os.path.isfile( self.file ):
 
78
                                self.type = 'f'
 
79
                        elif os.path.isdir( self.file ):
 
80
                                self.type = 'd'
 
81
                        else:
 
82
                                self.type = '?'
 
83
                        if os.path.islink( self.file ):
 
84
                                self.link_type = self.type
 
85
                                self.type = 'l'
 
86
                        else:
 
87
                                self.link_type = False
 
88
 
 
89
                def get_type_name( self ):
 
90
                        if self.type == 'l': return 'symlink'
 
91
                        elif self.type == 'f': return 'file'
 
92
                        elif self.type == 'd': return 'directory'
 
93
                        elif self.type == '_': return 'missing'
 
94
                        else: return 'unknown'
 
95
 
 
96
                def __str__( self ):
 
97
                        type = self.type
 
98
                        if( self.link_type ): type += '/' + self.link_type
 
99
                        return 'File( %s (%s) )' % ( self.file, type )
 
100
 
 
101
 
 
102
        @staticmethod
 
103
        def generate_walk_list( rel_file = '', full_dir = None ):
93
104
                """Returns a list of files and directories in full_dir, specified as relative
94
105
                files (relative to full_dir), breadth first.
95
106
                """
96
107
 
 
108
                # default place to walk
 
109
                if full_dir is None: full_dir = the.repo.full_dir
 
110
 
97
111
                # ignore some files
98
 
                if rel_file in { '.bzr', '.stdhome' }: return list()
 
112
                if rel_file in [ '.bzr', '.bzrignore', '.stdhome', '.stdhomerc' ]:
 
113
                        return list()
99
114
 
100
115
                full_file = os.path.join( full_dir, rel_file )
101
116
 
108
123
                        ret = [ rel_file ] if rel_file != '' else []
109
124
                        for file in os.listdir( full_file ):
110
125
                                ret.extend( Walker.generate_walk_list(
111
 
                                        full_dir, os.path.join( rel_file, file ) ) )
 
126
                                        os.path.join( rel_file, file ), full_dir ) )
112
127
                        return sorted( ret )
113
128
 
114
129
                # other kinds are invalid
115
130
                else:
116
131
                        raise RuntimeError(
117
132
                                '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'