/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-06 14:04:12 UTC
  • Revision ID: tim@ed.am-20140406140412-jucgusrltzf8vyz5
during `stdhome status`, announce if a deployment is ongoing

Show diffs side-by-side

added added

removed removed

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