/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: Aaron Bentley
  • Date: 2006-11-17 04:06:03 UTC
  • mfrom: (2139 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2162.
  • Revision ID: aaron.bentley@utoronto.ca-20061117040603-pgebxndswvwk26tt
Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
# created, but it's not for now.
28
28
ROOT_ID = "TREE_ROOT"
29
29
 
30
 
 
31
 
import collections
32
 
import os.path
 
30
import os
33
31
import re
34
32
import sys
 
33
 
 
34
from bzrlib.lazy_import import lazy_import
 
35
lazy_import(globals(), """
 
36
import collections
35
37
import tarfile
36
 
import types
37
 
from warnings import warn
38
38
 
39
39
import bzrlib
40
 
from bzrlib import errors, osutils
41
 
from bzrlib.osutils import (pumpfile, quotefn, splitpath, joinpath,
42
 
                            pathjoin, sha_strings)
43
 
from bzrlib.errors import (NotVersionedError, InvalidEntryName,
44
 
                           BzrError, BzrCheckError, BinaryFile)
 
40
from bzrlib import (
 
41
    errors,
 
42
    generate_ids,
 
43
    osutils,
 
44
    symbol_versioning,
 
45
    )
 
46
""")
 
47
 
 
48
from bzrlib.errors import (
 
49
    BzrCheckError,
 
50
    BzrError,
 
51
    )
45
52
from bzrlib.trace import mutter
46
53
 
47
54
 
82
89
    InventoryDirectory('123', 'src', parent_id='TREE_ROOT', revision=None)
83
90
    >>> i.add(InventoryFile('2323', 'hello.c', parent_id='123'))
84
91
    InventoryFile('2323', 'hello.c', parent_id='123', sha1=None, len=None)
85
 
    >>> shouldbe = {0: '', 1: 'src', 2: pathjoin('src','hello.c')}
 
92
    >>> shouldbe = {0: '', 1: 'src', 2: 'src/hello.c'}
86
93
    >>> for ix, j in enumerate(i.iter_entries()):
87
94
    ...   print (j[0] == shouldbe[ix], j[1])
88
95
    ... 
246
253
 
247
254
    def get_tar_item(self, root, dp, now, tree):
248
255
        """Get a tarfile item and a file stream for its content."""
249
 
        item = tarfile.TarInfo(pathjoin(root, dp))
 
256
        item = tarfile.TarInfo(osutils.pathjoin(root, dp).encode('utf8'))
250
257
        # TODO: would be cool to actually set it to the timestamp of the
251
258
        # revision it was last changed
252
259
        item.mtime = now
281
288
        """
282
289
        assert isinstance(name, basestring), name
283
290
        if '/' in name or '\\' in name:
284
 
            raise InvalidEntryName(name=name)
 
291
            raise errors.InvalidEntryName(name=name)
285
292
        self.executable = False
286
293
        self.revision = None
287
294
        self.text_sha1 = None
311
318
        
312
319
        This is a template method - implement _put_on_disk in subclasses.
313
320
        """
314
 
        fullpath = pathjoin(dest, dp)
 
321
        fullpath = osutils.pathjoin(dest, dp)
315
322
        self._put_on_disk(fullpath, tree)
316
323
        # mutter("  export {%s} kind %s to %s", self.file_id,
317
324
        #         self.kind, fullpath)
514
521
        self.parent_id = None
515
522
        self.name = u''
516
523
        self.revision = None
517
 
        warn('RootEntry is deprecated as of bzr 0.10.  Please use '
518
 
             'InventoryDirectory instead.',
519
 
            DeprecationWarning, stacklevel=2)
 
524
        symbol_versioning.warn('RootEntry is deprecated as of bzr 0.10.'
 
525
                               '  Please use InventoryDirectory instead.',
 
526
                               DeprecationWarning, stacklevel=2)
520
527
 
521
528
    def __eq__(self, other):
522
529
        if not isinstance(other, RootEntry):
645
652
            else:
646
653
                text_diff(to_label, to_text,
647
654
                          from_label, from_text, output_to)
648
 
        except BinaryFile:
 
655
        except errors.BinaryFile:
649
656
            if reverse:
650
657
                label_pair = (to_label, from_label)
651
658
            else:
677
684
 
678
685
    def _put_on_disk(self, fullpath, tree):
679
686
        """See InventoryEntry._put_on_disk."""
680
 
        pumpfile(tree.get_file(self.file_id), file(fullpath, 'wb'))
 
687
        osutils.pumpfile(tree.get_file(self.file_id), file(fullpath, 'wb'))
681
688
        if tree.is_executable(self.file_id):
682
689
            os.chmod(fullpath, 0755)
683
690
 
849
856
    ['', u'hello.c']
850
857
    >>> inv = Inventory('TREE_ROOT-12345678-12345678')
851
858
    >>> inv.add(InventoryFile('123-123', 'hello.c', ROOT_ID))
 
859
    Traceback (most recent call last):
 
860
    BzrError: parent_id {TREE_ROOT} not in inventory
 
861
    >>> inv.add(InventoryFile('123-123', 'hello.c', 'TREE_ROOT-12345678-12345678'))
852
862
    InventoryFile('123-123', 'hello.c', parent_id='TREE_ROOT-12345678-12345678', sha1=None, len=None)
853
863
    """
854
864
    def __init__(self, root_id=ROOT_ID, revision_id=None):
861
871
        The inventory is created with a default root directory, with
862
872
        an id of None.
863
873
        """
864
 
        # We are letting Branch.create() create a unique inventory
865
 
        # root id. Rather than generating a random one here.
866
 
        #if root_id is None:
867
 
        #    root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
868
874
        if root_id is not None:
869
875
            self._set_root(InventoryDirectory(root_id, '', None))
870
876
        else:
871
877
            self.root = None
872
878
            self._byid = {}
873
 
        # FIXME: this isn't ever used, changing it to self.revision may break
874
 
        # things. TODO make everything use self.revision_id
875
879
        self.revision_id = revision_id
876
880
 
877
881
    def _set_root(self, ie):
898
902
    def iter_entries(self, from_dir=None):
899
903
        """Return (path, entry) pairs, in order by name."""
900
904
        if from_dir is None:
901
 
            assert self.root
 
905
            if self.root is None:
 
906
                return
902
907
            from_dir = self.root
903
908
            yield '', self.root
904
909
        elif isinstance(from_dir, basestring):
951
956
        # TODO? Perhaps this should return the from_dir so that the root is
952
957
        # yielded? or maybe an option?
953
958
        if from_dir is None:
954
 
            assert self.root
 
959
            if self.root is None:
 
960
                return
955
961
            from_dir = self.root
956
962
            yield '', self.root
957
963
        elif isinstance(from_dir, basestring):
982
988
            kids = dir_ie.children.items()
983
989
            kids.sort()
984
990
            for name, ie in kids:
985
 
                child_path = pathjoin(dir_path, name)
 
991
                child_path = osutils.pathjoin(dir_path, name)
986
992
                accum.append((child_path, ie))
987
993
                if ie.kind == 'directory':
988
994
                    descend(ie, child_path)
1001
1007
            kids.sort()
1002
1008
 
1003
1009
            for name, child_ie in kids:
1004
 
                child_path = pathjoin(parent_path, name)
 
1010
                child_path = osutils.pathjoin(parent_path, name)
1005
1011
                descend(child_ie, child_path)
1006
1012
        descend(self.root, u'')
1007
1013
        return accum
1031
1037
        try:
1032
1038
            return self._byid[file_id]
1033
1039
        except KeyError:
1034
 
            if file_id is None:
1035
 
                raise BzrError("can't look up file_id None")
1036
 
            else:
1037
 
                raise BzrError("file_id {%s} not in inventory" % file_id)
 
1040
            # really we're passing an inventory, not a tree...
 
1041
            raise errors.NoSuchId(self, file_id)
1038
1042
 
1039
1043
    def get_file_kind(self, file_id):
1040
1044
        return self._byid[file_id].kind
1057
1061
            assert self.root is None and len(self._byid) == 0
1058
1062
            self._set_root(entry)
1059
1063
            return entry
1060
 
        if entry.parent_id == ROOT_ID:
1061
 
            assert self.root is not None, self
1062
 
            entry.parent_id = self.root.file_id
1063
 
 
1064
1064
        try:
1065
1065
            parent = self._byid[entry.parent_id]
1066
1066
        except KeyError:
1068
1068
 
1069
1069
        if entry.name in parent.children:
1070
1070
            raise BzrError("%s is already versioned" %
1071
 
                    pathjoin(self.id2path(parent.file_id), entry.name))
 
1071
                    osutils.pathjoin(self.id2path(parent.file_id), entry.name))
