/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_out.py

  • Committer: Tim Marston
  • Date: 2014-04-04 22:28:49 UTC
  • Revision ID: tim@ed.am-20140404222849-32apy1i949qaq2na
walker now passes Walker.File objects to process(), which includes file name,
type and the type of file it points to when it's a symlink

Show diffs side-by-side

added added

removed removed

43
43
                self.walk_list = updated_files if updated_files is not None else \
44
44
                                                 self.generate_walk_list( the.repo.full_dir )
45
45
 
46
 
 
47
 
        def process( self, rel_file, src_file, src_type, dst_file, dst_type ):
 
46
                self.accept_symlinks = list()
 
47
 
 
48
 
 
49
        def process( self, rel_file, src, dst ):
48
50
 
49
51
                # directory (in repo)
50
 
                if src_type == 'd':
 
52
                if src.type == 'd':
51
53
 
52
54
                        # if entity doesn't exist in home dir, create directory (no need to
53
55
                        # recurse, since we're copying the whole directory)
54
 
                        if dst_type == '_':
 
56
                        if dst.type == '_':
55
57
                                if the.verbose > 1: print "  _<d " + rel_file
56
 
                                os.mkdir( dst_file )
57
 
                                shutil.copystat( src_file, dst_file )
58
 
                                return False
59
 
 
60
 
                        # if entity is a file or symlink in home dir, replace it with
61
 
                        # directory (no need to recurse, since we're copying the whole
62
 
                        # directory)
63
 
                        elif dst_type == 'f' or dst_type == 'l':
64
 
                                if the.verbose > 1: print "  %s<d %s" % ( dst_type, rel_file )
65
 
                                os.unlink( dst_file )
66
 
                                os.mkdir( dst_file )
67
 
                                shutil.copystat( src_file, dst_file )
 
58
                                os.mkdir( dst.file )
 
59
                                shutil.copystat( src.file, dst.file )
68
60
                                return False
69
61
 
70
62
                        # if entity is a directory in home dir, copy permissions, as
71
63
                        # required (and recurse)
72
 
                        elif dst_type == 'd':
 
64
                        elif dst.type == 'd':
73
65
                                # TODO: should check permission and only do as necessary
74
66
                                if the.verbose > 1: print "  d<d " + rel_file
75
 
                                shutil.copystat( src_file, dst_file )
 
67
                                shutil.copystat( src.file, dst.file )
76
68
                                return True
77
69
 
 
70
                        # TODO: if entity is a symlink to a directory, and this is
 
71
                        # acceptable, just recurse
 
72
#                       elif dst.link_type == 'd' and self.accept_list.match( rel_file ):
 
73
#                               if the.verbose > 1: print "  d@=d " + rel_file
 
74
#                               return True
 
75
 
 
76
                        # if entity is a file or symlink in home dir, replace it with
 
77
                        # directory (no need to recurse, since we're copying the whole
 
78
                        # directory)
 
79
                        elif dst.type == 'f' or dst.type == 'l':
 
80
                                if the.verbose > 1: print "  %s<d %s" % ( dst.type, rel_file )
 
81
                                os.unlink( dst.file )
 
82
                                os.mkdir( dst.file )
 
83
                                shutil.copystat( src.file, dst.file )
 
84
                                return False
 
85
 
78
86
                        else:
79
87
                                raise NotImplementedError()
80
88
 
81
89
                # file (in repo)
82
 
                if src_type == 'f':
 
90
                if src.type == 'f':
83
91
 
84
92
                        # if entity doesn't exist in home dir, copy file
85
 
                        if dst_type == '_':
 
93
                        if dst.type == '_':
86
94
                                if the.verbose > 1: print "  _<f " + rel_file
87
 
                                shutil.copy( src_file, dst_file )
88
 
                                shutil.copystat( src_file, dst_file )
89
 
 
90
 
                        # if entity is a symlink in home dir, replace it with file
91
 
                        elif dst_type == 'l':
92
 
                                if the.verbose > 1: print "  l<f " + rel_file
93
 
                                os.unlink( dst_file )
94
 
                                shutil.copy( src_file, dst_file )
95
 
                                shutil.copystat( src_file, dst_file )
 
95
                                shutil.copy( src.file, dst.file )
 
96
                                shutil.copystat( src.file, dst.file )
96
97
 
97
98
                        # if entity is a file in home dir, replace it only if it differs
98
 
                        elif dst_type == 'f':
99
 
                                if not filecmp.cmp( src_file, dst_file ):
 
99
                        elif dst.type == 'f':
 
100
                                if not filecmp.cmp( src.file, dst.file ):
100
101
                                        if the.verbose > 1: print "  f<f " + rel_file
101
 
                                        os.unlink( dst_file )
102
 
                                        shutil.copy( src_file, dst_file )
103
 
                                        shutil.copystat( src_file, dst_file )
 
