/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:09 UTC
  • Revision ID: tim@ed.am-20140405222709-4mp50aiu184blnf1
fixed file_matcher

Show diffs side-by-side

added added

removed removed

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