~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/history2weaves.py

  • Committer: Martin Pool
  • Date: 2005-09-16 09:16:05 UTC
  • Revision ID: mbp@sourcefrog.net-20050916091605-166a47cb7136ce54
- refactor weave upgrade into a MethodObject

- start work on reading in old-format objects ready to convert

- prepare for conversion of all present revisions, not just
  mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
# sort of all revisions.  (Or do we, can we just before doing a revision
40
40
# see that all its parents have either been converted or abandoned?)
41
41
 
42
 
try:
43
 
    import psyco
44
 
    psyco.full()
45
 
except ImportError:
46
 
    pass
 
42
if False:
 
43
    try:
 
44
        import psyco
 
45
        psyco.full()
 
46
    except ImportError:
 
47
        pass
47
48
 
48
49
 
49
50
import tempfile
50
51
import hotshot, hotshot.stats
51
52
import sys
52
53
import logging
 
54
import time
53
55
 
54
56
from bzrlib.branch import Branch, find_branch
55
57
from bzrlib.revfile import Revfile
57
59
from bzrlib.weavefile import read_weave, write_weave
58
60
from bzrlib.progress import ProgressBar
59
61
from bzrlib.atomicfile import AtomicFile
 
62
from bzrlib.xml4 import serializer_v4
 
63
from bzrlib.xml5 import serializer_v5
60
64
import bzrlib.trace
61
65
 
62
66
 
63
67
 
64
 
def convert():
65
 
    bzrlib.trace.enable_default_logging()
66
 
 
67
 
    pb = ProgressBar()
68
 
 
69
 
    inv_weave = Weave()
70
 
 
71
 
    last_text_sha = {}
72
 
 
73
 
    # holds in-memory weaves for all files
74
 
    text_weaves = {}
75
 
 
76
 
    b = Branch('.', relax_version_check=True)
77
 
 
78
 
    revno = 1
79
 
    rev_history = b.revision_history()
80
 
    last_idx = None
81
 
    inv_parents = []
82
 
    text_count = 0
83
 
    
84
 
    for rev_id in rev_history:
85
 
        pb.update('converting revision', revno, len(rev_history))
86
 
        
87
 
        inv_xml = b.get_inventory_xml(rev_id).readlines()
88
 
 
89
 
        new_idx = inv_weave.add(rev_id, inv_parents, inv_xml)
 
68
class Convert(object):
 
69
    def __init__(self):
 
70
        self.total_revs = 0
 
71
        self.converted_revs = 0
 
72
        self.text_count = 0
 
73
        self.convert()
 
74
 
 
75
 
 
76
 
 
77
    def convert(self):
 
78
        bzrlib.trace.enable_default_logging()
 
79
        self.pb = ProgressBar()
 
80
        self.inv_weave = Weave('__inventory')
 
81
        self.anc_weave = Weave('__ancestry')
 
82
 
 
83
        last_text_sha = {}
 
84
 
 
85
        # holds in-memory weaves for all files
 
86
        text_weaves = {}
 
87
 
 
88
        b = self.branch = Branch('.', relax_version_check=True)
 
89
 
 
90
        revno = 1
 
91
        rev_history = b.revision_history()
 
92
        last_idx = None
 
93
        inv_parents = []
 
94
 
 
95
        # todo is a stack holding the revisions we still need to process;
 
96
        # appending to it adds new highest-priority revisions
 
97
        todo = rev_history[:]
 
98
        todo.reverse()
 
99
        self.total_revs = len(todo)
 
100
 
 
101
        while todo:
 
102
            self._convert_one_rev(todo.pop())
 
103
 
 
104
        self.pb.clear()
 
105
        print 'upgraded to weaves:'
 
106
        print '  %6d revisions and inventories' % self.converted_revs
 
107
        print '  %6d texts' % self.text_count
 
108
 
 
109
        self._write_all_weaves()
 
110
 
 
111
 
 
112
    def _write_all_weaves(self):
 
113
        i = 0
 
114
        return ############################################
 
115
        # TODO: commit them all atomically at the end, not one by one
 
116
        write_atomic_weave(self.inv_weave, 'weaves/inventory.weave')
 
117
        write_atomic_weave(self.anc_weave, 'weaves/ancestry.weave')
 
118
        for file_id, file_weave in text_weaves.items():
 
119
            self.pb.update('writing weave', i, len(text_weaves))
 
120
            write_atomic_weave(file_weave, 'weaves/%s.weave' % file_id)
 
121
            i += 1
 
122
 
 
123
        self.pb.clear()
 
124
 
 
125
        
 
126
    def _convert_one_rev(self, rev_id):
 
127
        self._bump_progress()
 
128
        b = self.branch
 
129
        rev_xml = b.revision_store[rev_id].read()
 
130
        inv_xml = b.inventory_store[rev_id].read()
 
131
 
 
132
        rev = serializer_v4.read_revision_from_string(rev_xml)
 
133
        inv = serializer_v4.read_inventory_from_string(inv_xml)
 
134
        
 
135
        return ##########################################
 
136
 
 
137
        new_idx = self.inv_weave.add(rev_id, inv_parents, inv_xml)
90
138
        inv_parents = [new_idx]
91
139
 
92
140
        tree = b.revision_tree(rev_id)
109
157
            # revision then make a new weave; else find the old one
110
158
            if file_id not in text_weaves:
111
159
                text_weaves[file_id] = Weave()
112
 
                
 
160
 
113
161
            w = text_weaves[file_id]
114
162
 
115
163
            # base the new text version off whatever was last
125
173
            text_count += 1
126
174
 
127
175
        revno += 1
128
 
 
129
 
    pb.clear()
130
 
    print '%6d revisions and inventories' % revno
131
 
    print '%6d texts' % text_count
132
 
 
133
 
    i = 0
134
 
    # TODO: commit them all atomically at the end, not one by one
135
 
    write_atomic_weave(inv_weave, 'weaves/inventory.weave')
136
 
    for file_id, file_weave in text_weaves.items():
137
 
        pb.update('writing weave', i, len(text_weaves))
138
 
        write_atomic_weave(file_weave, 'weaves/%s.weave' % file_id)
139
 
        i += 1
140
 
 
141
 
    pb.clear()
 
176
        
 
177
    def _bump_progress(self):
 
178
        self.converted_revs += 1
 
179
        self.pb.update('converting revisions',
 
180
                       self.converted_revs,
 
181
                       self.total_revs)
142
182
 
143
183
 
144
184
def write_atomic_weave(weave, filename):
157
197
 
158
198
    prof = hotshot.Profile(prof_f.name)
159
199
 
160
 
    prof.runcall(convert) 
 
200
    prof.runcall(Convert) 
161
201
    prof.close()
162
202
 
163
203
    stats = hotshot.stats.load(prof_f.name)
171
211
if '-p' in sys.argv[1:]:
172
212
    profile_convert()
173
213
else:
174
 
    convert()
 
214
    Convert()
175
215