/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
131
    def copy(self):
132
        other = InventoryEntry(self.file_id, self.name, self.kind,
133
                               self.text_id, self.parent_id)
134
        other.text_sha1 = self.text_sha1
135
        other.text_size = self.text_size
136
        return other
137
138
139
    def __repr__(self):
140
        return ("%s(%r, %r, kind=%r, parent_id=%r)"
141
                % (self.__class__.__name__,
142
                   self.file_id,
143
                   self.name,
144
                   self.kind,
145
                   self.parent_id))
146
147
    
148
    def to_element(self):
149
        """Convert to XML element"""
150
        e = Element('entry')
151
152
        e.set('name', self.name)
153
        e.set('file_id', self.file_id)
154
        e.set('kind', self.kind)
155
156
        if self.text_size is not None:
157
            e.set('text_size', '%d' % self.text_size)
158
            
159
        for f in ['text_id', 'text_sha1', 'parent_id']:
160
            v = getattr(self, f)
161
            if v is not None:
162
                e.set(f, v)
163
164
        e.tail = '\n'
165
            
166
        return e
167
168
169
    def from_element(cls, elt):
170
        assert elt.tag == 'entry'
171
        self = cls(elt.get('file_id'), elt.get('name'), elt.get('kind'))
172
        self.text_id = elt.get('text_id')
173
        self.text_sha1 = elt.get('text_sha1')
174
        self.parent_id = elt.get('parent_id')
175
        
176
        ## mutter("read inventoryentry: %r" % (elt.attrib))
177
178
        v = elt.get('text_size')
179
        self.text_size = v and int(v)
180
181
        return self
182
            
183
184
    from_element = classmethod(from_element)
185
186
    def __cmp__(self, other):
187
        if self is other:
188
            return 0
189
        if not isinstance(other, InventoryEntry):
190
            return NotImplemented
191
192
        return cmp(self.file_id, other.file_id) \
193
               or cmp(self.name, other.name) \
194
               or cmp(self.text_sha1, other.text_sha1) \
195
               or cmp(self.text_size, other.text_size) \
196
               or cmp(self.text_id, other.text_id) \
197
               or cmp(self.parent_id, other.parent_id) \
198
               or cmp(self.kind, other.kind)
199
200
201
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
202
class RootEntry(InventoryEntry):
203
    def __init__(self, file_id):
204
        self.file_id = file_id
205
        self.children = {}
206
        self.kind = 'root_directory'
207
        self.parent_id = None
208
209
    def __cmp__(self, other):
210
        if self is other:
211
            return 0
212
        if not isinstance(other, RootEntry):
213
            return NotImplemented
214
        return cmp(self.file_id, other.file_id) \
215
               or cmp(self.children, other.children)
216
217
218
1 by mbp at sourcefrog
import from baz patch-364
219
class Inventory(XMLMixin):
220
    """Inventory of versioned files in a tree.
221
222
    An Inventory acts like a set of InventoryEntry items.  You can
223
    also look files up by their file_id or name.
224
    
225
    May be read from and written to a metadata file in a tree.  To
226
    manipulate the inventory (for example to add a file), it is read
227
    in, modified, and then written back out.
228
229
    The inventory represents a typical unix file tree, with
230
    directories containing files and subdirectories.  We never store
231
    the full path to a file, because renaming a directory implicitly
232
    moves all of its contents.  This class internally maintains a
233
    lookup tree that allows the children under a directory to be
234
    returned quickly.
235
236
    InventoryEntry objects must not be modified after they are
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
237
    inserted, other than through the Inventory API.
1 by mbp at sourcefrog
import from baz patch-364
238
239
    >>> inv = Inventory()
240
    >>> inv.write_xml(sys.stdout)
241
    <inventory>
242
    </inventory>
243
    >>> inv.add(InventoryEntry('123-123', 'hello.c'))
244
    >>> inv['123-123'].name
245
    'hello.c'
246
247
    May be treated as an iterator or set to look up file ids:
248
    
249
    >>> bool(inv.path2id('hello.c'))
250
    True
251
    >>> '123-123' in inv
252
    True
253
254
    May also look up by name:
255
256
    >>> [x[0] for x in inv.iter_entries()]
257
    ['hello.c']
258
    
259
    >>> inv.write_xml(sys.stdout)
260
    <inventory>
261
    <entry file_id="123-123" kind="file" name="hello.c" />
262
    </inventory>
263
264
    """
265
266
    ## TODO: Make sure only canonical filenames are stored.
267
268
    ## TODO: Do something sensible about the possible collisions on
269
    ## case-losing filesystems.  Perhaps we should just always forbid
270
    ## such collisions.
271
138 by mbp at sourcefrog
remove parallel tree from inventory;
272
    ## TODO: No special cases for root, rather just give it a file id
273
    ## like everything else.
274
275
    ## TODO: Probably change XML serialization to use nesting
1 by mbp at sourcefrog
import from baz patch-364
276
277
    def __init__(self):
