/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/copy_base.py

  • Committer: Tim Marston
  • Date: 2014-04-05 22:29:08 UTC
  • Revision ID: tim@ed.am-20140405222908-3onggkfp5akpz21t
got symlink accept lists working; fixed some walker bugs

Show diffs side-by-side

added added

removed removed

28
28
        """The copy-base walker traverses a walklist ruthlessly mirroring src to dst.
29
29
        It is designed to be the base class of both the copy-in and copy-out walker,
30
30
        both of which are specialisations of this purpose.  See them for more
31
 
        information.  The print_op method, derived in those classes, takes, in
32
 
        addition to the relative filename, a source file type, an operation, and a
33
 
        sestination file type.  Valid file types are f (file), l (symlink), d
34
 
        (directory) and _ (non-existant).  Valid operations are * (modify), = (skip:
35
 
        same), @ (skip: symlink substitute) and # (skip: ignored).
 
31
        information.
36
32
        """
37
33
 
38
 
 
39
 
        def __init__( self ):
40
 
                self.check_src_symlinks = False
41
 
                self.check_dst_symlinks = False
42
 
                self.check_dst_ignores = False
 
34
        accept_list = None
43
35
 
44
36
 
45
37
        def process( self, rel_file, src, dst ):
46
38
 
47
 
                # ignore?
48
 
                if self.check_dst_ignores and the.config.ignores.matches( rel_file ):
49
 
                        self.print_op( rel_file, src.type, '#', dst.type )
50
 
                        return True
51
 
 
52
39
                # src entity is directory
53
40
                if src.type == 'd':
54
41
 
55
42
                        # if dst entity doesn't exist, create directory (no need to recurse,
56
43
                        # since we're copying the whole directory)
57
44
                        if dst.type == '_':
58
 
                                self.print_op( rel_file, 'd', '*', '_' )
 
45
                                if the.verbose > 1: self.print_op( rel_file, 'd>_' )
59
46
                                shutil.copytree( src.file, dst.file, True )
60
47
 
61
48
                        # if dst entity is a directory, copy permissions, as required (and
62
49
                        # recurse)
63
50
                        elif dst.type == 'd':
64
51
                                # TODO: should check permission and only do as necessary
65
 
                                self.print_op( rel_file, 'd', '*', 'd' )
 
52
                                if the.verbose > 1: self.print_op( rel_file, 'd>d' )
66
53
                                shutil.copystat( src.file, dst.file )
67
54
                                return True
68
55
 
69
56
                        # if dst entity is a symlink to a directory, and this is an
70
57
                        # acceptable substitute, just recurse
71
 
                        elif self.check_dst_symlinks and dst.link_type == 'd' and \
72
 
                                        the.config.symlinks.matches( rel_file ):
73
 
                                self.print_op( rel_file, 'd', '@', 'd' )
 
58
                        elif dst.link_type == 'd' and \
 
59
                                        self.accept_list and self.accept_list.match( rel_file ):
 
60
                                if the.verbose > 1: self.print_op( rel_file, 'd@d' )
74
61
                                return True
75
62
 
76
63
                        # if dst entity is a file or symlink in home dir, replace it with
77
64
                        # directory (no need to recurse, since we're copying the whole
78
65
                        # directory)
79
66
                        elif dst.type == 'f' or dst.type == 'l':
80
 
                                self.print_op( rel_file, 'd', '*', dst.type )
 
67
                                if the.verbose > 1: self.print_op( rel_file, 'd>' + dst.type )
81
68
                                os.unlink( dst.file )
82
69
                                shutil.copytree( src.file, dst.file, True )
83
70
 
89
76
 
90
77
                        # if dst entity doesn't exist, copy file
91
78
                        if dst.type == '_':
92
 
                                self.print_op( rel_file, 'f', '*', '_' )
 
79
                                if the.verbose > 1: self.print_op( rel_file, 'f>_' )
93
80
                                shutil.copy( src.file, dst.file )
94
81
                                shutil.copystat( src.file, dst.file )
95
82
 
96
83
                        # if dst entity is a file, replace it only if it differs
97
84
                        elif dst.type == 'f':
98
85
                                if not filecmp.cmp( src.file, dst.file ):
99
 
                                        self.print_op( rel_file, 'f', '*', 'f' )
 
86
                                        if the.verbose > 1: self.print_op( rel_file, 'f>f' )
100
87
                                        os.unlink( dst.file )
101
88
                                        shutil.copy( src.file, dst.file )
102
89
                                        shutil.copystat( src.file, dst.file )
103
90
                                else:
