2
# -*- coding: UTF-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Inventories map files to their name in a revision."""
21
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
22
__author__ = "Martin Pool <mbp@canonical.com>"
24
import sys, os.path, types
27
from xml import XMLMixin
28
from ElementTree import ElementTree, Element
29
from errors import bailout
30
from osutils import uuid, quotefn, splitpath, joinpath, appendpath
31
from trace import mutter
33
class InventoryEntry(XMLMixin):
34
"""Description of a versioned file.
36
An InventoryEntry has the following fields, which are also
37
present in the XML inventory-entry element:
40
* *name*: (only the basename within the directory, must not
42
* *kind*: "directory" or "file"
43
* *directory_id*: (if absent/null means the branch root directory)
44
* *text_sha1*: only for files
45
* *text_size*: in bytes, only for files
46
* *text_id*: identifier for the text version, only for files
48
InventoryEntries can also exist inside a WorkingTree
49
inventory, in which case they are not yet bound to a
50
particular revision of the file. In that case the text_sha1,
51
text_size and text_id are absent.
56
>>> i.add(InventoryEntry('123', 'src', kind='directory'))
57
>>> i.add(InventoryEntry('2323', 'hello.c', parent_id='123'))
58
>>> for j in i.iter_entries():
61
('src', InventoryEntry('123', 'src', kind='directory', parent_id=None))
62
('src/hello.c', InventoryEntry('2323', 'hello.c', kind='file', parent_id='123'))
63
>>> i.add(InventoryEntry('2323', 'bye.c', parent_id='123'))
64
Traceback (most recent call last):
66
BzrError: ('inventory already contains entry with id {2323}', [])
67
>>> i.add(InventoryEntry('2324', 'bye.c', parent_id='123'))
68
>>> i.add(InventoryEntry('2325', 'wibble', parent_id='123', kind='directory'))
69
>>> i.path2id('src/wibble')
73
>>> i.add(InventoryEntry('2326', 'wibble.c', parent_id='2325'))
75
InventoryEntry('2326', 'wibble.c', kind='file', parent_id='2325')
76
>>> for j in i.iter_entries():
78
... assert i.path2id(j[0])
88
:todo: Maybe also keep the full path of the entry, and the children?
89
But those depend on its position within a particular inventory, and
90
it would be nice not to need to hold the backpointer here.
92
def __init__(self, file_id, name, kind='file', text_id=None,
94
"""Create an InventoryEntry
96
The filename must be a single component, relative to the
97
parent directory; it cannot be a whole path or relative name.
99
>>> e = InventoryEntry('123', 'hello.c')
104
>>> e = InventoryEntry('123', 'src/hello.c')
105
Traceback (most recent call last):
106
BzrError: ("InventoryEntry name is not a simple filename: 'src/hello.c'", [])
109
if len(splitpath(name)) != 1:
110
bailout('InventoryEntry name is not a simple filename: %r'
113
self.file_id = file_id
115
assert kind in ['file', 'directory']
117
self.text_id = text_id
118
self.parent_id = parent_id
119
self.text_sha1 = None
120
self.text_size = None
124
other = InventoryEntry(self.file_id, self.name, self.kind,
125
self.text_id, self.parent_id)
126
other.text_sha1 = self.text_sha1
127
other.text_size = self.text_size
132
return ("%s(%r, %r, kind=%r, parent_id=%r)"
133
% (self.__class__.__name__,
140
def to_element(self):
141
"""Convert to XML element"""
144
e.set('name', self.name)
145
e.set('file_id', self.file_id)
146
e.set('kind', self.kind)
148
if self.text_size is not None:
149
e.set('text_size', '%d' % self.text_size)
151
for f in ['text_id', 'text_sha1', 'parent_id']:
161
def from_element(cls, elt):
162
assert elt.tag == 'entry'
163
self = cls(elt.get('file_id'), elt.get('name'), elt.get('kind'))
164
self.text_id = elt.get('text_id')
165
self.text_sha1 = elt.get('text_sha1')
166
self.parent_id = elt.get('parent_id')
168
## mutter("read inventoryentry: %r" % (elt.attrib))
170
v = elt.get('text_size')
171
self.text_size = v and int(v)
176
from_element = classmethod(from_element)
178
def __cmp__(self, other):
181
if not isinstance(other, InventoryEntry):
182
return NotImplemented
184
return cmp(self.file_id, other.file_id) \
185
or cmp(self.name, other.name) \
186
or cmp(self.text_sha1, other.text_sha1) \
187
or cmp(self.text_size, other.text_size) \
188
or cmp(self.text_id, other.text_id) \
189
or cmp(self.parent_id, other.parent_id) \
190
or cmp(self.kind, other.kind)
194
class Inventory(XMLMixin):
195
"""Inventory of versioned files in a tree.
197
An Inventory acts like a set of InventoryEntry items. You can
198
also look files up by their file_id or name.
200
May be read from and written to a metadata file in a tree. To
201
manipulate the inventory (for example to add a file), it is read
202
in, modified, and then written back out.
204
The inventory represents a typical unix file tree, with
205
directories containing files and subdirectories. We never store
206
the full path to a file, because renaming a directory implicitly
207
moves all of its contents. This class internally maintains a
208
lookup tree that allows the children under a directory to be
211
InventoryEntry objects must not be modified after they are
214
>>> inv = Inventory()
215
>>> inv.write_xml(sys.stdout)
218
>>> inv.add(InventoryEntry('123-123', 'hello.c'))
219
>>> inv['123-123'].name
221
>>> for file_id in inv: print file_id
225
May be treated as an iterator or set to look up file ids:
227
>>> bool(inv.path2id('hello.c'))
232
May also look up by name:
234
>>> [x[0] for x in inv.iter_entries()]
237
>>> inv.write_xml(sys.stdout)
239
<entry file_id="123-123" kind="file" name="hello.c" />
244
## TODO: Clear up handling of files in subdirectories; we probably
245
## do want to be able to just look them up by name but this
246
## probably means gradually walking down the path, looking up as we go.
248
## TODO: Make sure only canonical filenames are stored.
250
## TODO: Do something sensible about the possible collisions on
251
## case-losing filesystems. Perhaps we should just always forbid
254
## _tree should probably just be stored as
255
## InventoryEntry._children on each directory.
258
"""Create or read an inventory.
260
If a working directory is specified, the inventory is read
261
from there. If the file is specified, read from that. If not,
262
the inventory is created empty.
266
# _tree is indexed by parent_id; at each level a map from name
267
# to ie. The None entry is the root.
268
self._tree = {None: {}}
272
return iter(self._byid)
276
"""Returns number of entries."""
277
return len(self._byid)
280
def iter_entries(self, parent_id=None):
281
"""Return (path, entry) pairs, in order by name."""
282
kids = self._tree[parent_id].items()
284
for name, ie in kids:
286
if ie.kind == 'directory':
287
for cn, cie in self.iter_entries(parent_id=ie.file_id):
288
yield joinpath([name, cn]), cie
291
def directories(self, include_root=True):
292
"""Return (path, entry) pairs for all directories.
296
for path, entry in self.iter_entries():
297
if entry.kind == 'directory':
302
def children(self, parent_id):
303
"""Return entries that are direct children of parent_id."""
304
return self._tree[parent_id]
308
# TODO: return all paths and entries
311
def __contains__(self, file_id):
312
"""True if this entry contains a file with given id.
314
>>> inv = Inventory()
315
>>> inv.add(InventoryEntry('123', 'foo.c'))
321
return file_id in self._byid
324
def __getitem__(self, file_id):
325
"""Return the entry for given file_id.
327
>>> inv = Inventory()
328
>>> inv.add(InventoryEntry('123123', 'hello.c'))
329
>>> inv['123123'].name
332
return self._byid[file_id]
335
def add(self, entry):
336
"""Add entry to inventory.
338
To add a file to a branch ready to be committed, use Branch.add,
340
if entry.file_id in self:
341
bailout("inventory already contains entry with id {%s}" % entry.file_id)
343
if entry.parent_id != None:
344
if entry.parent_id not in self:
345
bailout("parent_id %s of new entry not found in inventory"
348
if self._tree[entry.parent_id].has_key(entry.name):
349
bailout("%s is already versioned"
350
% appendpath(self.id2path(entry.parent_id), entry.name))
352
self._byid[entry.file_id] = entry
353
self._tree[entry.parent_id][entry.name] = entry
355
if entry.kind == 'directory':
356
self._tree[entry.file_id] = {}
359
def __delitem__(self, file_id):
360
"""Remove entry by id.
362
>>> inv = Inventory()
363
>>> inv.add(InventoryEntry('123', 'foo.c'))
372
assert self._tree[ie.parent_id][ie.name] == ie
374
# TODO: Test deleting all children; maybe hoist to a separate
376
if ie.kind == 'directory':
377
for cie in self._tree[file_id].values():
378
del self[cie.file_id]
379
del self._tree[file_id]
381
del self._byid[file_id]
382
del self._tree[ie.parent_id][ie.name]
386
return Set(self._byid)
389
def to_element(self):
390
"""Convert to XML Element"""
391
e = Element('inventory')
393
for path, ie in self.iter_entries():
394
e.append(ie.to_element())
398
def from_element(cls, elt):
399
"""Construct from XML Element
401
>>> inv = Inventory()
402
>>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c'))
403
>>> elt = inv.to_element()
404
>>> inv2 = Inventory.from_element(elt)
408
assert elt.tag == 'inventory'
411
o.add(InventoryEntry.from_element(e))
414
from_element = classmethod(from_element)
417
def __cmp__(self, other):
418
"""Compare two sets by comparing their contents.
424
>>> i1.add(InventoryEntry('123', 'foo'))
427
>>> i2.add(InventoryEntry('123', 'foo'))
434
if not isinstance(other, Inventory):
435
return NotImplemented
437
if self.id_set() ^ other.id_set():
440
for file_id in self._byid:
441
c = cmp(self[file_id], other[file_id])
447
def id2path(self, file_id):
448
"""Return as a list the path to file_id."""
450
while file_id != None:
453
file_id = ie.parent_id
458
def path2id(self, name):
459
"""Walk down through directories to return entry of last component.
461
names may be either a list of path components, or a single
462
string, in which case it is automatically split.
464
This returns the entry of the last component in the path,
465
which may be either a file or a directory.
467
assert isinstance(name, types.StringTypes)
470
for f in splitpath(name):
472
cie = self._tree[parent_id][f]
474
parent_id = cie.file_id
482
def get_child(self, parent_id, child_name):
483
return self._tree[parent_id].get(child_name)
486
def has_filename(self, names):
487
return bool(self.path2id(names))
490
def has_id(self, file_id):
491
assert isinstance(file_id, str)
492
return self._byid.has_key(file_id)
496
if __name__ == '__main__':
497
import doctest, inventory
498
doctest.testmod(inventory)