~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Wouter van Heyst
  • Date: 2006-06-07 16:05:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: larstiq@larstiq.dyndns.org-20060607160527-2b3649154d0e2e84
more code cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/python
2
2
 
3
3
# Copyright (C) 2005 Canonical Ltd
4
 
#
 
4
 
5
5
# This program is free software; you can redistribute it and/or modify
6
6
# it under the terms of the GNU General Public License as published by
7
7
# the Free Software Foundation; either version 2 of the License, or
8
8
# (at your option) any later version.
9
 
#
 
9
 
10
10
# This program is distributed in the hope that it will be useful,
11
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
# GNU General Public License for more details.
14
 
#
 
14
 
15
15
# You should have received a copy of the GNU General Public License
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
27
# property.
28
28
 
29
29
# TODO: Nothing here so far assumes the lines are really \n newlines,
30
 
# rather than being split up in some other way.  We could accommodate
 
30
# rather than being split up in some other way.  We could accomodate
31
31
# binaries, perhaps by naively splitting on \n or perhaps using
32
32
# something like a rolling checksum.
33
33
 
73
73
import os
74
74
import sha
75
75
import time
76
 
import warnings
77
76
 
78
 
from bzrlib import (
79
 
    progress,
80
 
    )
81
77
from bzrlib.trace import mutter
82
78
from bzrlib.errors import (WeaveError, WeaveFormatError, WeaveParentMismatch,
83
79
        RevisionAlreadyPresent,
88
84
import bzrlib.errors as errors
89
85
from bzrlib.osutils import sha_strings
90
86
import bzrlib.patiencediff
91
 
from bzrlib.symbol_versioning import (deprecated_method,
92
 
        deprecated_function,
93
 
        zero_eight,
94
 
        )
 
87
from bzrlib.symbol_versioning import *
95
88
from bzrlib.tsort import topo_sort
96
89
from bzrlib.versionedfile import VersionedFile, InterVersionedFile
97
90
from bzrlib.weavefile import _read_weave_v5, write_weave_v5
238
231
 
239
232
    @deprecated_method(zero_eight)
240
233
    def lookup(self, name):
241
 
        """Backwards compatibility thunk:
 
234
        """Backwards compatability thunk:
242
235
 
243
236
        Return name, as name is valid in the api now, and spew deprecation
244
237
        warnings everywhere.
247
240
 
248
241
    def _lookup(self, name):
249
242
        """Convert symbolic version name to index."""
250
 
        self.check_not_reserved_id(name)
251
243
        try:
252
244
            return self._name_map[name]
253
245
        except KeyError:
269
261
 
270
262
    def has_version(self, version_id):
271
263
        """See VersionedFile.has_version."""
272
 
        return (version_id in self._name_map)
 
264
        return self._name_map.has_key(version_id)
273
265
 
274
266
    __contains__ = has_version
275
267
 
530
522
        if lines == basis_lines:
531
523
            return new_version            
532
524
 
533
 
        # add a sentinel, because we can also match against the final line
 
525
        # add a sentinal, because we can also match against the final line
534
526
        basis_lineno.append(len(self._weave))
535
527
 
536
528
        # XXX: which line of the weave should we really consider
607
599
        else:
608
600
            return self.get_ancestry(version_ids)
609
601
 
610
 
    def get_ancestry(self, version_ids, topo_sorted=True):
 
602
    def get_ancestry(self, version_ids):
611
603
        """See VersionedFile.get_ancestry."""
612
604
        if isinstance(version_ids, basestring):
613
605
            version_ids = [version_ids]
641
633
        """
642
634
        return len(other_parents.difference(my_parents)) == 0
643
635
 
 
636
    def annotate(self, version_id):
 
637
        if isinstance(version_id, int):
 
638
            warn('Weave.annotate(int) is deprecated. Please use version names'
 
639
                 ' in all circumstances as of 0.8',
 
640
                 DeprecationWarning,
 
641
                 stacklevel=2
 
642
                 )
 
643
            result = []
 
644
            for origin, lineno, text in self._extract([version_id]):
 
645
                result.append((origin, text))
 
646
            return result
 
647
        else:
 
648
            return super(Weave, self).annotate(version_id)
 
649
    
644
650
    def annotate_iter(self, version_id):
645
651
        """Yield list of (version-id, line) pairs for the specified version.
646
652
 
654
660
        """_walk has become visit, a supported api."""
655
661
        return self._walk_internal()
656
662
 
657
 
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
658
 
                                                pb=None):
 
