/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-04-17 21:00:42 UTC
  • Revision ID: tim@ed.am-20160417210042-njcd725axg87i20j
renamed tools dir to dev

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