~bzr-pqm/bzr/bzr.dev

1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
1
#!/usr/bin/env python2.4
1185.81.28 by Aaron Bentley
Add copyright/GPL to patience-test.py
2
# Copyright (C) 2006 by Canonical Ltd
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
17
18
import difflib
19
from StringIO import StringIO
20
from subprocess import Popen, PIPE
21
from tempfile import mkdtemp
22
23
from bzrlib.branch import Branch
1711.2.19 by John Arbash Meinel
switch patience-test to use PatienceSequenceMatcher.
24
import bzrlib.patiencediff
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
25
from bzrlib.diff import internal_diff
26
from bzrlib.osutils import pathjoin
27
28
29
def patch(path, patch_lines):
30
    """Apply a patch to a branch, using patch(1).  URLs may be used."""
31
    cmd = ['patch', '--quiet', path]
32
    r = 0
33
    child_proc = Popen(cmd, stdin=PIPE)
34
    for line in patch_lines:
35
        child_proc.stdin.write(line)
36
    child_proc.stdin.close()
37
    r = child_proc.wait()
38
    return r
39
40
1185.81.20 by Aaron Bentley
Add length counting to patience test
41
old_total = 0
42
new_total = 0
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
43
b = Branch.open_containing('.')[0]
44
repo = b.repository
45
repo.lock_write()
46
try:
47
    temp_dir = mkdtemp()
48
    transaction = repo.get_transaction()
49
    file_list = list(repo.text_store)
50
    for i, file_id in enumerate(file_list):
51
        print "%.2f%% %d of %d %s" % ((float(i)/len(file_list) * 100), i,
52
                                    len(file_list), file_id)
53
        versioned_file = repo.text_store.get_weave(file_id, transaction)
54
        last_id = None
55
        for revision_id in versioned_file.versions():
56
            if last_id != None:
57
                old_lines = versioned_file.get_lines(last_id)
58
                new_lines = versioned_file.get_lines(revision_id)
59
                if ''.join(old_lines) == ''.join(new_lines):
60
                    continue
1185.81.20 by Aaron Bentley
Add length counting to patience test
61
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
62
                new_patch = StringIO()
63
                try:
64
                    internal_diff('old', old_lines, 'new', new_lines, new_patch,
1711.2.19 by John Arbash Meinel
switch patience-test to use PatienceSequenceMatcher.
65
                        sequence_matcher=bzrlib.patiencediff.PatienceSequenceMatcher)
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
66
                except:
67
                    file(pathjoin(temp_dir, 'old'), 
68
                         'wb').write(''.join(old_lines))
69
                    file(pathjoin(temp_dir, 'new'), 
70
                         'wb').write(''.join(new_lines))
71
                    print "temp dir is %s" % temp_dir
72
                    raise
1185.81.20 by Aaron Bentley
Add length counting to patience test
73
                old_patch = StringIO()
74
                internal_diff('old', old_lines, 'new', new_lines, old_patch,
75
                              sequence_matcher=difflib.SequenceMatcher)
76
                old_total += len(old_patch.getvalue())
77
                new_total += len(new_patch.getvalue())
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
78
                new_patch.seek(0)
79
                file_path = pathjoin(temp_dir, 'file')
80
                orig_file = file(file_path, 'wb')
81
                for line in old_lines:
82
                    orig_file.write(line)
83
                orig_file.close()
84
                patch(file_path, new_patch)
85
                new_file = file(file_path, 'rb')
1185.81.26 by Aaron Bentley
Fix patience torture test
86
                assert list(new_file) == new_lines
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
87
            last_id = revision_id
1185.81.20 by Aaron Bentley
Add length counting to patience test
88
    print old_total, new_total
1185.81.19 by Aaron Bentley
Add test of Patience sequence matcher
89
finally:
90
    repo.unlock()