/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:27:29 UTC
  • Revision ID: tim@ed.am-20140405222729-385c9punmvxu2z9z
fixed more bugs in the bzr backend

Show diffs side-by-side

added added

removed removed

21
21
 
22
22
import filecmp, os, shutil
23
23
from walker import Walker
 
24
from stdhome.file_matcher import FileMatcher
24
25
import stdhome.the as the
25
26
 
26
27
 
28
29
        """The copy-base walker traverses a walklist ruthlessly mirroring src to dst.
29
30
        It is designed to be the base class of both the copy-in and copy-out walker,
30
31
        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).
 
32
        information.
36
33
        """
37
34
 
38
 
 
39
 
        def __init__( self ):
40
 
                self.check_src_symlinks = False
41
 
                self.check_dst_symlinks = False
42
 
                self.check_dst_ignores = False
 
35
        def __init__( self, updated_files = None ):
 
36
                self.accept_list = FileMatcher()
43
37
 
44
38
 
45
39
        def process( self, rel_file, src, dst ):
46
40
 
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
41
                # src entity is directory
53
42
                if src.type == 'd':
54
43
 
55
44
                        # if dst entity doesn't exist, create directory (no need to recurse,
56
45
                        # since we're copying the whole directory)
57
46
                        if dst.type == '_':
58
 
                                self.print_op( rel_file, 'd', '*', '_' )
59
 
                                shutil.copytree( src.file, dst.file, True )
 
47
                                if the.verbose > 1: self.print_op( rel_file, 'd>_' )
 
48
                                os.mkdir( dst.file )
 
49
                                shutil.copystat( src.file, dst.file )
 
50
                                return False
60
51
 
61
52
                        # if dst entity is a directory, copy permissions, as required (and
62
53
                        # recurse)
63
54
                        elif dst.type == 'd':
64
55
                                # TODO: should check permission and only do as necessary
65
 
                                self.print_op( rel_file, 'd', '*', 'd' )
 
56
                                if the.verbose > 1: self.print_op( rel_file, 'd>d' )
66
57
                                shutil.copystat( src.file, dst.file )
67
58
                                return True
68
59
 
69
 
                        # if dst entity is a symlink to a directory, and this is an
70
 
                        # 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' )
 
60
                        # TODO: if dst entity is a symlink to a directory, and rel_file is
 
61
                        # matched by the acceptable symlinks file matcher, just recurse
 
62
                        elif dst.link_type == 'd' and self.accept_list.match( rel_file ):
 
63
                                if the.verbose > 1: self.print_op( rel_file, 'd@d' )
74
64
                                return True
75
65
 
76
66
                        # if dst entity is a file or symlink in home dir, replace it with
77
67
                        # directory (no need to recurse, since we're copying the whole
78
68
                        # directory)
79
69
                        elif dst.type == 'f' or dst.type == 'l':
80
 
                                self.print_op( rel_file, 'd', '*', dst.type )
 
70
                                if the.verbose > 1: self.print_op( rel_file, 'd>' + dst.type )
81
71
                                os.unlink( dst.file )
82
 
                                shutil.copytree( src.file, dst.file, True )
 
72
                                os.mkdir( dst.file )
 
73
                                shutil.copystat( src.file, dst.file )
 
74
                                return False
83
75
 
84
76
                        else:
85
77
                                raise NotImplementedError()
86
78
 
87
79
                # src entity is file
88
 
                elif src.type == 'f':
 
80
                if src.type == 'f':
89
81
 
90
82
                        # if dst entity doesn't exist, copy file
91
83
                        if dst.type == '_':
92
 
                                self.print_op( rel_file, 'f', '*', '_' )
 
84
                                if the.verbose > 1: self.print_op( rel_file, 'f>_' )
93
85
                                shutil.copy( src.file, dst.file )
94
86
                                shutil.copystat( src.file, dst.file )
95
87
 
96
88
                        # if dst entity is a file, replace it only if it differs
97
89
                        elif dst.type == 'f':
98
90
                                if not filecmp.cmp( src.file, dst.file ):
99
 
                                        self.print_op( rel_file, 'f', '*', 'f' )
 
91
                                        if the.verbose > 1: self.print_op( rel_file, 'f>f' )
100
92
                                        os.unlink( dst.file )
101
93
                                        shutil.copy( src.file, dst.file )
