~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Robert Collins
  • Date: 2006-05-16 06:45:43 UTC
  • mto: (1713.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1714.
  • Revision ID: robertc@robertcollins.net-20060516064543-5cb7cc63047ba98b
Start on bench_add, an add benchtest.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
70
70
 
71
71
from copy import copy
72
72
from cStringIO import StringIO
 
73
from difflib import SequenceMatcher
73
74
import os
74
75
import sha
75
76
import time
76
 
import warnings
77
77
 
78
78
from bzrlib.trace import mutter
79
79
from bzrlib.errors import (WeaveError, WeaveFormatError, WeaveParentMismatch,
84
84
        )
85
85
import bzrlib.errors as errors
86
86
from bzrlib.osutils import sha_strings
87
 
import bzrlib.patiencediff
88
 
from bzrlib.symbol_versioning import (deprecated_method,
89
 
        deprecated_function,
90
 
        zero_eight,
91
 
        )
 
87
from bzrlib.symbol_versioning import *
92
88
from bzrlib.tsort import topo_sort
93
89
from bzrlib.versionedfile import VersionedFile, InterVersionedFile
94
90
from bzrlib.weavefile import _read_weave_v5, write_weave_v5
184
180
    """
185
181
 
186
182
    __slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
187
 
                 '_weave_name', '_matcher']
 
183
                 '_weave_name']
188
184
    
189
 
    def __init__(self, weave_name=None, access_mode='w', matcher=None):
 
185
    def __init__(self, weave_name=None, access_mode='w'):
190
186
        super(Weave, self).__init__(access_mode)
191
187
        self._weave = []
192
188
        self._parents = []
194
190
        self._names = []
195
191
        self._name_map = {}
196
192
        self._weave_name = weave_name
197
 
        if matcher is None:
198
 
            self._matcher = bzrlib.patiencediff.PatienceSequenceMatcher
199
 
        else:
200
 
            self._matcher = matcher
201
193
 
202
194
    def __repr__(self):
203
195
        return "Weave(%r)" % self._weave_name
235
227
 
236
228
    @deprecated_method(zero_eight)
237
229
    def lookup(self, name):
238
 
        """Backwards compatibility thunk:
 
230
        """Backwards compatability thunk:
239
231
 
240
232
        Return name, as name is valid in the api now, and spew deprecation
241
233
        warnings everywhere.
526
518
        if lines == basis_lines:
527
519
            return new_version            
528
520
 
529
 
        # add a sentinel, because we can also match against the final line
 
521
        # add a sentinal, because we can also match against the final line
530
522
        basis_lineno.append(len(self._weave))
531
523
 
532
524
        # XXX: which line of the weave should we really consider
536
528
        #print 'basis_lines:', basis_lines
537
529
        #print 'new_lines:  ', lines
538
530
 
539
 
        s = self._matcher(None, basis_lines, lines)
 
531
        s = SequenceMatcher(None, basis_lines, lines)
540
532
 
541
533
        # offset gives the number of lines that have been inserted
542
534
        # into the weave up to the current point; if the original edit instruction
639
631
 
640
632
    def annotate(self, version_id):
641
633
        if isinstance(version_id, int):
642
 
            warnings.warn('Weave.annotate(int) is deprecated. Please use version names'
 
634
            warn('Weave.annotate(int) is deprecated. Please use version names'
643
635
                 ' in all circumstances as of 0.8',
644
636
                 DeprecationWarning,
645
637
                 stacklevel=2
1073
1065
 
1074
1066
    @deprecated_method(zero_eight)
1075
1067
    def reweave(self, other, pb=None, msg=None):
1076
 
        """reweave has been superseded by plain use of join."""
 
1068
        """reweave has been superceded by plain use of join."""
1077
1069
        return self.join(other, pb, msg)
1078
1070
 
1079
1071
    def _reweave(self, other, pb, msg):
1243
1235
    from bzrlib.weavefile import read_weave
1244
1236
 
1245
1237
    wf = file(weave_file, 'rb')
1246
 
    w = read_weave(wf)
 
1238
    w = read_weave(wf, WeaveVersionedFile)
1247
1239
    # FIXME: doesn't work on pipes
1248
1240
    weave_size = wf.tell()
1249
1241
 
1364
1356
        sys.stdout.writelines(w.get_iter(int(argv[3])))
1365
1357
        
1366
1358
    elif cmd == 'diff':
 
1359
        from difflib import unified_diff
1367
1360
        w = readit()
1368
1361
        fn = argv[2]
1369
1362
        v1, v2 = map(int, argv[3:5])
1370
1363
        lines1 = w.get(v1)
1371
1364
        lines2 = w.get(v2)
1372
 
        diff_gen = bzrlib.patiencediff.unified_diff(lines1, lines2,
 
1365
        diff_gen = unified_diff(lines1, lines2,
1373
1366
                                '%s version %d' % (fn, v1),
1374
1367
                                '%s version %d' % (fn, v2))
1375
1368
        sys.stdout.writelines(diff_gen)
1423
1416
        raise ValueError('unknown command %r' % cmd)
1424
1417
    
1425
1418
 
 
1419
 
 
1420
def profile_main(argv):
 
1421
    import tempfile, hotshot, hotshot.stats
 
1422
 
 
1423
    prof_f = tempfile.NamedTemporaryFile()
 
1424
 
 
1425
    prof = hotshot.Profile(prof_f.name)
 
1426
 
 
1427
    ret = prof.runcall(main, argv)
 
1428
    prof.close()
 
1429
 
 
1430
    stats = hotshot.stats.load(prof_f.name)
 
1431
    #stats.strip_dirs()
 
1432
    stats.sort_stats('cumulative')
 
1433
    ## XXX: Might like to write to stderr or the trace file instead but
 
1434
    ## print_stats seems hardcoded to stdout
 
1435
    stats.print_stats(20)
 
1436
            
 
1437
    return ret
 
1438
 
 
1439
 
 
1440
def lsprofile_main(argv): 
 
1441
    from bzrlib.lsprof import profile
 
1442
    ret,stats = profile(main, argv)
 
1443
    stats.sort()
 
1444
    stats.pprint()
 
1445
    return ret
 
1446
 
 
1447
 
1426
1448
if __name__ == '__main__':
1427
1449
    import sys
1428
 
    sys.exit(main(sys.argv))
 
1450
    if '--profile' in sys.argv:
 
1451
        args = sys.argv[:]
 
1452
        args.remove('--profile')
 
1453
        sys.exit(profile_main(args))
 
1454
    elif '--lsprof' in sys.argv:
 
1455
        args = sys.argv[:]
 
1456
        args.remove('--lsprof')
 
1457
        sys.exit(lsprofile_main(args))
 
1458
    else:
 
1459
        sys.exit(main(sys.argv))
1429
1460
 
1430
1461
 
1431
1462
class InterWeave(InterVersionedFile):