/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-23 19:35:21 UTC
  • Revision ID: tim@ed.am-20160223193521-2vgtxbfos50rrpku
renamed version -> VERSION

Show diffs side-by-side

added added

removed removed

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