~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Robert Collins
  • Date: 2005-09-30 02:54:51 UTC
  • mfrom: (1395)
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: robertc@robertcollins.net-20050930025451-47b9e412202be44b
symlink and weaves, whaddya know

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tree classes, representing directory at point in time.
18
18
"""
19
19
 
20
 
from osutils import pumpfile, appendpath, fingerprint_file
21
20
import os
 
21
from cStringIO import StringIO
22
22
 
 
23
import bzrlib
23
24
from bzrlib.trace import mutter, note
24
 
from bzrlib.errors import BzrError
 
25
from bzrlib.errors import BzrError, BzrCheckError
 
26
from bzrlib.inventory import Inventory
 
27
from bzrlib.osutils import pumpfile, appendpath, fingerprint_file
25
28
 
26
 
import bzrlib
27
29
 
28
30
exporters = {}
29
31
 
66
68
 
67
69
    def _get_inventory(self):
68
70
        return self._inventory
 
71
    
 
72
    def get_file_by_path(self, path):
 
73
        return self.get_file(self._inventory.path2id(path))
69
74
 
70
75
    inventory = property(_get_inventory,
71
76
                         doc="Inventory of this Tree")
72
77
 
73
78
    def _check_retrieved(self, ie, f):
 
79
        if not __debug__:
 
80
            return  
74
81
        fp = fingerprint_file(f)
75
82
        f.seek(0)
76
83
        
88
95
                     "store is probably damaged/corrupt"])
89
96
 
90
97
 
91
 
    def print_file(self, fileid):
92
 
        """Print file with id `fileid` to stdout."""
 
98
    def print_file(self, file_id):
 
99
        """Print file with id `file_id` to stdout."""
93
100
        import sys
94
 
        pumpfile(self.get_file(fileid), sys.stdout)
 
101
        sys.stdout.write(self.get_file_text(file_id))
95
102
        
96
103
        
97
104
    def export(self, dest, format='dir', root=None):
115
122
           or at least passing a description to the constructor.
116
123
    """
117
124
    
118
 
    def __init__(self, store, inv):
119
 
        self._store = store
 
125
    def __init__(self, weave_store, inv, revision_id):
 
126
        self._weave_store = weave_store
120
127
        self._inventory = inv
 
128
        self._revision_id = revision_id
 
129
 
 
130
    def get_weave(self, file_id):
 
131
        return self._weave_store.get_weave(file_id)
 
132
 
 
133
 
 
134
    def get_file_lines(self, file_id):
 
135
        ie = self._inventory[file_id]
 
136
        weave = self.get_weave(file_id)
 
137
        return weave.get(ie.text_version)
 
138
        
 
139
 
 
140
    def get_file_text(self, file_id):
 
141
        return ''.join(self.get_file_lines(file_id))
 
142
 
121
143
 
122
144
    def get_file(self, file_id):
123
 
        ie = self._inventory[file_id]
124
 
        f = self._store[ie.text_id]
125
 
        mutter("  get fileid{%s} from %r" % (file_id, self))
126
 
        self._check_retrieved(ie, f)
127
 
        return f
 
145
        return StringIO(self.get_file_text(file_id))
128
146
 
129
147
    def get_file_size(self, file_id):
130
148
        return self._inventory[file_id].text_size
131
149
 
132
150
    def get_file_sha1(self, file_id):
133
151
        ie = self._inventory[file_id]
134
 
        return ie.text_sha1
 
152
        if ie.kind == "file":
 
153
            return ie.text_sha1
135
154
 
136
155
    def has_filename(self, filename):
137
156
        return bool(self.inventory.path2id(filename))
141
160
        for path, entry in self.inventory.iter_entries():
142
161
            yield path, 'V', entry.kind, entry.file_id
143
162
 
 
163
    def get_symlink_target(self, file_id):
 
164
        ie = self._inventory[file_id]
 
165
        return ie.symlink_target;
144
166
 
145
167
class EmptyTree(Tree):
146
 
    def __init__(self, root_id):
147
 
        from bzrlib.inventory import Inventory
148
 
        self._inventory = Inventory(root_id)
 
168
    def __init__(self):
 
169
        self._inventory = Inventory()
 
170
 
 
171
    def get_symlink_target(self, file_id):
 
172
        return None
149
173
 
150
174
    def has_filename(self, filename):
151
175
        return False
154
178
        if False:  # just to make it a generator
155
179
            yield None
156
180
    
 
181
    def __contains__(self, file_id):
 
182
        return file_id in self._inventory
 
183
 
 
184
    def get_file_sha1(self, file_id):
 
185
        assert self._inventory[file_id].kind == "root_directory"
 
186
        return None
 
187
 
 
188
 
157
189
 
158
190
 
159
191
######################################################################
248
280
            os.mkdir(fullpath)
249
281
        elif kind == 'file':
250
282
            pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
 
283
        elif kind == 'symlink':
 
284
            try:
 
285
                os.symlink(ie.symlink_target, fullpath)
 
286
            except OSError,e:
 
287
                raise BzrError("Failed to create symlink %r -> %r, error: %s" % (fullpath, ie.symlink_target, e))
251
288
        else:
252
289
            raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
253
290
        mutter("  export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))