/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-06-28 03:02:31 UTC
  • Revision ID: mbp@sourcefrog.net-20050628030231-d311e4ebcd467ef4
Merge John's import-speedup branch:

                                                                                         
  777 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:32 -0500
      revision-id: john@arbash-meinel.com-20050627032031-e82a50db3863b18e
      bzr selftest was not using the correct bzr

  776 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:22 -0500
      revision-id: john@arbash-meinel.com-20050627032021-c9f21fde989ddaee
      Add was using an old mutter

  775 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:02:33 -0500
      revision-id: john@arbash-meinel.com-20050627030233-9165cfe98fc63298
      Cleaned up to be less different

  774 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:54:53 -0500
      revision-id: john@arbash-meinel.com-20050627025452-4260d0e744edef43
      Allow BZR_PLUGIN_PATH='' to negate plugin loading.

  773 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:49:34 -0500
      revision-id: john@arbash-meinel.com-20050627024933-b7158f67b7b9eae5
      Finished the previous cleanup (allowing load_plugins to be called twice)

  772 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:45:08 -0500
      revision-id: john@arbash-meinel.com-20050627024508-723b1df510d196fc
      Work on making the tests pass. versioning.py is calling run_cmd directly, but plugins have been loaded.

  771 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:32:29 -0500
      revision-id: john@arbash-meinel.com-20050627023228-79972744d7c53e15
      Got it down a little bit more by removing import of tree and inventory.

  770 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:26:05 -0500
      revision-id: john@arbash-meinel.com-20050627022604-350b9773ef622f95
      Reducing the number of import from bzrlib/__init__.py and bzrlib/branch.py

  769 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:32:25 -0500
      revision-id: john@arbash-meinel.com-20050627013225-32dd044f10d23948
      Updated revision.py and xml.py to include SubElement.

  768 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:56 -0500
      revision-id: john@arbash-meinel.com-20050627010356-ee66919e1c377faf
      Minor typo

  767 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:13 -0500
      revision-id: john@arbash-meinel.com-20050627010312-40d024007eb85051
      Caching the import

  766 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:51:47 -0500
      revision-id: john@arbash-meinel.com-20050627005147-5281c99e48ed1834
      Created wrapper functions for lazy import of ElementTree

  765 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:46:37 -0500
      revision-id: john@arbash-meinel.com-20050627004636-bf432902004a94c5
      Removed all of the test imports of cElementTree

  764 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:43:59 -0500
      revision-id: john@arbash-meinel.com-20050627004358-d137fbe9570dd71b
      Trying to make bzr startup faster.

Show diffs side-by-side

added added

removed removed

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