/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:19:47 UTC
  • Revision ID: tim@ed.am-20140717181947-pe060idibfu0kfsk
fixed diff command out-of-order output issue

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