/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
138 by mbp at sourcefrog
remove parallel tree from inventory;
1
# (C) 2005 Canonical Ltd
1 by mbp at sourcefrog
import from baz patch-364
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Inventories map files to their name in a revision."""
18
120 by mbp at sourcefrog
more check functions
19
# TODO: Maybe store inventory_id in the file?  Not really needed.
1 by mbp at sourcefrog
import from baz patch-364
20
21
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
22
__author__ = "Martin Pool <mbp@canonical.com>"
23
24
import sys, os.path, types
25
from sets import Set
26
7 by mbp at sourcefrog
depend only on regular ElementTree installation
27
try:
28
    from cElementTree import Element, ElementTree, SubElement
29
except ImportError:
27 by mbp at sourcefrog
- fix up use of ElementTree without cElementTree
30
    from elementtree.ElementTree import Element, ElementTree, SubElement
7 by mbp at sourcefrog
depend only on regular ElementTree installation
31
1 by mbp at sourcefrog
import from baz patch-364
32
from xml import XMLMixin
33
from errors import bailout
70 by mbp at sourcefrog
Prepare for smart recursive add.
34
35
import bzrlib
36
from bzrlib.osutils import uuid, quotefn, splitpath, joinpath, appendpath
37
from bzrlib.trace import mutter
1 by mbp at sourcefrog
import from baz patch-364
38
39
class InventoryEntry(XMLMixin):
40
    """Description of a versioned file.
41
42
    An InventoryEntry has the following fields, which are also
43
    present in the XML inventory-entry element:
44
45
    * *file_id*
46
    * *name*: (only the basename within the directory, must not
47
      contain slashes)
48
    * *kind*: "directory" or "file"
49
    * *directory_id*: (if absent/null means the branch root directory)
50
    * *text_sha1*: only for files
51
    * *text_size*: in bytes, only for files 
52
    * *text_id*: identifier for the text version, only for files
53
54
    InventoryEntries can also exist inside a WorkingTree
55
    inventory, in which case they are not yet bound to a
56
    particular revision of the file.  In that case the text_sha1,
57
    text_size and text_id are absent.
58
59
60
    >>> i = Inventory()
61
    >>> i.path2id('')
62
    >>> i.add(InventoryEntry('123', 'src', kind='directory'))
63
    >>> i.add(InventoryEntry('2323', 'hello.c', parent_id='123'))
64
    >>> for j in i.iter_entries():
65
    ...   print j
66
    ... 
67
    ('src', InventoryEntry('123', 'src', kind='directory', parent_id=None))
68
    ('src/hello.c', InventoryEntry('2323', 'hello.c', kind='file', parent_id='123'))
69
    >>> i.add(InventoryEntry('2323', 'bye.c', parent_id='123'))
70
    Traceback (most recent call last):
71
    ...
72
    BzrError: ('inventory already contains entry with id {2323}', [])
73
    >>> i.add(InventoryEntry('2324', 'bye.c', parent_id='123'))
74
    >>> i.add(InventoryEntry('2325', 'wibble', parent_id='123', kind='directory'))
75
    >>> i.path2id('src/wibble')
76
    '2325'
77
    >>> '2325' in i
78
    True
79
    >>> i.add(InventoryEntry('2326', 'wibble.c', parent_id='2325'))
80
    >>> i['2326']
81
    InventoryEntry('2326', 'wibble.c', kind='file', parent_id='2325')
82
    >>> for j in i.iter_entries():
83
    ...     print j[0]
84
    ...     assert i.path2id(j[0])
85
    ... 
86
    src
87
    src/bye.c
88
    src/hello.c
89
    src/wibble
90
    src/wibble/wibble.c
91
    >>> i.id2path('2326')
92
    'src/wibble/wibble.c'
93
94
    :todo: Maybe also keep the full path of the entry, and the children?
95
           But those depend on its position within a particular inventory, and
96
           it would be nice not to need to hold the backpointer here.
97
    """
98
    def __init__(self, file_id, name, kind='file', text_id=None,
99
                 parent_id=None):
100
        """Create an InventoryEntry
101
        
102
        The filename must be a single component, relative to the
103
        parent directory; it cannot be a whole path or relative name.
