~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Martin Pool
  • Date: 2005-08-19 22:41:41 UTC
  • Revision ID: mbp@sourcefrog.net-20050819224141-b43244067d36eee7
- add a tool script to convert past history into weaves

  this is not quite finished yet but does pretty well.  
  we still need to upgrade the inventories as we go to make
  them store file revision_ids, so that we can get the right
  file back out of the weave.

  the results compare quite well: the history of bzr up to 
  this point is 25688kB in the full-text store, gzipped, and 
  3676kB in the weave store, and 1460kB in the weave store
  gzipped.  so about 20x compression before gzip.  a full
  working directory is 3084kB.

  using this we can get accurate fast annotations of all past
  versions, though you do need to map the weave version numbers
  back to revisions by hand for the moment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
1
3
# Copyright (C) 2005 Canonical Ltd
2
4
 
3
5
# This program is free software; you can redistribute it and/or modify
93
95
 
94
96
 
95
97
def read_weave(f):
96
 
    # FIXME: detect the weave type and dispatch
97
 
    from bzrlib.trace import mutter
98
 
    from weave import Weave
99
 
    w = Weave(getattr(f, 'name', None))
100
 
    _read_weave_v5(f, w)
101
 
    return w
102
 
 
103
 
 
104
 
def _read_weave_v5(f, w):
105
 
    """Private helper routine to read a weave format 5 file into memory.
106
 
    
107
 
    This is only to be used by read_weave and WeaveFile.__init__.
108
 
    """
109
 
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
110
 
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
111
 
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
112
 
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
113
 
    # replace readline call with iter over all lines ->
114
 
    # safe because we already suck on memory.
115
 
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
116
 
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
117
 
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
118
 
    # replaced startswith with slice lookups:
119
 
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
120
 
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
121
 
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
122
 
                  
123
 
    from weave import WeaveFormatError
124
 
 
125
 
    lines = iter(f.readlines())
126
 
    
127
 
    l = lines.next()
 
98
    return read_weave_v5(f)
 
99
 
 
100
 
 
101
def read_weave_v5(f):
 
102
    from weave import Weave, WeaveFormatError
 
103
    w = Weave()
 
104
 
 
105
    wfe = WeaveFormatError
 
106
    l = f.readline()
128
107
    if l != FORMAT_1:
129
108
        raise WeaveFormatError('invalid weave file header: %r' % l)
130
109
 
131
110
    ver = 0
132
 
    # read weave header.
133
111
    while True:
134
 
        l = lines.next()
 
112
        l = f.readline()
135
113
        if l[0] == 'i':
136
114
            if len(l) > 2:
137
115
                w._parents.append(map(int, l[2:].split(' ')))
138
116
            else:
139
117
                w._parents.append([])
140
118
 
141
 
            l = lines.next()[:-1]
142
 
            assert '1 ' == l[0:2]
 
119
            l = f.readline()[:-1]
 
120
            assert l.startswith('1 ')
143
121
            w._sha1s.append(l[2:])
144
122
                
145
 
            l = lines.next()
146
 
            assert 'n ' == l[0:2]
 
123
            l = f.readline()
 
124
            assert l.startswith('n ')
147
125
            name = l[2:-1]
148
126
            assert name not in w._name_map
149
127
            w._names.append(name)
150
128
            w._name_map[name] = ver
151
129
                
152
 
            l = lines.next()
 
130
            l = f.readline()
153
131
            assert l == '\n'
154
132
 
155
133
            ver += 1
158
136
        else:
159
137
            raise WeaveFormatError('unexpected line %r' % l)
160
138
 
161
 
    # read weave body
162
139
    while True:
163
 
        l = lines.next()
 
140
        l = f.readline()
164
141
        if l == 'W\n':
165
142
            break
166
 
        elif '. ' == l[0:2]:
 
143
        elif l.startswith('. '):
167
144
            w._weave.append(l[2:])  # include newline
168
 
        elif ', ' == l[0:2]:
 
145
        elif l.startswith(', '):
169
146
            w._weave.append(l[2:-1])        # exclude newline
170
147
        elif l == '}\n':
171
148
            w._weave.append(('}', None))