278
        """Create or read an inventory.
279
280
        If a working directory is specified, the inventory is read
281
        from there.  If the file is specified, read from that. If not,
282
        the inventory is created empty.
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
283
284
        The inventory is created with a default root directory, with
285
        an id of None.
1 by mbp at sourcefrog
import from baz patch-364
286
        """
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
287
        self.root = RootEntry(None)
288
        self._byid = {None: self.root}
1 by mbp at sourcefrog
import from baz patch-364
289
290
291
    def __iter__(self):
292
        return iter(self._byid)
293
294
295
    def __len__(self):
296
        """Returns number of entries."""
297
        return len(self._byid)
298
299
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
300
    def iter_entries(self, from_dir=None):
1 by mbp at sourcefrog
import from baz patch-364
301
        """Return (path, entry) pairs, in order by name."""
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
302
        if from_dir == None:
303
            assert self.root
304
            from_dir = self.root
305
        elif isinstance(from_dir, basestring):
306
            from_dir = self._byid[from_dir]
307
            
308
        kids = from_dir.children.items()
1 by mbp at sourcefrog
import from baz patch-364
309
        kids.sort()
310
        for name, ie in kids:
311
            yield name, ie
312
            if ie.kind == 'directory':
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
313
                for cn, cie in self.iter_entries(from_dir=ie.file_id):
314
                    yield '/'.join((name, cn)), cie
315
                    
316
317
318
    def directories(self, from_dir=None):
1 by mbp at sourcefrog
import from baz patch-364
319
        """Return (path, entry) pairs for all directories.
320
        """
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
321
        assert self.root
322
        yield '', self.root
1 by mbp at sourcefrog
import from baz patch-364
323
        for path, entry in self.iter_entries():
324
            if entry.kind == 'directory':
325
                yield path, entry
326
        
327
328
329
    def __contains__(self, file_id):
330
        """True if this entry contains a file with given id.
331
332
        >>> inv = Inventory()
333
        >>> inv.add(InventoryEntry('123', 'foo.c'))
334
        >>> '123' in inv
335
        True
336
        >>> '456' in inv
337
        False
338
        """
339
        return file_id in self._byid
340
341
342
    def __getitem__(self, file_id):
343
        """Return the entry for given file_id.
344
345
        >>> inv = Inventory()
346
        >>> inv.add(InventoryEntry('123123', 'hello.c'))
347
        >>> inv['123123'].name
348
        'hello.c'
349
        """
350
        return self._byid[file_id]
351
352
138 by mbp at sourcefrog
remove parallel tree from inventory;
353
    def get_child(self, parent_id, filename):
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
354
        return self[parent_id].children.get(filename)
138 by mbp at sourcefrog
remove parallel tree from inventory;
355
356
1 by mbp at sourcefrog
import from baz patch-364
357
    def add(self, entry):
358
        """Add entry to inventory.
359
360
        To add  a file to a branch ready to be committed, use Branch.add,
361
        which calls this."""
139 by mbp at sourcefrog
simplified/faster Inventory.add
362
        if entry.file_id in self._byid:
1 by mbp at sourcefrog
import from baz patch-364
363
            bailout("inventory already contains entry with id {%s}" % entry.file_id)
364
155 by mbp at sourcefrog
add new explicit RootEntry to inventory (in-core only)
365
        try:
366
            parent = self._byid[entry.parent_id]
367
        except KeyError:
368
            bailout("parent_id %r not in inventory" % entry.parent_id)
139 by mbp at sourcefrog
simplified/faster Inventory.add
369
370
        if parent.children.has_key(entry.name):
140 by mbp at sourcefrog
fix error message for repeated add
371
            bailout("%s is already versioned" %
372
                    appendpath(self.id2path(parent.file_id), entry.name))
1 by mbp at sourcefrog
import from baz patch-364
373
374
        self._byid[entry.file_id] = entry
139 by mbp at sourcefrog
simplified/faster Inventory.add
375
        parent.children[entry.name] = entry
1 by mbp at sourcefrog
import from baz patch-364
376
377
70 by mbp at sourcefrog
Prepare for smart recursive add.
378
    def add_path(self, relpath, kind, file_id=None):
379
        """Add entry from a path.
380
381
        The immediate parent must already be versioned"""
382
        parts = bzrlib.osutils.splitpath(relpath)
383
        if len(parts) == 0:
384
            bailout("cannot re-add root of inventory")
385
386
        if file_id is None:
387
            file_id = bzrlib.branch.gen_file_id(relpath)
388
389
        parent_id = self.path2id(parts[:-1])
390
        ie = InventoryEntry(file_id, parts[-1],
391
                            kind=kind, parent_id=parent_id)
392
        return self.add(ie)
393
394
1 by mbp at sourcefrog
import from baz patch-364
395
    def __delitem__(self, file_id):
396
        """Remove entry by id.
397
398
        >>> inv = Inventory()
399
        >>> inv.add(InventoryEntry('123', 'foo.c'))
400
        >>> '123' in inv
401
        True
402
        >>> del inv['123']
403
        >>> '123' in inv
404
        False
405
        """