104
105
        >>> e = InventoryEntry('123', 'hello.c')
106
        >>> e.name
107
        'hello.c'
108
        >>> e.file_id
109
        '123'
110
        >>> e = InventoryEntry('123', 'src/hello.c')
111
        Traceback (most recent call last):
112
        BzrError: ("InventoryEntry name is not a simple filename: 'src/hello.c'", [])
113
        """
114
        
115
        if len(splitpath(name)) != 1:
116
            bailout('InventoryEntry name is not a simple filename: %r'
117
                    % name)
118
        
119
        self.file_id = file_id
120
        self.name = name
121
        assert kind in ['file', 'directory']
122
        self.kind = kind
123
        self.text_id = text_id
124
        self.parent_id = parent_id
125
        self.text_sha1 = None
126
        self.text_size = None
138 by mbp at sourcefrog
remove parallel tree from inventory;
127
        if kind == 'directory':
128
            self.children = {}
1 by mbp at sourcefrog
import from baz patch-364
129
130
156 by mbp at sourcefrog
new "directories" command
131
    def sorted_children(self):
132
        l = self.children.items()
133
        l.sort()
134
        return l
135
136
1 by mbp at sourcefrog
import from baz patch-364
137
    def copy(self):
138
        other = InventoryEntry(self.file_id, self.name, self.kind,
139
                               self.text_id, self.parent_id)
140
        other.text_sha1 = self.text_sha1
141
        other.text_size = self.text_size
142
        return other
143
144
145
    def __repr__(self):
146
        return ("%s(%r, %r, kind=%r, parent_id=%r)"
147
                % (self.__class__.__name__,
148
                   self.file_id,
149
                   self.name,
150
                   self.kind,
151
                   self.parent_id))
152
153
    
154
    def to_element(self):
155
        """Convert to XML element"""
156
        e = Element('entry')
157
158
        e.set('name', self.name)
159
        e.set('file_id', self.file_id)
160
        e.set('kind', self.kind)
161
162
        if self.text_size is not None:
163
            e.set('text_size', '%d' % self.text_size)
164
            
165
        for f in ['text_id', 'text_sha1', 'parent_id']:
166
            v = getattr(self, f)
167
            if v is not None:
168
                e.set(f, v)
169
170
        e.tail = '\n'
171
            
172
        return e
173
174
175
    def from_element(cls, elt):
176
        assert elt.tag == 'entry'
177
        self = cls(elt.get('file_id'), elt.get('name'), elt.get('kind'))
178
        self.text_id = elt.get('text_id')
179
        self.text_sha1 = elt.get('text_sha1')
180
        self.parent_id = elt.get('parent_id')
181
        
182
        ## mutter("read inventoryentry: %r" % (elt.attrib))
183
184
        v = elt.get('text_size')
185
        self.text_size = v and int(v)
186
187
        return self
188
            
189
190
    from_element = classmethod(from_element)
191
192
    def __cmp__(self, other):
193
        if self is other:
194
            return 0
195
        if not isinstance(other, InventoryEntry):
196
            return NotImplemented
197
198
        return cmp(self.file_id, other.file_id) \
199
               or cmp(self.name, other.name) \
200
               or cmp(self.text_sha1, other.text_sha1) \
201
               or cmp(self.text_size, other.text_size) \
202
               or cmp(self.text_id, other.text_id) \
203
               or cmp(self.parent_id, other.parent_id) \
204
               or cmp(self.kind, other.kind)
205
206
207
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
208
class RootEntry(InventoryEntry):
209
    def __init__(self, file_id):
210
        self.file_id = file_id
211
        self.children = {}
212
        self.kind = 'root_directory'
213
        self.parent_id = None
156 by mbp at sourcefrog
new "directories" command
214
        self.name = ''
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
215
216
    def __cmp__(self, other):
217
        if self is other:
218
            return 0
219
        if not isinstance(other, RootEntry):
220
            return NotImplemented
221
        return cmp(self.file_id, other.file_id) \
222
               or cmp(self.children, other.children)
223
224
225
1 by mbp at sourcefrog
import from baz patch-364
226
class Inventory(XMLMixin):
227
    """Inventory of versioned files in a tree.
