/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: 2016-02-13 11:38:50 UTC
  • Revision ID: tim@ed.am-20160213113850-iaplsdx6h1y4l21b
switch to lzip

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