/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-07-17 18:17:18 UTC
  • Revision ID: tim@ed.am-20140717181718-gxi5lpi8qszsigxf
switch to using bzr+ssh, rather than sftp (to work around broken pimiko)

Show diffs side-by-side

added added

removed removed

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