228
229
    An Inventory acts like a set of InventoryEntry items.  You can
230
    also look files up by their file_id or name.
231
    
232
    May be read from and written to a metadata file in a tree.  To
233
    manipulate the inventory (for example to add a file), it is read
234
    in, modified, and then written back out.
235
236
    The inventory represents a typical unix file tree, with
237
    directories containing files and subdirectories.  We never store
238
    the full path to a file, because renaming a directory implicitly
239
    moves all of its contents.  This class internally maintains a
240
    lookup tree that allows the children under a directory to be
241
    returned quickly.
242
243
    InventoryEntry objects must not be modified after they are
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
244
    inserted, other than through the Inventory API.
1 by mbp at sourcefrog
import from baz patch-364
245
246
    >>> inv = Inventory()
247
    >>> inv.write_xml(sys.stdout)
248
    <inventory>
249
    </inventory>
250
    >>> inv.add(InventoryEntry('123-123', 'hello.c'))
251
    >>> inv['123-123'].name
252
    'hello.c'
253
254
    May be treated as an iterator or set to look up file ids:
255
    
256
    >>> bool(inv.path2id('hello.c'))
257
    True
258
    >>> '123-123' in inv
259
    True
260
261
    May also look up by name:
262
263
    >>> [x[0] for x in inv.iter_entries()]
264
    ['hello.c']
265
    
266
    >>> inv.write_xml(sys.stdout)
267
    <inventory>
268
    <entry file_id="123-123" kind="file" name="hello.c" />
269
    </inventory>
270
271
    """
272
273
    ## TODO: Make sure only canonical filenames are stored.
274
275
    ## TODO: Do something sensible about the possible collisions on
276
    ## case-losing filesystems.  Perhaps we should just always forbid
277
    ## such collisions.
278
138 by mbp at sourcefrog
remove parallel tree from inventory;
279
    ## TODO: No special cases for root, rather just give it a file id
280
    ## like everything else.
281
282
    ## TODO: Probably change XML serialization to use nesting
1 by mbp at sourcefrog
import from baz patch-364
283
284
    def __init__(self):
285
        """Create or read an inventory.
286
287
        If a working directory is specified, the inventory is read
288
        from there.  If the file is specified, read from that. If not,
289
        the inventory is created empty.
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
290
291
        The inventory is created with a default root directory, with
292
        an id of None.
1 by mbp at sourcefrog
import from baz patch-364
293
        """
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
294
        self.root = RootEntry(None)
295
        self._byid = {None: self.root}
1 by mbp at sourcefrog
import from baz patch-364
296
297
298
    def __iter__(self):
299
        return iter(self._byid)
300
301
302
    def __len__(self):
303
        """Returns number of entries."""
304
        return len(self._byid)
305
306
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
307
    def iter_entries(self, from_dir=None):
1 by mbp at sourcefrog
import from baz patch-364
308
        """Return (path, entry) pairs, in order by name."""
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
309
        if from_dir == None:
310
            assert self.root
311
            from_dir = self.root
312
        elif isinstance(from_dir, basestring):
313
            from_dir = self._byid[from_dir]
314
            
315
        kids = from_dir.children.items()
1 by mbp at sourcefrog
import from baz patch-364
316
        kids.sort()
317
        for name, ie in kids:
318
            yield name, ie
319
            if ie.kind == 'directory':
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
320
                for cn, cie in self.iter_entries(from_dir=ie.file_id):
321
                    yield '/'.join((name, cn)), cie
322
                    
323
324
325
    def directories(self, from_dir=None):
1 by mbp at sourcefrog
import from baz patch-364
326
        """Return (path, entry) pairs for all directories.
327
        """
156 by mbp at sourcefrog
new "directories" command
328
        def descend(parent_ie):
329
            parent_name = parent_ie.name
330
            if parent_name == '':
331
                parent_name = '.'
332
            yield parent_name, parent_ie
333
334
            # directory children in sorted order
335
            dn = []
336
            for ie in parent_ie.children.itervalues():
337
                if ie.kind == 'directory':
338
                    dn.append((ie.name, ie))
339
            dn.sort()
340
            
341
            for name, child_ie in dn:
342
                for sub_name, sub_ie in descend(child_ie):
343
                    yield appendpath(parent_name, sub_name), sub_ie
344
345
        for name, ie in descend(self.root):
346
            yield name, ie
1 by mbp at sourcefrog
import from baz patch-364
347
        
348
349
350
    def __contains__(self, file_id):
351
        """True if this entry contains a file with given id.