104
 
                                        self.print_op( rel_file, 'f', '=', 'f' )
 
91
                                        if the.verbose > 1: self.print_op( rel_file, 'f=f' )
105
92
 
106
93
                        # if dst entity is a directory, replace it with file
107
94
                        elif dst.type == 'd':
108
 
                                self.print_op( rel_file, 'f', '*', 'd' )
 
95
                                if the.verbose > 1: self.print_op( rel_file, 'f>d' )
109
96
                                shutil.rmtree( dst.file )
110
97
                                shutil.copy( src.file, dst.file )
111
98
                                shutil.copystat( src.file, dst.file )
112
99
 
113
100
                        # if dst entity is a symlink, replace it with file
114
101
                        elif dst.type == 'l':
115
 
                                self.print_op( rel_file, 'f', '*', 'l' )
 
102
                                if the.verbose > 1: self.print_op( rel_file, 'f>l' )
116
103
                                os.unlink( dst.file )
117
104
                                shutil.copy( src.file, dst.file )
118
105
                                shutil.copystat( src.file, dst.file )
125
112
 
126
113
                        # if dst entity doesn't exist, copy symlink
127
114
                        if dst.type == '_':
128
 
                                self.print_op( rel_file, 'l', '*', '_' )
 
115
                                if the.verbose > 1: self.print_op( rel_file, 'l>_' )
129
116
                                os.symlink( os.readlink( src.file ), dst.file )
130
117
 
131
118
                        # if dst entity is a symlink, replace it only if it differs
132
119
                        elif dst.type == 'l':
133
120
                                if os.readlink( src.file ) != os.readlink( dst.file ):
134
 
                                        self.print_op( rel_file, 'l', '*', 'l' )
 
121
                                        if the.verbose > 1: self.print_op( rel_file, 'l>l' )
135
122
                                        os.unlink( dst.file )
136
123
                                        os.symlink( os.readlink( src.file ), dst.file )
137
124
                                else:
138
 
                                        self.print_op( rel_file, 'l', '=', 'l' )
 
125
                                        if the.verbose > 1: self.print_op( rel_file, 'l=l' )
139
126
 
140
127
                        # if dst entity is a file, replace it with symlink
141
128
                        elif dst.type == 'f':
142
 
                                self.print_op( rel_file, 'l', '*', 'f' )
 
129
                                if the.verbose > 1: self.print_op( rel_file, 'l>f' )
143
130
                                os.unlink( dst.file )
144
131
                                os.symlink( os.readlink( src.file ), dst.file )
145
132
 
146
 
                        # if dst entity is a directory...
 
133
                        # if dst entity is a directory, and src entity is a symlink to a
 
134
                        # directory, and this is an acceptable substitute, just recurse
 
135
                        elif dst.type == 'd' and src.link_type == 'd' and \
 
136
                                        self.accept_list and self.accept_list.match( rel_file ):
 
137
                                if the.verbose > 1: self.print_op( rel_file, 'd@d' )
 
138
                                return True
 
139
 
 
140
                        # if dst entity is a directory, replace it with symlink
147
141
                        elif dst.type == 'd':
148
 
 
149
 
                                # if src entity is a symlink to a directory, and this is an
150
 
                                # acceptable substitute, just recurse
151
 
                                if self.check_src_symlinks and src.link_type == 'd' and \
152
 
                                                the.config.symlinks.matches( rel_file ):
153
 
                                        self.print_op( rel_file, 'd', '@', 'd' )
154
 
                                        return True
155
 
 
156
 
                                # else replace it with a symlink
157
 
                                self.print_op( rel_file, 'l', '*', 'd' )
 
142
                                if the.verbose > 1: self.print_op( rel_file, 'l>d' )
158
143
                                shutil.rmtree( dst.file )
159
144
                                os.symlink( os.readlink( src.file ), dst.file )
160
145
 
170
155
 
171
156
                        # if dst entity is a file or symlink, delete it
172
157
                        elif dst.type == 'f' or dst.type == 'l':
173
 
                                self.print_op( rel_file, '_', '*', dst.type )
 
158
                                if the.verbose > 1: self.print_op( rel_file, '_>' + dst.type )
174
159
                                os.unlink( dst.file )
175
160
 
176
161
                        # if dst entity is a directory, delete it
177
162
                        elif dst.type == 'd':
178
 
                                self.print_op( rel_file, '_', '*', 'd' )
 
163
                                if the.verbose > 1: self.print_op( rel_file, '_>d' )
179
164
                                shutil.rmtree( dst.file )
180
165
 
181
166
                        else: