/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: Martin Pool
  • Date: 2005-08-24 08:59:32 UTC
  • Revision ID: mbp@sourcefrog.net-20050824085932-c61f1f1f1c930e13
- Add a simple UIFactory 

  The idea of this is to let a client of bzrlib set some 
  policy about how output is displayed.

  In this revision all that's done is that progress bars
  are constructed by a policy established by the application
  rather than being randomly constructed in the library 
  or passed down the calls.  This avoids progress bars
  popping up while running the test suite and cleans up
  some code.

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
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
7
 
#
 
9
 
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
13
# GNU General Public License for more details.
12
 
#
 
14
 
13
15
# You should have received a copy of the GNU General Public License
14
16
# 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
 
#
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
17
19
# Author: Martin Pool <mbp@canonical.com>
18
20
 
19
21
 
45
47
 
46
48
 
47
49
def write_weave(weave, f, format=None):
48
 
    if format is None or format == 1:
 
50
    if format == None or format == 1:
49
51
        return write_weave_v5(weave, f)
50
52
    else:
51
53
        raise ValueError("unknown weave format %r" % format)
53
55
 
54
56
def write_weave_v5(weave, f):
55
57
    """Write weave to file f."""
56
 
    f.write(FORMAT_1)
 
58
    print >>f, FORMAT_1,
57
59
 
58
60
    for version, included in enumerate(weave._parents):
59
61
        if included:
60
62
            # mininc = weave.minimal_parents(version)
61
63
            mininc = included
62
 
            f.write('i ')
63
 
            f.write(' '.join(str(i) for i in mininc))
64
 
            f.write('\n')
 
64
            print >>f, 'i',
 
65
            for i in mininc:
 
66
                print >>f, i,
 
67
            print >>f
65
68
        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')
 
69
            print >>f, 'i'
 
70
        print >>f, '1', weave._sha1s[version]
 
71
        print >>f, 'n', weave._names[version]
 
72
        print >>f
70
73
 
71
 
    f.write('w\n')
 
74
    print >>f, 'w'
72
75
 
73
76
    for l in weave._weave:
74
77
        if isinstance(l, tuple):
 
78
            assert l[0] in '{}[]'
75
79
            if l[0] == '}':
76
 
                f.write('}\n')
 
80
                print >>f, '}'
77
81
            else:
78
 
                f.write('%s %d\n' % l)
 
82
                print >>f, '%s %d' % l
79
83
        else: # text line
80
84
            if not l:
81
 
                f.write(', \n')
 
85
                print >>f, ', '
82
86
            elif l[-1] == '\n':
83
 
                f.write('. ' + l)
 
87
                assert l.find('\n', 0, -1) == -1
 
88
                print >>f, '.', l,
84
89
            else:
85
 
                f.write(', ' + l + '\n')
 
90
                assert l.find('\n') == -1
 
91
                print >>f, ',', l
86
92
 
87
 
    f.write('W\n')
 
93
    print >>f, 'W'
88
94
 
89
95
 
90
96
 
91
97
def read_weave(f):
92
 
    # FIXME: detect the weave type and dispatch
93
 
    from bzrlib.trace import mutter
94
 
    from weave import Weave
95
 
    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
 
 
 
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
    l = f.readline()
128
106
    if l != FORMAT_1:
129
107
        raise WeaveFormatError('invalid weave file header: %r' % l)
130
108
 
131
109
    ver = 0
132
 
    # read weave header.
133
110
    while True:
134
 
        l = lines.next()
 
111
        l = f.readline()
135
112
        if l[0] == 'i':
136
113
            if len(l) > 2:
137
114
                w._parents.append(map(int, l[2:].split(' ')))
138
115
            else:
139
116
                w._parents.append([])
140
 
            l = lines.next()[:-1]
 
117
 
 
118
            l = f.readline()[:-1]
 
119
            assert l.startswith('1 ')
141
120
            w._sha1s.append(l[2:])
142
 
            l = lines.next()
 
121
                
 
122
            l = f.readline()
 
123
            assert l.startswith('n ')
143
124
            name = l[2:-1]
 
125
            assert name not in w._name_map
144
126
            w._names.append(name)
145
127
            w._name_map[name] = ver
146
 
            l = lines.next()
 
128
                
 
129
            l = f.readline()
 
130
            assert l == '\n'
 
131
 
147
132
            ver += 1
148
133
        elif l == 'w\n':
149
134
            break
150
135
        else:
151
136
            raise WeaveFormatError('unexpected line %r' % l)
152
137
 
153
 
    # read weave body
154
138
    while True:
155
 
        l = lines.next()
 
139
        l = f.readline()
156
140
        if l == 'W\n':
157
141
            break
158
 
        elif '. ' == l[0:2]:
 
142
        elif l.startswith('. '):
159
143
            w._weave.append(l[2:])  # include newline
160
 
        elif ', ' == l[0:2]:
 
144
        elif l.startswith(', '):
161
145
            w._weave.append(l[2:-1])        # exclude newline
162
146
        elif l == '}\n':
163
147
            w._weave.append(('}', None))
164
148
        else:
 
149
            assert l[0] in '{[]', l
 
150
            assert l[1] == ' ', l
165
151
            w._weave.append((intern(l[0]), int(l[2:])))
 
152
 
166
153
    return w
 
154