/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-12 12:53:54 UTC
  • Revision ID: tim@ed.am-20140412125354-64aysxvaz7nnk8hm
make verbose levels clearer

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