102
94
                                        shutil.copystat( src.file, dst.file )
103
95
                                else:
104
 
                                        self.print_op( rel_file, 'f', '=', 'f' )
 
96
                                        if the.verbose > 1: self.print_op( rel_file, 'f=f' )
105
97
 
106
98
                        # if dst entity is a directory, replace it with file
107
99
                        elif dst.type == 'd':
108
 
                                self.print_op( rel_file, 'f', '*', 'd' )
 
100
                                if the.verbose > 1: self.print_op( rel_file, 'f>d' )
109
101
                                shutil.rmtree( dst.file )
110
102
                                shutil.copy( src.file, dst.file )
111
103
                                shutil.copystat( src.file, dst.file )
112
104
 
113
105
                        # if dst entity is a symlink, replace it with file
114
106
                        elif dst.type == 'l':
115
 
                                self.print_op( rel_file, 'f', '*', 'l' )
 
107
                                if the.verbose > 1: self.print_op( rel_file, 'f>l' )
116
108
                                os.unlink( dst.file )
117
109
                                shutil.copy( src.file, dst.file )
118
110
                                shutil.copystat( src.file, dst.file )
121
113
                                raise NotImplementedError()
122
114
 
123
115
                # src entity is a symlink
124
 
                elif src.type == 'l':
 
116
                if src.type == 'l':
125
117
 
126
118
                        # if dst entity doesn't exist, copy symlink
127
119
                        if dst.type == '_':
128
 
                                self.print_op( rel_file, 'l', '*', '_' )
 
120
                                if the.verbose > 1: self.print_op( rel_file, 'l>_' )
129
121
                                os.symlink( os.readlink( src.file ), dst.file )
130
122
 
131
123
                        # if dst entity is a symlink, replace it only if it differs
132
124
                        elif dst.type == 'l':
133
125
                                if os.readlink( src.file ) != os.readlink( dst.file ):
134
 
                                        self.print_op( rel_file, 'l', '*', 'l' )
 
126
                                        if the.verbose > 1: self.print_op( rel_file, 'l>l' )
135
127
                                        os.unlink( dst.file )
136
128
                                        os.symlink( os.readlink( src.file ), dst.file )
137
129
                                else:
138
 
                                        self.print_op( rel_file, 'l', '=', 'l' )
 
130
                                        if the.verbose > 1: self.print_op( rel_file, 'l=l' )
139
131
 
140
132
                        # if dst entity is a file, replace it with symlink
141
133
                        elif dst.type == 'f':
142
 
                                self.print_op( rel_file, 'l', '*', 'f' )
 
134
                                if the.verbose > 1: self.print_op( rel_file, 'l>f' )
143
135
                                os.unlink( dst.file )
144
136
                                os.symlink( os.readlink( src.file ), dst.file )
145
137
 
146
 
                        # if dst entity is a directory...
 
138
                        # if dst entity is a directory, replace it with symlink
147
139
                        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' )
 
140
                                if the.verbose > 1: self.print_op( rel_file, 'l>d' )
158
141
                                shutil.rmtree( dst.file )
159
142
                                os.symlink( os.readlink( src.file ), dst.file )
160
143
 
162
145
                                raise NotImplementedError()
163
146
 
164
147
                # src entity is missing
165
 
                elif src.type == '_':
 
148
                if src.type == '_':
166
149
 
167
150
                        # if dst entity doesn't exist, we're good
168
151
                        if dst.type == '_':
170
153
 
171
154
                        # if dst entity is a file or symlink, delete it
172
155
                        elif dst.type == 'f' or dst.type == 'l':
173
 
                                self.print_op( rel_file, '_', '*', dst.type )
 
156
                                if the.verbose > 1: self.print_op( rel_file, '_>' + dst.type )
174
157
                                os.unlink( dst.file )
175
158
 
176
159
                        # if dst entity is a directory, delete it
177
160
                        elif dst.type == 'd':
178
 
                                self.print_op( rel_file, '_', '*', 'd' )
 
161
                                if the.verbose > 1: self.print_op( rel_file, '_>d' )
179
162
                                shutil.rmtree( dst.file )
180
163
 
181
164
                        else:
182
165
                                raise NotImplementedError()
183
166
 
184
 
                # if we got here, we don't want to recurse...
 
167
                # non-directories can not be recursed in to
185
168
                return False