~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Patch Queue Manager
  • Date: 2013-05-23 10:35:23 UTC
  • mfrom: (6574.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20130523103523-2wt6jmauja1n1vdt
(jameinel) Merge bzr/2.5 into trunk. (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
#
17
17
# Author: Martin Pool <mbp@canonical.com>
18
18
 
19
 
 
20
 
 
21
 
 
22
19
"""Store and retrieve weaves in files.
23
20
 
24
21
There is one format marker followed by a blank line, followed by a
37
34
line contains a newline, or ',' if not.
38
35
"""
39
36
 
 
37
from __future__ import absolute_import
 
38
 
40
39
# TODO: When extracting a single version it'd be enough to just pass
41
40
# an iterator returning the weave lines...  We don't really need to
42
41
# deserialize it into memory.
45
44
 
46
45
 
47
46
def write_weave(weave, f, format=None):
48
 
    if format == None or format == 1:
 
47
    if format is None or format == 1:
49
48
        return write_weave_v5(weave, f)
50
49
    else:
51
50
        raise ValueError("unknown weave format %r" % format)
53
52
 
54
53
def write_weave_v5(weave, f):
55
54
    """Write weave to file f."""
56
 
    print >>f, FORMAT_1,
 
55
    f.write(FORMAT_1)
57
56
 
58
57
    for version, included in enumerate(weave._parents):
59
58
        if included:
60
59
            # mininc = weave.minimal_parents(version)
61
60
            mininc = included
62
 
            print >>f, 'i',
63
 
            for i in mininc:
64
 
                print >>f, i,
65
 
            print >>f
 
61
            f.write('i ')
 
62
            f.write(' '.join(str(i) for i in mininc))
 
63
            f.write('\n')
66
64
        else:
67
 
            print >>f, 'i'
68
 
        print >>f, '1', weave._sha1s[version]
69
 
        print >>f, 'n', weave._names[version]
70
 
        print >>f
 
65
            f.write('i\n')
 
66
        f.write('1 ' + weave._sha1s[version] + '\n')
 
67
        f.write('n ' + weave._names[version] + '\n')
 
68
        f.write('\n')
71
69
 
72
 
    print >>f, 'w'
 
70
    f.write('w\n')
73
71
 
74
72
    for l in weave._weave:
75
73
        if isinstance(l, tuple):
76
 
            assert l[0] in '{}[]'
77
74
            if l[0] == '}':
78
 
                print >>f, '}'
 
75
                f.write('}\n')
79
76
            else:
80
 
                print >>f, '%s %d' % l
 
77
                f.write('%s %d\n' % l)
81
78
        else: # text line
82
79
            if not l:
83
 
                print >>f, ', '
 
80
                f.write(', \n')
84
81
            elif l[-1] == '\n':
85
 
                assert l.find('\n', 0, -1) == -1
86
 
                print >>f, '.', l,
 
82
                f.write('. ' + l)
87
83
            else:
88
 
                assert l.find('\n') == -1
89
 
                print >>f, ',', l
 
84
                f.write(', ' + l + '\n')
90
85
 
91
 
    print >>f, 'W'
 
86
    f.write('W\n')
92
87
 
93
88
 
94
89
 
95
90
def read_weave(f):
96
91
    # FIXME: detect the weave type and dispatch
97
 
    from bzrlib.trace import mutter
98
 
    from weave import Weave
 
92
    from bzrlib.weave import Weave
99
93
    w = Weave(getattr(f, 'name', None))
100
94
    _read_weave_v5(f, w)
101
95
    return w
103
97
 
104
98
def _read_weave_v5(f, w):
105
99
    """Private helper routine to read a weave format 5 file into memory.
106
 
    
 
100
 
107
101
    This is only to be used by read_weave and WeaveFile.__init__.
108
102
    """
109
103
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
119
113
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
120
114
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
121
115
    # +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()
 
116
 
 
117
    from bzrlib.weave import WeaveFormatError
 
118
 
 
119
    try:
 
120
        lines = iter(f.readlines())
 
121
    finally:
 
122
        f.close()
 
123
 
 
124
    try:
 
125
        l = lines.next()
 
126
    except StopIteration:
 
127
        raise WeaveFormatError('invalid weave file: no header')
 
128
 
128
129
    if l != FORMAT_1:
129
130
        raise WeaveFormatError('invalid weave file header: %r' % l)
130
131
 
137
138
                w._parents.append(map(int, l[2:].split(' ')))
138
139
            else:
139
140
                w._parents.append([])
140
 
 
141
141
            l = lines.next()[:-1]
142
 
            assert '1 ' == l[0:2]
143
142
            w._sha1s.append(l[2:])
144
 
                
145
143
            l = lines.next()
146
 
            assert 'n ' == l[0:2]
147
144
            name = l[2:-1]
148
 
            assert name not in w._name_map
149
145
            w._names.append(name)
150
146
            w._name_map[name] = ver
151
 
                
152
147
            l = lines.next()
153
 
            assert l == '\n'
154
 
 
155
148
            ver += 1
156
149
        elif l == 'w\n':
157
150
            break
170
163
        elif l == '}\n':
171
164
            w._weave.append(('}', None))
172
165
        else:
173
 
            assert l[0] in '{[]', l
174
 
            assert l[1] == ' ', l
175
166
            w._weave.append((intern(l[0]), int(l[2:])))
176
 
 
177
167
    return w
178