352
353
        >>> inv = Inventory()
354
        >>> inv.add(InventoryEntry('123', 'foo.c'))
355
        >>> '123' in inv
356
        True
357
        >>> '456' in inv
358
        False
359
        """
360
        return file_id in self._byid
361
362
363
    def __getitem__(self, file_id):
364
        """Return the entry for given file_id.
365
366
        >>> inv = Inventory()
367
        >>> inv.add(InventoryEntry('123123', 'hello.c'))
368
        >>> inv['123123'].name
369
        'hello.c'
370
        """
371
        return self._byid[file_id]
372
373
138 by mbp at sourcefrog
remove parallel tree from inventory;
374
    def get_child(self, parent_id, filename):
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
375
        return self[parent_id].children.get(filename)
138 by mbp at sourcefrog
remove parallel tree from inventory;
376
377
1 by mbp at sourcefrog
import from baz patch-364
378
    def add(self, entry):
379
        """Add entry to inventory.
380
381
        To add  a file to a branch ready to be committed, use Branch.add,
382
        which calls this."""
139 by mbp at sourcefrog
simplified/faster Inventory.add
383
        if entry.file_id in self._byid:
1 by mbp at sourcefrog
import from baz patch-364
384
            bailout("inventory already contains entry with id {%s}" % entry.file_id)
385
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
386
        try:
387
            parent = self._byid[entry.parent_id]
388
        except KeyError:
389
            bailout("parent_id %r not in inventory" % entry.parent_id)
139 by mbp at sourcefrog
simplified/faster Inventory.add
390
391
        if parent.children.has_key(entry.name):
140 by mbp at sourcefrog
fix error message for repeated add
392
            bailout("%s is already versioned" %
393
                    appendpath(self.id2path(parent.file_id), entry.name))
1 by mbp at sourcefrog
import from baz patch-364
394
395
        self._byid[entry.file_id] = entry
139 by mbp at sourcefrog
simplified/faster Inventory.add
396
        parent.children[entry.name] = entry
1 by mbp at sourcefrog
import from baz patch-364
397
398
70 by mbp at sourcefrog
Prepare for smart recursive add.
399
    def add_path(self, relpath, kind, file_id=None):
400
        """Add entry from a path.
401
402
        The immediate parent must already be versioned"""
403
        parts = bzrlib.osutils.splitpath(relpath)
404
        if len(parts) == 0:
405
            bailout("cannot re-add root of inventory")
406
407
        if file_id is None:
408
            file_id = bzrlib.branch.gen_file_id(relpath)
409
410
        parent_id = self.path2id(parts[:-1])
411
        ie = InventoryEntry(file_id, parts[-1],
412
                            kind=kind, parent_id=parent_id)
413
        return self.add(ie)
414
415
1 by mbp at sourcefrog
import from baz patch-364
416
    def __delitem__(self, file_id):
417
        """Remove entry by id.
418
419
        >>> inv = Inventory()
420
        >>> inv.add(InventoryEntry('123', 'foo.c'))
421
        >>> '123' in inv
422
        True
423
        >>> del inv['123']
424
        >>> '123' in inv
425
        False
426
        """
427
        ie = self[file_id]
428
138 by mbp at sourcefrog
remove parallel tree from inventory;
429
        assert self[ie.parent_id].children[ie.name] == ie
1 by mbp at sourcefrog
import from baz patch-364
430
        
431
        # TODO: Test deleting all children; maybe hoist to a separate
432
        # deltree method?
433
        if ie.kind == 'directory':
138 by mbp at sourcefrog
remove parallel tree from inventory;
434
            for cie in ie.children.values():
1 by mbp at sourcefrog
import from baz patch-364
435
                del self[cie.file_id]
138 by mbp at sourcefrog
remove parallel tree from inventory;
436
            del ie.children
1 by mbp at sourcefrog
import from baz patch-364
437
438
        del self._byid[file_id]
138 by mbp at sourcefrog
remove parallel tree from inventory;
439
        del self[ie.parent_id].children[ie.name]
1 by mbp at sourcefrog
import from baz patch-364
440
441
442
    def id_set(self):
443
        return Set(self._byid)
444
445
446
    def to_element(self):
447
        """Convert to XML Element"""
448
        e = Element('inventory')
449
        e.text = '\n'
450
        for path, ie in self.iter_entries():
451
            e.append(ie.to_element())
452
        return e
453
    
454
455
    def from_element(cls, elt):
456
        """Construct from XML Element