1072
1072
 
1073
1073
        self._byid[entry.file_id] = entry
1074
1074
        parent.children[entry.name] = entry
1085
1085
 
1086
1086
        if len(parts) == 0:
1087
1087
            if file_id is None:
1088
 
                file_id = bzrlib.workingtree.gen_root_id()
 
1088
                file_id = generate_ids.gen_root_id()
1089
1089
            self.root = InventoryDirectory(file_id, '', None)
1090
1090
            self._byid = {self.root.file_id: self.root}
1091
1091
            return
1093
1093
            parent_path = parts[:-1]
1094
1094
            parent_id = self.path2id(parent_path)
1095
1095
            if parent_id is None:
1096
 
                raise NotVersionedError(path=parent_path)
 
1096
                raise errors.NotVersionedError(path=parent_path)
1097
1097
        ie = make_entry(kind, parts[-1], parent_id, file_id)
1098
1098
        return self.add(ie)
1099
1099
 
1193
1193
 
1194
1194
        Returns None IFF the path is not found.
1195
1195
        """
1196
 
        if isinstance(name, types.StringTypes):
1197
 
            name = splitpath(name)
 
1196
        if isinstance(name, basestring):
 
1197
            name = osutils.splitpath(name)
1198
1198
 
1199
1199
        # mutter("lookup path %r" % name)
1200
1200
 
1201
1201
        parent = self.root
 
1202
        if parent is None:
 
1203
            return None
1202
1204
        for f in name:
1203
1205
            try:
1204
 
                cie = parent.children[f]
 
1206
                children = getattr(parent, 'children', None)
 
1207
                if children is None:
 
1208
                    return None
 
1209
                cie = children[f]
1205
1210
                assert cie.name == f
1206
1211
                assert cie.parent_id == parent.file_id
1207
1212
                parent = cie
1264
1269
        file_ie.name = new_name
1265
1270
        file_ie.parent_id = new_parent_id
1266
1271
 
 
1272
    def is_root(self, file_id):
 
1273
        return self.root is not None and file_id == self.root.file_id
 
1274
 
1267
1275
 
1268
1276
def make_entry(kind, name, parent_id, file_id=None):
1269
1277
    """Create an inventory entry.
1274
1282
    :param file_id: the file_id to use. if None, one will be created.
1275
1283
    """
1276
1284
    if file_id is None:
1277
 
        file_id = bzrlib.workingtree.gen_file_id(name)
 
1285
        file_id = generate_ids.gen_file_id(name)
1278
1286
 
1279
1287
    norm_name, can_access = osutils.normalized_filename(name)
1280
1288
    if norm_name != name: