/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-18 14:52:41 UTC
  • Revision ID: tim@ed.am-20140418145241-gldwc2ey6d2gjv96
defaulted Walker.generate_walk_list() to act on the.repo.full_dir

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
                                else:
50
50
                                        skip = ''
51
51
 
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 )
 
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 )
60
57
 
61
58
                        # Set up skipping, as required.  Note that we don't check to see if
62
59
                        # we're dealing with a directory here.  We can't, because we've no
66
63
                        # it shouldn't matter.  We adding an os.pathset to the end of the
67
64
                        # filename, so it wuill only match files that are descendents of a
68
65
                        # 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 = '' ):
 
66
                        if not recurse: skip = rel_file + os.sep
 
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 = self.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
                def __str__( self ):
 
95
                        type = self.type
 
96
                        if( self.link_type ): type += '/' + self.link_type
 
97
                        return 'File( %s (%s) )' % ( self.file, type )
 
98
 
 
99
 
 
100
        @staticmethod
 
101
        def generate_walk_list( rel_file = '', full_dir = the.repo.full_dir ):
93
102
                """Returns a list of files and directories in full_dir, specified as relative
94
103
                files (relative to full_dir), breadth first.
95
104
                """
96
105
 
97
106
                # ignore some files
98
 
                if rel_file in { '.bzr', '.stdhome' }: return list()
 
107
                if rel_file in { '.bzr', '.bzrignore', '.stdhome', '.stdhomerc' }:
 
108
                        return list()
99
109
 
100
110
                full_file = os.path.join( full_dir, rel_file )
101
111
 
108
118
                        ret = [ rel_file ] if rel_file != '' else []
109
119
                        for file in os.listdir( full_file ):
110
120
                                ret.extend( Walker.generate_walk_list(
111
 
                                        full_dir, os.path.join( rel_file, file ) ) )
 
121
                                        os.path.join( rel_file, file ), full_dir ) )
112
122
                        return sorted( ret )
113
123
 
114
124
                # other kinds are invalid
115
125
                else:
116
126
                        raise RuntimeError(
117
127
                                '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'