457
458
        >>> inv = Inventory()
459
        >>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c'))
460
        >>> elt = inv.to_element()
461
        >>> inv2 = Inventory.from_element(elt)
462
        >>> inv2 == inv
463
        True
464
        """
465
        assert elt.tag == 'inventory'
466
        o = cls()
467
        for e in elt:
468
            o.add(InventoryEntry.from_element(e))
469
        return o
470
        
471
    from_element = classmethod(from_element)
472
473
474
    def __cmp__(self, other):
475
        """Compare two sets by comparing their contents.
476
477
        >>> i1 = Inventory()
478
        >>> i2 = Inventory()
479
        >>> i1 == i2
480
        True
481
        >>> i1.add(InventoryEntry('123', 'foo'))
482
        >>> i1 == i2
483
        False
484
        >>> i2.add(InventoryEntry('123', 'foo'))
485
        >>> i1 == i2
486
        True
487
        """
488
        if self is other:
489
            return 0
490
        
491
        if not isinstance(other, Inventory):
492
            return NotImplemented
493
494
        if self.id_set() ^ other.id_set():
495
            return 1
496
497
        for file_id in self._byid:
498
            c = cmp(self[file_id], other[file_id])
499
            if c: return c
500
501
        return 0
502
503
504
    def id2path(self, file_id):
505
        """Return as a list the path to file_id."""
506
        p = []
507
        while file_id != None:
149 by mbp at sourcefrog
experiment with new nested inventory file format
508
            ie = self._byid[file_id]
509
            p.insert(0, ie.name)
1 by mbp at sourcefrog
import from baz patch-364
510
            file_id = ie.parent_id
149 by mbp at sourcefrog
experiment with new nested inventory file format
511
        return '/'.join(p)
1 by mbp at sourcefrog
import from baz patch-364
512
            
513
514
515
    def path2id(self, name):
516
        """Walk down through directories to return entry of last component.
517
518
        names may be either a list of path components, or a single
519
        string, in which case it is automatically split.
520
521
        This returns the entry of the last component in the path,
522
        which may be either a file or a directory.
523
        """
70 by mbp at sourcefrog
Prepare for smart recursive add.
524
        if isinstance(name, types.StringTypes):
525
            name = splitpath(name)
1 by mbp at sourcefrog
import from baz patch-364
526
138 by mbp at sourcefrog
remove parallel tree from inventory;
527
        parent = self[None]
70 by mbp at sourcefrog
Prepare for smart recursive add.
528
        for f in name:
1 by mbp at sourcefrog
import from baz patch-364
529
            try:
138 by mbp at sourcefrog
remove parallel tree from inventory;
530
                cie = parent.children[f]
1 by mbp at sourcefrog
import from baz patch-364
531
                assert cie.name == f
138 by mbp at sourcefrog
remove parallel tree from inventory;
532
                parent = cie
1 by mbp at sourcefrog
import from baz patch-364
533
            except KeyError:
534
                # or raise an error?
535
                return None
536
138 by mbp at sourcefrog
remove parallel tree from inventory;
537
        return parent.file_id
1 by mbp at sourcefrog
import from baz patch-364
538
539
540
    def has_filename(self, names):
541
        return bool(self.path2id(names))
542
543
544
    def has_id(self, file_id):
545
        return self._byid.has_key(file_id)
546
547
548
70 by mbp at sourcefrog
Prepare for smart recursive add.
549
550
1 by mbp at sourcefrog
import from baz patch-364
551
if __name__ == '__main__':
552
    import doctest, inventory
553
    doctest.testmod(inventory)