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

  • Committer: Tim Marston
  • Date: 2014-02-26 19:15:30 UTC
  • Revision ID: tim@ed.am-20140226191530-6x21vlwto2xx80cd
renamed updated_files to affected_files

Show diffs side-by-side

added added

removed removed

1
 
# copy_out.py
 
1
# copy_out_walker.py
2
2
#
3
3
# Copyright (C) 2013 to 2014 Tim Marston <tim@edm.am>
4
4
#
19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
22
 
import re
23
 
from copy_base import CopyBaseWalker
 
22
import filecmp, os, shutil
 
23
from walker import Walker
24
24
import stdhome.the as the
25
 
from stdhome.file_matcher import FileMatcher
26
 
 
27
 
 
28
 
class CopyOutWalker( CopyBaseWalker ):
 
25
 
 
26
 
 
27
class CopyOutWalker( Walker ):
29
28
        """The copy-out walker traverses the repo, copying its content to the home
30
 
        directory.  During an update, it is run *after* checking for conflicts that
31
 
        might occur during copy-out, so any conflicts that exist can be assumed to
32
 
        be unimportant and disposable.  In other words, this will happily overwrite
33
 
        stuff in the home directory, without regard to its content.
 
29
        directory.  It is run *after* checking for conflicts that might occur during
 
30
        copy-out, so any conflicts that exist can be assumed to be unimportant and
 
31
        disposable.  In other words, this will happily overwrite stuff in the home
 
32
        directory, without regard to its content.
34
33
 
35
34
        Walker source:       repo
36
35
        Walker destination:  home dir
38
37
                                                        that no longer exists in the repo
39
38
        """
40
39
 
41
 
        def __init__( self, walk_files = None, quiet = False ):
42
 
                CopyBaseWalker.__init__( self )
 
40
        def __init__( self, updated_files = None ):
43
41
                self.src_dir = the.repo.full_dir
44
 
                self.dst_dir = the.full_home_dir
45
 
                self.walk_list = walk_files if walk_files is not None else \
46
 
                                                 self.generate_walk_list()
47
 
                self.check_dst_symlinks = True
48
 
                self.check_dst_ignores = True
49
 
                self.quiet = quiet
50
 
 
51
 
 
52
 
        def print_op( self, rel_file, src, op, dst ):
53
 
 
54
 
                # report changes
55
 
                if not self.quiet and the.verbose < 2 and op == '*':
56
 
                        if dst == '_':
57
 
                                print " N  %s" % ( rel_file )
58
 
                        elif src == '_':
59
 
                                print " D  %s" % ( rel_file )
60
 
                        elif src == dst:
61
 
                                print " M  %s" % ( rel_file )
62
 
                        else:
63
 
                                print " K  %s" % ( rel_file )
64
 
 
65
 
                # verbose reporting
66
 
                if the.verbose >= 2:
67
 
                        if op == '*': op = '<'
68
 
                        print "  %s%s%s %s" % ( src, op, dst, rel_file )
 
42
                self.dst_dir = the.full_fsdir
 
43
                self.walk_list = updated_files if updated_files is not None else \
 
44
                                                 self.generate_walk_list( the.repo.full_dir )
 
45
 
 
46
 
 
47
        def process( self, rel_file, src_file, src_type, dst_file, dst_type ):
 
48
 
 
49
                # directory (in repo)
 
50
                if src_type == 'd':
 
51
 
 
52
                        # if entity doesn't exist in home dir, create directory (no need to
 
53
                        # recurse, since we're copying the whole directory)
 
54
                        if dst_type == '_':
 
55
                                if the.verbose: 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: 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 )
 
68
                                return False
 
69
 
 
70
                        # if entity is a directory in home dir, copy permissions, as
 
71
                        # required (and recurse)
 
72
                        elif dst_type == 'd':
 
73
                                # TODO: should check permission and only do as necessary
 
74
                                if the.verbose: print "  d<d " + rel_file
 
75
                                shutil.copystat( src_file, dst_file )
 
76
                                return True
 
77
 
 
78
                        else:
 
79
                                raise NotImplementedError()
 
80
 
 
81
                # file (in repo)
 
82
                if src_type == 'f':
 
83
 
 
84
                        # if entity doesn't exist in home dir, copy file
 
85
                        if dst_type == '_':
 
86
                                if the.verbose: 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: print "  l<f " + rel_file
 
93
                                os.unlink( dst_file )
 
94
                                shutil.copy( src_file, dst_file )
 
95
                                shutil.copystat( src_file, dst_file )
 
96
 
 
97
                        # 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 ):
 
100
                                        if the.verbose: print "  f<f " + rel_file
 
101
                                        os.unlink( dst_file )
 
102
                                        shutil.copy( src_file, dst_file )
 
103
                                        shutil.copystat( src_file, dst_file )
 
104
                                else:
 
105
                                        if the.verbose: print "  f=f " + rel_file
 
106
 
 
107
                        # if entity is a directory in home dir, replace it with file
 
108
                        elif dst_type == 'd':
 
109
                                if the.verbose: print "  d<f " + rel_file
 
110
                                shutil.rmtree( dst_file )
 
111
                                shutil.copy( src_file, dst_file )
 
112
                                shutil.copystat( src_file, dst_file )
 
113
 
 
114
                        else:
 
115
                                raise NotImplementedError()
 
116
 
 
117
                # link (in repo)
 
118
                if src_type == 'l':
 
119
 
 
120
                        # if entity doesn't exist in home dir, copy symlink
 
121
                        if dst_type == '_':
 
122
                                if the.verbose: print "  _<l " + rel_file
 
123
                                os.symlink( os.readlink( src_file ), dst_file )
 
124
 
 
125
                        # 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 ):
 
128
                                        if the.verbose: print "  l<l " + rel_file
 
129
                                        os.unlink( dst_file )
 
130
                                        os.symlink( os.readlink( src_file ), dst_file )
 
131
                                else:
 
132
                                        if the.verbose: print "  l=l " + rel_file
 
133
 
 
134
                        # if entity is a file in home dir, replace it with file
 
135
                        elif dst_type == 'f':
 
136
                                if the.verbose: print "  f<l " + rel_file
 
137
                                os.unlink( dst_file )
 
138
                                os.symlink( os.readlink( src_file ), dst_file )
 
139
 
 
140
                        # if entity is a directory in home dir, replace it with file
 
141
                        elif dst_type == 'd':
 
142
                                if the.verbose: print "  d<l " + rel_file
 
143
                                shutil.rmtree( dst_file )
 
144
                                os.symlink( os.readlink( src_file ), dst_file )
 
145
 
 
146
                        else:
 
147
                                raise NotImplementedError()
 
148
 
 
149
                # TODO: handle files missing in repo -- we may or may not want to remove
 
150
                # them from home dir, depending on whether we wanted to delete them
 
151
                # remotely or whether we just wanted to remove them from version control
 
152
 
 
153
                # non-directories can not be recursed in to
 
154
                return False