/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 17:30:58 UTC
  • Revision ID: tim@ed.am-20140405173058-1oyw150nzmxhbc6o
moved funcitonality of copy-out walker in to base class and made copy-out walker
a specialisation of it

Show diffs side-by-side

added added

removed removed

 
1
# copy_base.py
 
2
#
 
3
# Copyright (C) 2013 to 2014 Tim Marston <tim@edm.am>
 
4
#
 
5
# This file is part of stdhome (hereafter referred to as "this program").
 
6
# See http://ed.am/dev/stdhome for more information.
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
 
 
22
import filecmp, os, shutil
 
23
from walker import Walker
 
24
from stdhome.file_matcher import FileMatcher
 
25
import stdhome.the as the
 
26
 
 
27
 
 
28
class CopyBaseWalker( Walker ):
 
29
        """The copy-base walker traverses a walklist ruthlessly mirroring src to dst.
 
30
        It is designed to be the base class of both the copy-in and copy-out walker,
 
31
        both of which are specialisations of this purpose.  See them for more
 
32
        information.
 
33
        """
 
34
 
 
35
        def __init__( self, updated_files = None ):
 
36
                self.accept_list = FileMatcher()
 
37
 
 
38
 
 
39
        def process( self, rel_file, src, dst ):
 
40
 
 
41
                # src entity is directory
 
42
                if src.type == 'd':
 
43
 
 
44
                        # if dst entity doesn't exist, create directory (no need to recurse,
 
45
                        # since we're copying the whole directory)
 
46
                        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
 
51
 
 
52
                        # if dst entity is a directory, copy permissions, as required (and
 
53
                        # recurse)
 
54
                        elif dst.type == 'd':
 
55
                                # TODO: should check permission and only do as necessary
 
56
                                if the.verbose > 1: self.print_cp( rel_file, 'd', 'd' )
 
57
                                shutil.copystat( src.file, dst.file )
 
58
                                return True
 
59
 
 
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
 
65
 
 
66
                        # if dst entity is a file or symlink in home dir, replace it with
 
67
                        # directory (no need to recurse, since we're copying the whole
 
68
                        # directory)
 
69
                        elif dst.type == 'f' or dst.type == 'l':
 
70
                                if the.verbose > 1: self.print_cp( rel_file, 'd', dst.type )
 
71
                                os.unlink( dst.file )
 
72
                                os.mkdir( dst.file )
 
73
                                shutil.copystat( src.file, dst.file )
 
74
                                return False
 
75
 
 
76
                        else:
 
77
                                raise NotImplementedError()
 
78
 
 
79
                # src entity is file
 
80
                if src.type == 'f':
 
81
 
 
82
                        # if dst entity doesn't exist, copy file
 
83
                        if dst.type == '_':
 
84
                                if the.verbose > 1: self.print_cp( rel_file, 'f', '_' )
 
85
                                shutil.copy( src.file, dst.file )
 
86
                                shutil.copystat( src.file, dst.file )
 
87
 
 
88
                        # if dst entity is a file, replace it only if it differs
 
89
                        elif dst.type == 'f':
 
90
                                if not filecmp.cmp( src.file, dst.file ):
 
91
                                        if the.verbose > 1: self.print_cp( rel_file, 'f', 'f' )
 
92
                                        os.unlink( dst.file )
 
93
                                        shutil.copy( src.file, dst.file )
 
94
                                        shutil.copystat( src.file, dst.file )
 
95
                                else:
 
96
                                        if the.verbose > 1: self.print_cp( rel_file, 'f', 'f', '=' )
 
97
 
 
98
                        # if dst entity is a directory, replace it with file
 
99
                        elif dst.type == 'd':
 
100
                                if the.verbose > 1: self.print_cp( rel_file, 'f', 'd' )
 
101
                                shutil.rmtree( dst.file )
 
102
                                shutil.copy( src.file, dst.file )
 
103
                                shutil.copystat( src.file, dst.file )
 
104
 
 
105
                        # if dst entity is a symlink, replace it with file
 
106
                        elif dst.type == 'l':
 
107
                                if the.verbose > 1: self.print_cp( rel_file, 'f', 'l' )
 
108
                                os.unlink( dst.file )
 
109
                                shutil.copy( src.file, dst.file )
 
110
                                shutil.copystat( src.file, dst.file )
 
111
 
 
112
                        else:
 
113
                                raise NotImplementedError()
 
114
 
 
115
                # src entity is a symlink
 
116
                if src.type == 'l':
 
117
 
 
118
                        # if dst entity doesn't exist, copy symlink
 
119
                        if dst.type == '_':
 
120
                                if the.verbose > 1: self.print_cp( rel_file, 'l', '_' )
 
121
                                os.symlink( os.readlink( src.file ), dst.file )
 
122
 
 
123
                        # if dst entity is a symlink, replace it only if it differs
 
124
                        elif dst.type == 'l':
 
125
                                if os.readlink( src.file ) != os.readlink( dst.file ):
 
126
                                        if the.verbose > 1: self.print_cp( rel_file, 'l', 'l' )
 
127
                                        os.unlink( dst.file )
 
128
                                        os.symlink( os.readlink( src.file ), dst.file )
 
129
                                else:
 
130
                                        if the.verbose > 1: self.print_cp( rel_file, 'l', 'l', '=' )
 
131
 
 
132
                        # if dst entity is a file, replace it with symlink
 
133
                        elif dst.type == 'f':
 
134
                                if the.verbose > 1: self.print_cp( rel_file, 'l', 'f' )
 
135
                                os.unlink( dst.file )
 
136
                                os.symlink( os.readlink( src.file ), dst.file )
 
137
 
 
138
                        # if dst entity is a directory, replace it with symlink
 
139
                        elif dst.type == 'd':
 
140
                                if the.verbose > 1: self.print_cp( rel_file, 'l', 'd' )
 
141
                                shutil.rmtree( dst.file )
 
142
                                os.symlink( os.readlink( src.file ), dst.file )
 
143
 
 
144
                        else:
 
145
                                raise NotImplementedError()
 
146
 
 
147
                # src entity is missing
 
148
                if src.type == '_':
 
149
 
 
150
                        # if dst entity doesn't exist, we're good
 
151
                        if dst.type == '_':
 
152
                                pass;
 
153
 
 
154
                        # 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
                                os.unlink( dst.file )
 
158
 
 
159
                        # if dst entity is a directory, delete it
 
160
                        elif dst.type == 'd':
 
161
                                if the.verbose > 1: self.print_cp( rel_file, '_', 'd' )
 
162
                                shutil.rmtree( dst.file )
 
163
 
 
164
                        else:
 
165
                                raise NotImplementedError()
 
166
 
 
167
                # non-directories can not be recursed in to
 
168
                return False