/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-16 00:26:53 UTC
  • Revision ID: tim@ed.am-20160216002653-oa8dgponknyislg3
added home directory change reporting to CopyOutWalker; added --quiet option to
update, resolve, revert and init commands; replace use of re.match with
re.search for clarity (and fixed related bug in FileMatcher); added BzrVcs.run
command output when verbose >= 2

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