~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-06-24 11:04:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050624110455-b0e54cd5f96691e5
- better quotefn for windows: use doublequotes for strings with 
  strange characters, not backslashes
- new backup_file() routine

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
import sys, os.path, types, re
24
24
 
 
25
try:
 
26
    from cElementTree import Element, ElementTree, SubElement
 
27
except ImportError:
 
28
    from elementtree.ElementTree import Element, ElementTree, SubElement
 
29
 
 
30
from bzrlib.xml import XMLMixin
 
31
from bzrlib.errors import BzrError, BzrCheckError
 
32
 
25
33
import bzrlib
26
 
from bzrlib.errors import BzrError, BzrCheckError
27
 
 
28
34
from bzrlib.osutils import uuid, quotefn, splitpath, joinpath, appendpath
29
35
from bzrlib.trace import mutter
30
36
 
31
 
class InventoryEntry(object):
 
37
class InventoryEntry(XMLMixin):
32
38
    """Description of a versioned file.
33
39
 
34
40
    An InventoryEntry has the following fields, which are also
92
98
    # TODO: split InventoryEntry into subclasses for files,
93
99
    # directories, etc etc.
94
100
 
95
 
    __slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
96
 
                 'text_id', 'parent_id', 'children', ]
97
 
 
 
101
    text_sha1 = None
 
102
    text_size = None
 
103
    
98
104
    def __init__(self, file_id, name, kind, parent_id, text_id=None):
99
105
        """Create an InventoryEntry
100
106
        
113
119
        if '/' in name or '\\' in name:
114
120
            raise BzrCheckError('InventoryEntry name %r is invalid' % name)
115
121
        
116
 
        self.text_sha1 = None
117
 
        self.text_size = None
118
 
    
119
122
        self.file_id = file_id
120
123
        self.name = name
121
124
        self.kind = kind
157
160
    
158
161
    def to_element(self):
159
162
        """Convert to XML element"""
160
 
        from bzrlib.xml import Element
161
 
        
162
163
        e = Element('entry')
163
164
 
164
165
        e.set('name', self.name)
247
248
 
248
249
 
249
250
 
250
 
class Inventory(object):
 
251
class Inventory(XMLMixin):
251
252
    """Inventory of versioned files in a tree.
252
253
 
253
254
    This describes which file_id is present at each point in the tree,
265
266
    inserted, other than through the Inventory API.
266
267
 
267
268
    >>> inv = Inventory()
 
269
    >>> inv.write_xml(sys.stdout)
 
270
    <inventory>
 
271
    </inventory>
268
272
    >>> inv.add(InventoryEntry('123-123', 'hello.c', 'file', ROOT_ID))
269
273
    >>> inv['123-123'].name
270
274
    'hello.c'
280
284
 
281
285
    >>> [x[0] for x in inv.iter_entries()]
282
286
    ['hello.c']
283
 
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
284
 
    >>> inv.add(InventoryEntry('123-123', 'hello.c', 'file', ROOT_ID))
 
287
    
 
288
    >>> inv.write_xml(sys.stdout)
 
289
    <inventory>
 
290
    <entry file_id="123-123" kind="file" name="hello.c" />
 
291
    </inventory>
 
292
 
285
293
    """
286
 
    def __init__(self, root_id=ROOT_ID):
 
294
    def __init__(self):
287
295
        """Create or read an inventory.
288
296
 
289
297
        If a working directory is specified, the inventory is read
293
301
        The inventory is created with a default root directory, with
294
302
        an id of None.
295
303
        """
296
 
        # We are letting Branch(init=True) create a unique inventory
297
 
        # root id. Rather than generating a random one here.
298
 
        #if root_id is None:
299
 
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
300
 
        self.root = RootEntry(root_id)
 
304
        self.root = RootEntry(ROOT_ID)
301
305
        self._byid = {self.root.file_id: self.root}
302
306
 
303
307
 
409
413
        if entry.file_id in self._byid:
410
414
            raise BzrError("inventory already contains entry with id {%s}" % entry.file_id)
411
415
 
412
 
        if entry.parent_id == ROOT_ID or entry.parent_id is None:
413
 
            entry.parent_id = self.root.file_id
414
 
 
415
416
        try:
416
417
            parent = self._byid[entry.parent_id]
417
418
        except KeyError:
436
437
            raise BzrError("cannot re-add root of inventory")
437
438
 
438
439
        if file_id == None:
439
 
            from bzrlib.branch import gen_file_id
440
 
            file_id = gen_file_id(relpath)
 
440
            file_id = bzrlib.branch.gen_file_id(relpath)
441
441
 
442
442
        parent_path = parts[:-1]
443
443
        parent_id = self.path2id(parent_path)
477
477
 
478
478
    def to_element(self):
479
479
        """Convert to XML Element"""
480
 
        from bzrlib.xml import Element
481
 
        
482
480
        e = Element('inventory')
483
481
        e.text = '\n'
484
 
        if self.root.file_id not in (None, ROOT_ID):
485
 
            e.set('file_id', self.root.file_id)
486
482
        for path, ie in self.iter_entries():
487
483
            e.append(ie.to_element())
488
484
        return e
490
486
 
491
487
    def from_element(cls, elt):
492
488
        """Construct from XML Element
493
 
        
 
489
 
494
490
        >>> inv = Inventory()
495
491
        >>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c', 'file', ROOT_ID))
496
492
        >>> elt = inv.to_element()
498
494
        >>> inv2 == inv
499
495
        True
500
496
        """
501
 
        # XXXX: doctest doesn't run this properly under python2.3
502
497
        assert elt.tag == 'inventory'
503
 
        root_id = elt.get('file_id') or ROOT_ID
504
 
        o = cls(root_id)
 
498
        o = cls()
505
499
        for e in elt:
506
 
            ie = InventoryEntry.from_element(e)
507
 
            if ie.parent_id == ROOT_ID:
508
 
                ie.parent_id = root_id
509
 
            o.add(ie)
 
500
            o.add(InventoryEntry.from_element(e))
510
501
        return o
511
502
        
512
503
    from_element = classmethod(from_element)
568
559
        """Return as a list the path to file_id."""
569
560
 
570
561
        # get all names, skipping root
571
 
        p = [self._byid[fid].name for fid in self.get_idpath(file_id)[1:]]
 
562
        p = [self[fid].name for fid in self.get_idpath(file_id)[1:]]
572
563
        return os.sep.join(p)
573
564
            
574
565