/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: Michael Ellerman
  • Date: 2006-03-09 00:24:48 UTC
  • mto: (1610.1.8 bzr.mbp.integration)
  • mto: This revision was merged to the branch mainline in revision 1616.
  • Revision ID: michael@ellerman.id.au-20060309002448-70cce15e3d605130
Make the "ignore line" in the commit message editor the "right" width, so
that if you make your message that wide it won't wrap in bzr log output.
Just as a visual aid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Canonical Ltd
2
 
#
 
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
19
 
45
45
 
46
46
 
47
47
def write_weave(weave, f, format=None):
48
 
    if format is None or format == 1:
 
48
    if format == None or format == 1:
49
49
        return write_weave_v5(weave, f)
50
50
    else:
51
51
        raise ValueError("unknown weave format %r" % format)
53
53
 
54
54
def write_weave_v5(weave, f):
55
55
    """Write weave to file f."""
56
 
    f.write(FORMAT_1)
 
56
    print >>f, FORMAT_1,
57
57
 
58
58
    for version, included in enumerate(weave._parents):
59
59
        if included:
60
60
            # mininc = weave.minimal_parents(version)
61
61
            mininc = included
62
 
            f.write('i ')
63
 
            f.write(' '.join(str(i) for i in mininc))
64
 
            f.write('\n')
 
62
            print >>f, 'i',
 
63
            for i in mininc:
 
64
                print >>f, i,
 
65
            print >>f
65
66
        else:
66
 
            f.write('i\n')
67
 
        f.write('1 ' + weave._sha1s[version] + '\n')
68
 
        f.write('n ' + weave._names[version] + '\n')
69
 
        f.write('\n')
 
67
            print >>f, 'i'
 
68
        print >>f, '1', weave._sha1s[version]
 
69
        print >>f, 'n', weave._names[version]
 
70
        print >>f
70
71
 
71
 
    f.write('w\n')
 
72
    print >>f, 'w'
72
73
 
73
74
    for l in weave._weave:
74
75
        if isinstance(l, tuple):
 
76
            assert l[0] in '{}[]'
75
77
            if l[0] == '}':
76
 
                f.write('}\n')
 
78
                print >>f, '}'
77
79
            else:
78
 
                f.write('%s %d\n' % l)
 
80
                print >>f, '%s %d' % l
79
81
        else: # text line
80
82
            if not l:
81
 
                f.write(', \n')
 
83
                print >>f, ', '
82
84
            elif l[-1] == '\n':
83
 
                f.write('. ' + l)
 
85
                assert l.find('\n', 0, -1) == -1
 
86
                print >>f, '.', l,
84
87
            else:
85
 
                f.write(', ' + l + '\n')
86
 
 
87
 
    f.write('W\n')
88
 
 
89
 
 
90
 
 
91
 
def read_weave(f):
92
 
    # FIXME: detect the weave type and dispatch
93
 
    from bzrlib.trace import mutter
94
 
    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
95
101
    w = Weave(getattr(f, 'name', None))
96
 
    _read_weave_v5(f, w)
97
 
    return w
98
 
 
99
 
 
100
 
def _read_weave_v5(f, w):
101
 
    """Private helper routine to read a weave format 5 file into memory.
102
 
 
103
 
    This is only to be used by read_weave and WeaveFile.__init__.
104
 
    """
105
 
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
106
 
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
107
 
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
108
 
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
109
 
    # replace readline call with iter over all lines ->
110
 
    # safe because we already suck on memory.
111
 
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
112
 
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
113
 
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
114
 
    # replaced startswith with slice lookups:
115
 
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
116
 
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
117
 
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
118
 
 
119
 
    from weave import WeaveFormatError
120
 
 
121
 
    lines = iter(f.readlines())
122
 
 
123
 
    try:
124
 
        l = lines.next()
125
 
    except StopIteration:
126
 
        raise WeaveFormatError('invalid weave file: no header')
127
 
 
 
102
 
 
103
    l = f.readline()
128
104
    if l != FORMAT_1:
129
105
        raise WeaveFormatError('invalid weave file header: %r' % l)
130
106
 
131
107
    ver = 0
132
 
    # read weave header.
133
108
    while True:
134
 
        l = lines.next()
 
109
        l = f.readline()
135
110
        if l[0] == 'i':
136
111
            if len(l) > 2:
137
112
                w._parents.append(map(int, l[2:].split(' ')))
138
113
            else:
139
114
                w._parents.append([])
140
 
            l = lines.next()[:-1]
 
115
 
 
116
            l = f.readline()[:-1]
 
117
            assert l.startswith('1 ')
141
118
            w._sha1s.append(l[2:])
142
 
            l = lines.next()
 
119
                
 
120
            l = f.readline()
 
121
            assert l.startswith('n ')
143
122
            name = l[2:-1]
 
123
            assert name not in w._name_map
144
124
            w._names.append(name)
145
125
            w._name_map[name] = ver
146
 
            l = lines.next()
 
126
                
 
127
            l = f.readline()
 
128
            assert l == '\n'
 
129
 
147
130
            ver += 1
148
131
        elif l == 'w\n':
149
132
            break
150
133
        else:
151
134
            raise WeaveFormatError('unexpected line %r' % l)
152
135
 
153
 
    # read weave body
 
136
    if prelude:
 
137
        return w
 
138
 
154
139
    while True:
155
 
        l = lines.next()
 
140
        l = f.readline()
156
141
        if l == 'W\n':
157
142
            break
158
 
        elif '. ' == l[0:2]:
 
143
        elif l.startswith('. '):
159
144
            w._weave.append(l[2:])  # include newline
160
 
        elif ', ' == l[0:2]:
 
145
        elif l.startswith(', '):
161
146
            w._weave.append(l[2:-1])        # exclude newline
162
147
        elif l == '}\n':
163
148
            w._weave.append(('}', None))
164
149
        else:
 
150
            assert l[0] in '{[]', l
 
151
            assert l[1] == ' ', l
165
152
            w._weave.append((intern(l[0]), int(l[2:])))
 
153
 
166
154
    return w
 
155