406
        ie = self[file_id]
407
138 by mbp at sourcefrog
remove parallel tree from inventory;
408
        assert self[ie.parent_id].children[ie.name] == ie
1 by mbp at sourcefrog
import from baz patch-364
409
        
410
        # TODO: Test deleting all children; maybe hoist to a separate
411
        # deltree method?
412
        if ie.kind == 'directory':
138 by mbp at sourcefrog
remove parallel tree from inventory;
413
            for cie in ie.children.values():
1 by mbp at sourcefrog
import from baz patch-364
414
                del self[cie.file_id]
138 by mbp at sourcefrog
remove parallel tree from inventory;
415
            del ie.children
1 by mbp at sourcefrog
import from baz patch-364
416
417
        del self._byid[file_id]
138 by mbp at sourcefrog
remove parallel tree from inventory;
418
        del self[ie.parent_id].children[ie.name]
1 by mbp at sourcefrog
import from baz patch-364
419
420
421
    def id_set(self):
422
        return Set(self._byid)
423
424
425
    def to_element(self):
426
        """Convert to XML Element"""
427
        e = Element('inventory')
428
        e.text = '\n'
429
        for path, ie in self.iter_entries():
430
            e.append(ie.to_element())
431
        return e
432
    
433
434
    def from_element(cls, elt):
435
        """Construct from XML Element
436
437
        >>> inv = Inventory()
438
        >>> inv.add(InventoryEntry('foo.c-123981239', 'foo.c'))
439
        >>> elt = inv.to_element()
440
        >>> inv2 = Inventory.from_element(elt)
441
        >>> inv2 == inv
442
        True
443
        """
444
        assert elt.tag == 'inventory'
445
        o = cls()
446
        for e in elt:
447
            o.add(InventoryEntry.from_element(e))
448
        return o
449
        
450
    from_element = classmethod(from_element)
451
452
453
    def __cmp__(self, other):
454
        """Compare two sets by comparing their contents.
455
456
        >>> i1 = Inventory()
457
        >>> i2 = Inventory()
458
        >>> i1 == i2
459
        True
460
        >>> i1.add(InventoryEntry('123', 'foo'))
461
        >>> i1 == i2
462
        False
463
        >>> i2.add(InventoryEntry('123', 'foo'))
464
        >>> i1 == i2
465
        True
466
        """
467
        if self is other:
468
            return 0
469
        
470
        if not isinstance(other, Inventory):
471
            return NotImplemented
472
473
        if self.id_set() ^ other.id_set():
474
            return 1
475
476
        for file_id in self._byid:
477
            c = cmp(self[file_id], other[file_id])
478
            if c: return c
479
480
        return 0
481
482
483
    def id2path(self, file_id):
484
        """Return as a list the path to file_id."""
485
        p = []
486
        while file_id != None:
149 by mbp at sourcefrog
experiment with new nested inventory file format
487
            ie = self._byid[file_id]
488
            p.insert(0, ie.name)
1 by mbp at sourcefrog
import from baz patch-364
489
            file_id = ie.parent_id
149 by mbp at sourcefrog
experiment with new nested inventory file format
490
        return '/'.join(p)
1 by mbp at sourcefrog
import from baz patch-364
491
            
492
493
494
    def path2id(self, name):
495
        """Walk down through directories to return entry of last component.
496
497
        names may be either a list of path components, or a single
498
        string, in which case it is automatically split.
499
500
        This returns the entry of the last component in the path,
501
        which may be either a file or a directory.
502
        """
70 by mbp at sourcefrog
Prepare for smart recursive add.
503
        if isinstance(name, types.StringTypes):
504
            name = splitpath(name)
1 by mbp at sourcefrog
import from baz patch-364
505
138 by mbp at sourcefrog
remove parallel tree from inventory;
506
        parent = self[None]
70 by mbp at sourcefrog
Prepare for smart recursive add.
507
        for f in name:
1 by mbp at sourcefrog
import from baz patch-364
508
            try:
138 by mbp at sourcefrog
remove parallel tree from inventory;
509
                cie = parent.children[f]
1 by mbp at sourcefrog
import from baz patch-364
510
                assert cie.name == f
138 by mbp at sourcefrog
remove parallel tree from inventory;
511
                parent = cie
1 by mbp at sourcefrog
import from baz patch-364
512
            except KeyError:
513
                # or raise an error?
514
                return None
515
138 by mbp at sourcefrog
remove parallel tree from inventory;
516
        return parent.file_id
1 by mbp at sourcefrog
import from baz patch-364
517
518
519
    def has_filename(self, names):
520
        return bool(self.path2id(names))
521
522
523
    def has_id(self, file_id):
524
        return self._byid.has_key(file_id)
525
526
527
70 by mbp at sourcefrog
Prepare for smart recursive add.
528
529
1 by mbp at sourcefrog
import from baz patch-364
530
if __name__ == '__main__':
531
    import doctest, inventory
532
    doctest.testmod(inventory)