100
100
# directories, etc etc.
102
102
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
103
'text_id', 'parent_id', 'children', ]
103
'text_id', 'parent_id', 'children',
104
'text_version', 'entry_version', 'symlink_target']
105
107
def __init__(self, file_id, name, kind, parent_id, text_id=None):
106
108
"""Create an InventoryEntry
117
119
Traceback (most recent call last):
118
120
BzrCheckError: InventoryEntry name 'src/hello.c' is invalid
122
assert isinstance(name, basestring), name
120
123
if '/' in name or '\\' in name:
121
124
raise BzrCheckError('InventoryEntry name %r is invalid' % name)
126
self.text_version = None
127
self.entry_version = None
123
128
self.text_sha1 = None
124
129
self.text_size = None
126
130
self.file_id = file_id
129
133
self.text_id = text_id
130
134
self.parent_id = parent_id
135
self.symlink_target = None
131
136
if kind == 'directory':
132
137
self.children = {}
133
138
elif kind == 'file':
140
elif kind == 'symlink':
136
143
raise BzrError("unhandled entry kind %r" % kind)
145
def read_symlink_target(self, path):
146
if self.kind == 'symlink':
148
self.symlink_target = os.readlink(path)
150
raise BzrError("os.readlink error, %s" % e)
140
152
def sorted_children(self):
141
153
l = self.children.items()
148
160
self.parent_id, text_id=self.text_id)
149
161
other.text_sha1 = self.text_sha1
150
162
other.text_size = self.text_size
163
other.symlink_target = self.symlink_target
151
164
# note that children are *not* copied; they're pulled across when
152
165
# others are added
165
def to_element(self):
166
"""Convert to XML element"""
167
from bzrlib.xml import Element
171
e.set('name', self.name)
172
e.set('file_id', self.file_id)
173
e.set('kind', self.kind)
175
if self.text_size != None:
176
e.set('text_size', '%d' % self.text_size)
178
for f in ['text_id', 'text_sha1']:
183
# to be conservative, we don't externalize the root pointers
184
# for now, leaving them as null in the xml form. in a future
185
# version it will be implied by nested elements.
186
if self.parent_id != ROOT_ID:
187
assert isinstance(self.parent_id, basestring)
188
e.set('parent_id', self.parent_id)
195
def from_element(cls, elt):
196
assert elt.tag == 'entry'
198
## original format inventories don't have a parent_id for
199
## nodes in the root directory, but it's cleaner to use one
201
parent_id = elt.get('parent_id')
202
if parent_id == None:
205
self = cls(elt.get('file_id'), elt.get('name'), elt.get('kind'), parent_id)
206
self.text_id = elt.get('text_id')
207
self.text_sha1 = elt.get('text_sha1')
209
## mutter("read inventoryentry: %r" % (elt.attrib))
211
v = elt.get('text_size')
212
self.text_size = v and int(v)
217
from_element = classmethod(from_element)
219
178
def __eq__(self, other):
220
179
if not isinstance(other, InventoryEntry):
221
180
return NotImplemented
223
182
return (self.file_id == other.file_id) \
224
183
and (self.name == other.name) \
184
and (other.symlink_target == self.symlink_target) \
225
185
and (self.text_sha1 == other.text_sha1) \
226
186
and (self.text_size == other.text_size) \
227
187
and (self.text_id == other.text_id) \
228
188
and (self.parent_id == other.parent_id) \
229
and (self.kind == other.kind)
189
and (self.kind == other.kind) \
190
and (self.text_version == other.text_version) \
191
and (self.entry_version == other.entry_version)
232
193
def __ne__(self, other):
233
194
return not (self == other)
416
377
"""Add entry to inventory.
418
379
To add a file to a branch ready to be committed, use Branch.add,
382
Returns the new entry object.
420
384
if entry.file_id in self._byid:
421
385
raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
440
404
def add_path(self, relpath, kind, file_id=None):
441
405
"""Add entry from a path.
443
The immediate parent must already be versioned"""
407
The immediate parent must already be versioned.
409
Returns the new entry object."""
444
410
from bzrlib.branch import gen_file_id
446
412
parts = bzrlib.osutils.splitpath(relpath)
487
453
del self[ie.parent_id].children[ie.name]
490
def to_element(self):
491
"""Convert to XML Element"""
492
from bzrlib.xml import Element
494
e = Element('inventory')
496
if self.root.file_id not in (None, ROOT_ID):
497
e.set('file_id', self.root.file_id)
498
for path, ie in self.iter_entries():
499
e.append(ie.to_element())
503
def from_element(cls, elt):
504
"""Construct from XML Element
506
>>> inv = Inventory()
507
>>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c', 'file', ROOT_ID))
508
InventoryEntry('foo.c-123981239', 'foo.c', kind='file', parent_id='TREE_ROOT')
509
>>> elt = inv.to_element()
510
>>> inv2 = Inventory.from_element(elt)
514
# XXXX: doctest doesn't run this properly under python2.3
515
assert elt.tag == 'inventory'
516
root_id = elt.get('file_id') or ROOT_ID
519
ie = InventoryEntry.from_element(e)
520
if ie.parent_id == ROOT_ID:
521
ie.parent_id = root_id
525
from_element = classmethod(from_element)
528
456
def __eq__(self, other):
529
457
"""Compare two sets by comparing their contents.