/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:33:09 UTC
  • Revision ID: tim@ed.am-20140404223309-macifjzkiryg982n
read ~/.stdhomerc; commands set repo before run(); program performs late
initialisation of some variables; updated help

Show diffs side-by-side

added added

removed removed

20
20
 
21
21
 
22
22
import os
23
 
import stdhome.the
24
23
 
25
24
 
26
25
class Walker:
50
49
                                else:
51
50
                                        skip = ''
52
51
 
53
 
                        src_file = os.path.join( self.src_dir, rel_file )
54
 
                        dst_file = os.path.join( self.dst_dir, rel_file )
55
 
 
56
 
                        src_type = Walker.get_file_type( src_file )
57
 
                        dst_type = Walker.get_file_type( dst_file )
58
 
 
59
 
                        recurse = self.process(
60
 
                                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 )
61
57
 
62
58
                        # Set up skipping, as required.  Note that we don't check to see if
63
59
                        # we're dealing with a directory here.  We can't, because we've no
70
66
                        if not recurse: skip = rel_file + os.pathsep
71
67
 
72
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 = 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
 
73
95
        @staticmethod
74
 
        def get_file_type( full_file ):
75
 
                """Returns the type of a given file, at the time of calling.  Types are 'd' for
76
 
                directory, 'f' for file, 'l' for symlink, '_' for missing and '?' for
77
 
                anything else.
 
96
        def generate_walk_list( full_dir, rel_file = '' ):
 
97
                """Returns a list of files and directories in full_dir, specified as relative
 
98
                files (relative to full_dir), breadth first.
78
99
                """
79
100
 
80
 
                if not os.path.lexists( full_file ):
81
 
                        return '_'
82
 
                elif os.path.islink( full_file ):
83
 
                        return 'l'
84
 
                elif os.path.isfile( full_file ):
85
 
                        return 'f'
 
101
                # ignore some files
 
102
                if rel_file in { '.bzr', '.bzrignore', '.stdhome', '.stdhomerc' }:
 
103
                        return list()
 
104
 
 
105
                full_file = os.path.join( full_dir, rel_file )
 
106
 
 
107
                # files and links are returned
 
108
                if os.path.isfile( full_file ) or os.path.islink( full_file ):
 
109
                        return [ rel_file ]
 
110
 
 
111
                # directories are returned and recursed in to
86
112
                elif os.path.isdir( full_file ):
87
 
                        return 'd'
 
113
                        ret = [ rel_file ] if rel_file != '' else []
 
114
                        for file in os.listdir( full_file ):
 
115
                                ret.extend( Walker.generate_walk_list(
 
116
                                        full_dir, os.path.join( rel_file, file ) ) )
 
117
                        return sorted( ret )
 
118
 
 
119
                # other kinds are invalid
88
120
                else:
89
 
                        return '?'
90
 
 
91
 
 
92
 
        @staticmethod
93
 
        def generate_walk_list( full_dir, rel_dir = '' ):
94
 
                """Returns a list of files and directories in full_dir, specified as relative
95
 
                files (relative to full_dir), breadth first.
96
 
                """
97
 
 
98
 
                ret = list()
99
 
 
100
 
                for file in os.listdir( os.path.join( full_dir, rel_dir ) ):
101
 
 
102
 
                        rel_file = os.path.join( rel_dir, file )
103
 
                        if rel_file == '.bzr': continue
104
 
 
105
 
                        full_file = os.path.join( full_dir, rel_file )
106
 
 
107
 
                        if os.path.isfile( full_file ) or os.path.islink( full_file ):
108
 
                                ret.append( rel_file )
109
 
                        elif os.path.isdir( full_file ):
110
 
                                ret.append( rel_file )
111
 
                                ret.extend( generate_file_list( full_dir, rel_file ) )
112
 
                        else:
113
 
                                raise RuntimeError(
114
 
                                        'unknown/exotic file: %s' % full_file )
115
 
 
116
 
                return ret
117
 
 
118
 
 
119
 
        @staticmethod
120
 
        def name_of_type( type ):
121
 
                if type == 'd': return 'a directory'
122
 
                elif type == 'f': return 'a file'
123
 
                elif type == 'l': return 'a symlink'
124
 
                elif type == '_': return 'missing'
125
 
                else: return 'something exotic'
 
121
                        raise RuntimeError(
 
122
                                'unknown/exotic file: %s' % full_file )