102
                                        os.unlink( dst.file )
 
103
                                        shutil.copy( src.file, dst.file )
 
104
                                        shutil.copystat( src.file, dst.file )
104
105
                                else:
105
106
                                        if the.verbose > 1: print "  f=f " + rel_file
106
107
 
107
108
                        # if entity is a directory in home dir, replace it with file
108
 
                        elif dst_type == 'd':
 
109
                        elif dst.type == 'd':
109
110
                                if the.verbose > 1: print "  d<f " + rel_file
110
 
                                shutil.rmtree( dst_file )
111
 
                                shutil.copy( src_file, dst_file )
112
 
                                shutil.copystat( src_file, dst_file )
 
111
                                shutil.rmtree( dst.file )
 
112
                                shutil.copy( src.file, dst.file )
 
113
                                shutil.copystat( src.file, dst.file )
 
114
 
 
115
                        # if entity is a symlink in home dir, replace it with file
 
116
                        elif dst.type == 'l':
 
117
                                if the.verbose > 1: print "  l<f " + rel_file
 
118
                                os.unlink( dst.file )
 
119
                                shutil.copy( src.file, dst.file )
 
120
                                shutil.copystat( src.file, dst.file )
113
121
 
114
122
                        else:
115
123
                                raise NotImplementedError()
116
124
 
117
125
                # link (in repo)
118
 
                if src_type == 'l':
 
126
                if src.type == 'l':
119
127
 
120
128
                        # if entity doesn't exist in home dir, copy symlink
121
 
                        if dst_type == '_':
 
129
                        if dst.type == '_':
122
130
                                if the.verbose > 1: print "  _<l " + rel_file
123
 
                                os.symlink( os.readlink( src_file ), dst_file )
 
131
                                os.symlink( os.readlink( src.file ), dst.file )
124
132
 
125
133
                        # if entity is a symlink in home dir, replace it only if it differs
126
 
                        elif dst_type == 'l':
127
 
                                if os.readlink( src_file ) != os.readlink( dst_file ):
 
134
                        elif dst.type == 'l':
 
135
                                if os.readlink( src.file ) != os.readlink( dst.file ):
128
136
                                        if the.verbose > 1: print "  l<l " + rel_file
129
 
                                        os.unlink( dst_file )
130
 
                                        os.symlink( os.readlink( src_file ), dst_file )
 
137
                                        os.unlink( dst.file )
 
138
                                        os.symlink( os.readlink( src.file ), dst.file )
131
139
                                else:
132
140
                                        if the.verbose > 1: print "  l=l " + rel_file
133
141
 
134
142
                        # if entity is a file in home dir, replace it with file
135
 
                        elif dst_type == 'f':
 
143
                        elif dst.type == 'f':
136
144
                                if the.verbose > 1: print "  f<l " + rel_file
137
 
                                os.unlink( dst_file )
138
 
                                os.symlink( os.readlink( src_file ), dst_file )
 
145
                                os.unlink( dst.file )
 
146
                                os.symlink( os.readlink( src.file ), dst.file )
139
147
 
140
148
                        # if entity is a directory in home dir, replace it with file
141
 
                        elif dst_type == 'd':
 
149
                        elif dst.type == 'd':
142
150
                                if the.verbose > 1: print "  d<l " + rel_file
143
 
                                shutil.rmtree( dst_file )
144
 
                                os.symlink( os.readlink( src_file ), dst_file )
 
151
                                shutil.rmtree( dst.file )
 
152
                                os.symlink( os.readlink( src.file ), dst.file )
145
153
 
146
154
                        else:
147
155
                                raise NotImplementedError()
148
156
 
149
 
                # deleted file
150
 
                if src_type == '_':
 
157
                # deleted file (in repo)
 
158
                if src.type == '_':
151
159
 
152
160
                        # if entity doesn't exist in home dir, we're good
153
 
                        if dst_type == '_':
 
161
                        if dst.type == '_':
154
162
                                pass;
155
163
 
156
164
                        # if entity is a file or symlink in home dir, delete it
157
 
                        if dst_type == 'f' or dst_type == 'l':
158
 
                                if the.verbose > 1: print "  %s<_ %s" % ( dst_type, rel_file )
159
 
                                os.unlink( dst_file )
 
165
                        if dst.type == 'f' or dst.type == 'l':
 
166
                                if the.verbose > 1: print "  %s<_ %s" % ( dst.type, rel_file )
 
167
                                os.unlink( dst.file )
160
168
 
161
169
                        # if entity is a directory in home dir, delete it
162
 
                        elif dst_type == 'd':
 
170
                        elif dst.type == 'd':
163
171
                                if the.verbose > 1: print "  d<_ " + rel_file
164
 
                                shutil.rmtree( dst_file )
 
172
                                shutil.rmtree( dst.file )
165
173
 
166
174
                        else:
167
175
                                raise NotImplementedError()