/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-02-28 17:51:29 UTC
  • Revision ID: tim@ed.am-20160228175129-ea341cedv8g3s63m
fixed display of bzr 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
23
24
 
24
25
 
25
26
class Walker:
49
50
                                else:
50
51
                                        skip = ''
51
52
 
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 )
 
53
                        src = Walker.File( os.path.join( self.src_dir, rel_file ) )
 
54
                        dst = Walker.File( os.path.join( self.dst_dir, rel_file ) )
 
55
 
 
56
                        # process ths entry
 
57
                        recurse = self.process( rel_file, src, dst )
60
58
 
61
59
                        # Set up skipping, as required.  Note that we don't check to see if
62
60
                        # we're dealing with a directory here.  We can't, because we've no
66
64
                        # it shouldn't matter.  We adding an os.pathset to the end of the
67
65
                        # filename, so it wuill only match files that are descendents of a
68
66
                        # 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 = '' ):
 
67
                        if not recurse: skip = rel_file + os.sep
 
68
 
 
69
 
 
70
        class File:
 
71
 
 
72
                def __init__( self, full_file ):
 
73
                        self.file = full_file
 
74
                        if not os.path.exists( self.file ):
 
75
                                self.type = '_'
 
76
                        elif os.path.isfile( self.file ):
 
77
                                self.type = 'f'
 
78
                        elif os.path.isdir( self.file ):
 
79
                                self.type = 'd'
 
80
                        else:
 
81
                                self.type = '?'
 
82
                        if os.path.islink( self.file ):
 
83
                                self.link_type = self.type
 
84
                                self.type = 'l'
 
85
                        else:
 
86
                                self.link_type = False
 
87
 
 
88
                def get_type_name( self ):
 
89
                        if self.type == 'l': return 'symlink'
 
90
                        elif self.type == 'f': return 'file'
 
91
                        elif self.type == 'd': return 'directory'
 
92
                        elif self.type == '_': return 'missing'
 
93
                        else: return 'unknown'
 
94
 
 
95
                def __str__( self ):
 
96
                        type = self.type
 
97
                        if( self.link_type ): type += '/' + self.link_type
 
98
                        return 'File( %s (%s) )' % ( self.file, type )
 
99
 
 
100
 
 
101
        @staticmethod
 
102
        def generate_walk_list( rel_file = '', full_dir = None ):
93
103
                """Returns a list of files and directories in full_dir, specified as relative
94
104
                files (relative to full_dir), breadth first.
95
105
                """
96
106
 
 
107
                # default place to walk
 
108
                if full_dir is None: full_dir = the.repo.full_dir
 
109
 
97
110
                # ignore some files
98
 
                if rel_file in { '.bzr', '.stdhome' }: return list()
 
111
                if rel_file in [ '.bzr', '.bzrignore', '.stdhome', '.stdhomerc' ]:
 
112
                        return list()
99
113
 
100
114
                full_file = os.path.join( full_dir, rel_file )
101
115
 
108
122
                        ret = [ rel_file ] if rel_file != '' else []
109
123
                        for file in os.listdir( full_file ):
110
124
                                ret.extend( Walker.generate_walk_list(
111
 
                                        full_dir, os.path.join( rel_file, file ) ) )
 
125
                                        os.path.join( rel_file, file ), full_dir ) )
112
126
                        return sorted( ret )
113
127
 
114
128
                # other kinds are invalid
115
129
                else:
116
130
                        raise RuntimeError(
117
131
                                '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'