/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: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
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
16
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
34
37
line contains a newline, or ',' if not.
35
38
"""
36
39
 
37
 
from __future__ import absolute_import
38
 
 
39
 
from ..sixish import bytesintern
40
 
 
41
40
# TODO: When extracting a single version it'd be enough to just pass
42
41
# an iterator returning the weave lines...  We don't really need to
43
42
# deserialize it into memory.
44
43
 
45
 
FORMAT_1 = b'# bzr weave file v5\n'
 
44
FORMAT_1 = '# bzr weave file v5\n'
46
45
 
47
46
 
48
47
def write_weave(weave, f, format=None):
60
59
        if included:
61
60
            # mininc = weave.minimal_parents(version)
62
61
            mininc = included
63
 
            f.write(b'i ')
64
 
            f.write(b' '.join(b'%d' % i for i in mininc))
65
 
            f.write(b'\n')
 
62
            f.write('i ')
 
63
            f.write(' '.join(str(i) for i in mininc))
 
64
            f.write('\n')
66
65
        else:
67
 
            f.write(b'i\n')
68
 
        f.write(b'1 ' + weave._sha1s[version] + b'\n')
69
 
        f.write(b'n ' + weave._names[version] + b'\n')
70
 
        f.write(b'\n')
 
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')
71
70
 
72
 
    f.write(b'w\n')
 
71
    f.write('w\n')
73
72
 
74
73
    for l in weave._weave:
75
74
        if isinstance(l, tuple):
76
 
            if l[0] == b'}':
77
 
                f.write(b'}\n')
 
75
            if l[0] == '}':
 
76
                f.write('}\n')
78
77
            else:
79
 
                f.write(l[0] + b' %d\n' % l[1])
80
 
        else:  # text line
 
78
                f.write('%s %d\n' % l)
 
79
        else: # text line
81
80
            if not l:
82
 
                f.write(b', \n')
83
 
            elif l.endswith(b'\n'):
84
 
                f.write(b'. ' + l)
 
81
                f.write(', \n')
 
82
            elif l[-1] == '\n':
 
83
                f.write('. ' + l)
85
84
            else:
86
 
                f.write(b', ' + l + b'\n')
87
 
 
88
 
    f.write(b'W\n')
 
85
                f.write(', ' + l + '\n')
 
86
 
 
87
    f.write('W\n')
 
88
 
89
89
 
90
90
 
91
91
def read_weave(f):
92
92
    # FIXME: detect the weave type and dispatch
93
 
    from .weave import Weave
 
93
    from bzrlib.trace import mutter
 
94
    from weave import Weave
94
95
    w = Weave(getattr(f, 'name', None))
95
96
    _read_weave_v5(f, w)
96
97
    return w
101
102
 
102
103
    This is only to be used by read_weave and WeaveFile.__init__.
103
104
    """
104
 
    #  200   0   2075.5080   1084.0360   breezy.weavefile:104(_read_weave_v5)
 
105
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
105
106
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
106
107
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
107
108
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
108
109
    # replace readline call with iter over all lines ->
109
110
    # safe because we already suck on memory.
110
 
    #  200   0   1492.7170    802.6220   breezy.weavefile:104(_read_weave_v5)
 
111
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
111
112
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
112
113
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
113
114
    # replaced startswith with slice lookups:
114
 
    #  200   0    851.7250    501.1120   breezy.weavefile:104(_read_weave_v5)
 
115
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
115
116
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
116
117
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
117
118
 
118
 
    from .weave import WeaveFormatError
119
 
 
120
 
    try:
121
 
        lines = iter(f.readlines())
122
 
    finally:
123
 
        f.close()
124
 
 
125
 
    try:
126
 
        l = next(lines)
 
119
    from weave import WeaveFormatError
 
120
 
 
121
    lines = iter(f.readlines())
 
122
 
 
123
    try:
 
124
        l = lines.next()
127
125
    except StopIteration:
128
126
        raise WeaveFormatError('invalid weave file: no header')
129
127
 
133
131
    ver = 0
134
132
    # read weave header.
135
133
    while True:
136
 
        l = next(lines)
137
 
        if l[0:1] == b'i':
 
134
        l = lines.next()
 
135
        if l[0] == 'i':
138
136
            if len(l) > 2:
139
 
                w._parents.append(list(map(int, l[2:].split(b' '))))
 
137
                w._parents.append(map(int, l[2:].split(' ')))
140
138
            else:
141
139
                w._parents.append([])
142
 
            l = next(lines)[:-1]
 
140
            l = lines.next()[:-1]
143
141
            w._sha1s.append(l[2:])
144
 
            l = next(lines)
 
142
            l = lines.next()
145
143
            name = l[2:-1]
146
144
            w._names.append(name)
147
145
            w._name_map[name] = ver
148
 
            l = next(lines)
 
146
            l = lines.next()
149
147
            ver += 1
150
 
        elif l == b'w\n':
 
148
        elif l == 'w\n':
151
149
            break
152
150
        else:
153
151
            raise WeaveFormatError('unexpected line %r' % l)
154
152
 
155
153
    # read weave body
156
154
    while True:
157
 
        l = next(lines)
158
 
        if l == b'W\n':
 
155
        l = lines.next()
 
156
        if l == 'W\n':
159
157
            break
160
 
        elif b'. ' == l[0:2]:
 
158
        elif '. ' == l[0:2]:
161
159
            w._weave.append(l[2:])  # include newline
162
 
        elif b', ' == l[0:2]:
 
160
        elif ', ' == l[0:2]:
163
161
            w._weave.append(l[2:-1])        # exclude newline
164
 
        elif l == b'}\n':
165
 
            w._weave.append((b'}', None))
 
162
        elif l == '}\n':
 
163
            w._weave.append(('}', None))
166
164
        else:
167
 
            w._weave.append((bytesintern(l[0:1]), int(l[2:].decode('ascii'))))
 
165
            w._weave.append((intern(l[0]), int(l[2:])))
168
166
    return w