663
    def iter_lines_added_or_present_in_versions(self, version_ids=None):
659
664
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
660
665
        if version_ids is None:
661
666
            version_ids = self.versions()
1064
1069
 
1065
1070
    @deprecated_method(zero_eight)
1066
1071
    def reweave(self, other, pb=None, msg=None):
1067
 
        """reweave has been superseded by plain use of join."""
 
1072
        """reweave has been superceded by plain use of join."""
1068
1073
        return self.join(other, pb, msg)
1069
1074
 
1070
1075
    def _reweave(self, other, pb, msg):
1107
1112
 
1108
1113
    def _add_lines(self, version_id, parents, lines, parent_texts):
1109
1114
        """Add a version and save the weave."""
1110
 
        self.check_not_reserved_id(version_id)
1111
1115
        result = super(WeaveFile, self)._add_lines(version_id, parents, lines,
1112
1116
                                                   parent_texts)
1113
1117
        self._save()
1124
1128
        sio = StringIO()
1125
1129
        write_weave_v5(self, sio)
1126
1130
        sio.seek(0)
1127
 
        transport.put_file(name + WeaveFile.WEAVE_SUFFIX, sio, self._filemode)
 
1131
        transport.put(name + WeaveFile.WEAVE_SUFFIX, sio, self._filemode)
1128
1132
 
1129
1133
    def create_empty(self, name, transport, filemode=None):
1130
1134
        return WeaveFile(name, transport, filemode, create=True)
1135
1139
        sio = StringIO()
1136
1140
        write_weave_v5(self, sio)
1137
1141
        sio.seek(0)
1138
 
        self._transport.put_file(self._weave_name + WeaveFile.WEAVE_SUFFIX,
1139
 
                                 sio,
1140
 
                                 self._filemode)
 
1142
        self._transport.put(self._weave_name + WeaveFile.WEAVE_SUFFIX,
 
1143
                            sio,
 
1144
                            self._filemode)
1141
1145
 
1142
1146
    @staticmethod
1143
1147
    def get_suffixes():
1235
1239
    from bzrlib.weavefile import read_weave
1236
1240
 
1237
1241
    wf = file(weave_file, 'rb')
1238
 
    w = read_weave(wf)
 
1242
    w = read_weave(wf, WeaveVersionedFile)
1239
1243
    # FIXME: doesn't work on pipes
1240
1244
    weave_size = wf.tell()
1241
1245
 
1415
1419
        raise ValueError('unknown command %r' % cmd)
1416
1420
    
1417
1421
 
 
1422
 
 
1423
def profile_main(argv):
 
1424
    import tempfile, hotshot, hotshot.stats
 
1425
 
 
1426
    prof_f = tempfile.NamedTemporaryFile()
 
1427
 
 
1428
    prof = hotshot.Profile(prof_f.name)
 
1429
 
 
1430
    ret = prof.runcall(main, argv)
 
1431
    prof.close()
 
1432
 
 
1433
    stats = hotshot.stats.load(prof_f.name)
 
1434
    #stats.strip_dirs()
 
1435
    stats.sort_stats('cumulative')
 
1436
    ## XXX: Might like to write to stderr or the trace file instead but
 
1437
    ## print_stats seems hardcoded to stdout
 
1438
    stats.print_stats(20)
 
1439
            
 
1440
    return ret
 
1441
 
 
1442
 
 
1443
def lsprofile_main(argv): 
 
1444
    from bzrlib.lsprof import profile
 
1445
    ret,stats = profile(main, argv)
 
1446
    stats.sort()
 
1447
    stats.pprint()
 
1448
    return ret
 
1449
 
 
1450
 
1418
1451
if __name__ == '__main__':
1419
1452
    import sys
1420
 
    sys.exit(main(sys.argv))
 
1453
    if '--profile' in sys.argv:
 
1454
        args = sys.argv[:]
 
1455
        args.remove('--profile')
 
1456
        sys.exit(profile_main(args))
 
1457
    elif '--lsprof' in sys.argv:
 
1458
        args = sys.argv[:]
 
1459
        args.remove('--lsprof')
 
1460
        sys.exit(lsprofile_main(args))
 
1461
    else:
 
1462
        sys.exit(main(sys.argv))
1421
1463
 
1422
1464
 
1423
1465
class InterWeave(InterVersionedFile):