/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Robert Collins
  • Date: 2005-12-24 02:20:45 UTC
  • mto: (1185.50.57 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1550.
  • Revision ID: robertc@robertcollins.net-20051224022045-14efc8dfa0e1a4e9
Start tests for api usage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
#
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
17
17
# Author: Martin Pool <mbp@canonical.com>
18
18
 
 
19
 
 
20
 
 
21
 
19
22
"""Store and retrieve weaves in files.
20
23
 
21
24
There is one format marker followed by a blank line, followed by a
38
41
# an iterator returning the weave lines...  We don't really need to
39
42
# deserialize it into memory.
40
43
 
41
 
FORMAT_1 = b'# bzr weave file v5\n'
 
44
FORMAT_1 = '# bzr weave file v5\n'
42
45
 
43
46
 
44
47
def write_weave(weave, f, format=None):
45
 
    if format is None or format == 1:
 
48
    if format == None or format == 1:
46
49
        return write_weave_v5(weave, f)
47
50
    else:
48
51
        raise ValueError("unknown weave format %r" % format)
50
53
 
51
54
def write_weave_v5(weave, f):
52
55
    """Write weave to file f."""
53
 
    f.write(FORMAT_1)
 
56
    print >>f, FORMAT_1,
54
57
 
55
58
    for version, included in enumerate(weave._parents):
56
59
        if included:
57
60
            # mininc = weave.minimal_parents(version)
58
61
            mininc = included
59
 
            f.write(b'i ')
60
 
            f.write(b' '.join(b'%d' % i for i in mininc))
61
 
            f.write(b'\n')
 
62
            print >>f, 'i',
 
63
            for i in mininc:
 
64
                print >>f, i,
 
65
            print >>f
62
66
        else:
63
 
            f.write(b'i\n')
64
 
        f.write(b'1 ' + weave._sha1s[version] + b'\n')
65
 
        f.write(b'n ' + weave._names[version] + b'\n')
66
 
        f.write(b'\n')
 
67
            print >>f, 'i'
 
68
        print >>f, '1', weave._sha1s[version]
 
69
        print >>f, 'n', weave._names[version]
 
70
        print >>f
67
71
 
68
 
    f.write(b'w\n')
 
72
    print >>f, 'w'
69
73
 
70
74
    for l in weave._weave:
71
75
        if isinstance(l, tuple):
72
 
            if l[0] == b'}':
73
 
                f.write(b'}\n')
 
76
            assert l[0] in '{}[]'
 
77
            if l[0] == '}':
 
78
                print >>f, '}'
74
79
            else:
75
 
                f.write(l[0] + b' %d\n' % l[1])
76
 
        else:  # text line
 
80
                print >>f, '%s %d' % l
 
81
        else: # text line
77
82
            if not l:
78
 
                f.write(b', \n')
79
 
            elif l.endswith(b'\n'):
80
 
                f.write(b'. ' + l)
 
83
                print >>f, ', '
 
84
            elif l[-1] == '\n':
 
85
                assert l.find('\n', 0, -1) == -1
 
86
                print >>f, '.', l,
81
87
            else:
82
 
                f.write(b', ' + l + b'\n')
83
 
 
84
 
    f.write(b'W\n')
85
 
 
86
 
 
87
 
def read_weave(f):
88
 
    # FIXME: detect the weave type and dispatch
89
 
    from .weave import Weave
 
88
                assert l.find('\n') == -1
 
89
                print >>f, ',', l
 
90
 
 
91
    print >>f, 'W'
 
92
 
 
93
 
 
94
 
 
95
def read_weave(f,prelude=False):
 
96
    return read_weave_v5(f,prelude=prelude)
 
97
 
 
98
 
 
99
def read_weave_v5(f,prelude=False):
 
100
    from weave import Weave, WeaveFormatError
90
101
    w = Weave(getattr(f, 'name', None))
91
 
    _read_weave_v5(f, w)
92
 
    return w
93
 
 
94
 
 
95
 
def _read_weave_v5(f, w):
96
 
    """Private helper routine to read a weave format 5 file into memory.
97
 
 
98
 
    This is only to be used by read_weave and WeaveFile.__init__.
99
 
    """
100
 
    #  200   0   2075.5080   1084.0360   breezy.weavefile:104(_read_weave_v5)
101
 
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
102
 
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
103
 
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
104
 
    # replace readline call with iter over all lines ->
105
 
    # safe because we already suck on memory.
106
 
    #  200   0   1492.7170    802.6220   breezy.weavefile:104(_read_weave_v5)
107
 
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
108
 
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
109
 
    # replaced startswith with slice lookups:
110
 
    #  200   0    851.7250    501.1120   breezy.weavefile:104(_read_weave_v5)
111
 
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
112
 
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
113
 
 
114
 
    from .weave import WeaveFormatError
115
 
 
116
 
    try:
117
 
        lines = iter(f.readlines())
118
 
    finally:
119
 
        f.close()
120
 
 
121
 
    try:
122
 
        l = next(lines)
123
 
    except StopIteration:
124
 
        raise WeaveFormatError('invalid weave file: no header')
125
 
 
 
102
 
 
103
    l = f.readline()
126
104
    if l != FORMAT_1:
127
105
        raise WeaveFormatError('invalid weave file header: %r' % l)
128
106
 
129
107
    ver = 0
130
 
    # read weave header.
131
108
    while True:
132
 
        l = next(lines)
133
 
        if l[0:1] == b'i':
 
109
        l = f.readline()
 
110
        if l[0] == 'i':
134
111
            if len(l) > 2:
135
 
                w._parents.append(list(map(int, l[2:].split(b' '))))
 
112
                w._parents.append(map(int, l[2:].split(' ')))
136
113
            else:
137
114
                w._parents.append([])
138
 
            l = next(lines)[:-1]
 
115
 
 
116
            l = f.readline()[:-1]
 
117
            assert l.startswith('1 ')
139
118
            w._sha1s.append(l[2:])
140
 
            l = next(lines)
 
119
                
 
120
            l = f.readline()
 
121
            assert l.startswith('n ')
141
122
            name = l[2:-1]
 
123
            assert name not in w._name_map
142
124
            w._names.append(name)
143
125
            w._name_map[name] = ver
144
 
            l = next(lines)
 
126
                
 
127
            l = f.readline()
 
128
            assert l == '\n'
 
129
 
145
130
            ver += 1
146
 
        elif l == b'w\n':
 
131
        elif l == 'w\n':
147
132
            break
148
133
        else:
149
134
            raise WeaveFormatError('unexpected line %r' % l)
150
135
 
151
 
    # read weave body
 
136
    if prelude:
 
137
        return w
 
138
 
152
139
    while True:
153
 
        l = next(lines)
154
 
        if l == b'W\n':
 
140
        l = f.readline()
 
141
        if l == 'W\n':
155
142
            break
156
 
        elif b'. ' == l[0:2]:
 
143
        elif l.startswith('. '):
157
144
            w._weave.append(l[2:])  # include newline
158
 
        elif b', ' == l[0:2]:
 
145
        elif l.startswith(', '):
159
146
            w._weave.append(l[2:-1])        # exclude newline
160
 
        elif l == b'}\n':
161
 
            w._weave.append((b'}', None))
 
147
        elif l == '}\n':
 
148
            w._weave.append(('}', None))
162
149
        else:
163
 
            w._weave.append((l[0:1], int(l[2:].decode('ascii'))))
 
150
            assert l[0] in '{[]', l
 
151
            assert l[1] == ' ', l
 
152
            w._weave.append((intern(l[0]), int(l[2:])))
 
153
 
164
154
    return w
 
155