/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-04-05 22:26:17 UTC
  • Revision ID: tim@ed.am-20140405222617-4nf4alk3u2dxe430
removed reference to copy-in conflicts (which no longer exist)

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