/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 breezy/bzr/weavefile.py

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
#
 
17
# Author: Martin Pool <mbp@canonical.com>
 
18
 
 
19
"""Store and retrieve weaves in files.
 
20
 
 
21
There is one format marker followed by a blank line, followed by a
 
22
series of version headers, followed by the weave itself.
 
23
 
 
24
Each version marker has
 
25
 
 
26
 'i'   parent version indexes
 
27
 '1'   SHA-1 of text
 
28
 'n'   name
 
29
 
 
30
The inclusions do not need to list versions included by a parent.
 
31
 
 
32
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
 
33
processing instructions.  Lines of text are prefixed by '.' if the
 
34
line contains a newline, or ',' if not.
 
35
"""
 
36
 
 
37
from __future__ import absolute_import
 
38
 
 
39
from ..sixish import bytesintern
 
40
 
 
41
# TODO: When extracting a single version it'd be enough to just pass
 
42
# an iterator returning the weave lines...  We don't really need to
 
43
# deserialize it into memory.
 
44
 
 
45
FORMAT_1 = b'# bzr weave file v5\n'
 
46
 
 
47
 
 
48
def write_weave(weave, f, format=None):
 
49
    if format is None or format == 1:
 
50
        return write_weave_v5(weave, f)
 
51
    else:
 
52
        raise ValueError("unknown weave format %r" % format)
 
53
 
 
54
 
 
55
def write_weave_v5(weave, f):
 
56
    """Write weave to file f."""
 
57
    f.write(FORMAT_1)
 
58
 
 
59
    for version, included in enumerate(weave._parents):
 
60
        if included:
 
61
            # mininc = weave.minimal_parents(version)
 
62
            mininc = included
 
63
            f.write(b'i ')
 
64
            f.write(b' '.join(b'%d' % i for i in mininc))
 
65
            f.write(b'\n')
 
66
        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')
 
71
 
 
72
    f.write(b'w\n')
 
73
 
 
74
    for l in weave._weave:
 
75
        if isinstance(l, tuple):
 
76
            if l[0] == b'}':
 
77
                f.write(b'}\n')
 
78
            else:
 
79
                f.write(l[0] + b' %d\n' % l[1])
 
80
        else:  # text line
 
81
            if not l:
 
82
                f.write(b', \n')
 
83
            elif l.endswith(b'\n'):
 
84
                f.write(b'. ' + l)
 
85
            else:
 
86
                f.write(b', ' + l + b'\n')
 
87
 
 
88
    f.write(b'W\n')
 
89
 
 
90
 
 
91
def read_weave(f):
 
92
    # FIXME: detect the weave type and dispatch
 
93
    from .weave import Weave
 
94
    w = Weave(getattr(f, 'name', None))
 
95
    _read_weave_v5(f, w)
 
96
    return w
 
97
 
 
98
 
 
99
def _read_weave_v5(f, w):
 
100
    """Private helper routine to read a weave format 5 file into memory.
 
101
 
 
102
    This is only to be used by read_weave and WeaveFile.__init__.
 
103
    """
 
104
    #  200   0   2075.5080   1084.0360   breezy.weavefile:104(_read_weave_v5)
 
105
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
 
106
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
 
107
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
 
108
    # replace readline call with iter over all lines ->
 
109
    # safe because we already suck on memory.
 
110
    #  200   0   1492.7170    802.6220   breezy.weavefile:104(_read_weave_v5)
 
111
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
 
112
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
 
113
    # replaced startswith with slice lookups:
 
114
    #  200   0    851.7250    501.1120   breezy.weavefile:104(_read_weave_v5)
 
115
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
 
116
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
 
117
 
 
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)
 
127
    except StopIteration:
 
128
        raise WeaveFormatError('invalid weave file: no header')
 
129
 
 
130
    if l != FORMAT_1:
 
131
        raise WeaveFormatError('invalid weave file header: %r' % l)
 
132
 
 
133
    ver = 0
 
134
    # read weave header.
 
135
    while True:
 
136
        l = next(lines)
 
137
        if l[0:1] == b'i':
 
138
            if len(l) > 2:
 
139
                w._parents.append(list(map(int, l[2:].split(b' '))))
 
140
            else:
 
141
                w._parents.append([])
 
142
            l = next(lines)[:-1]
 
143
            w._sha1s.append(l[2:])
 
144
            l = next(lines)
 
145
            name = l[2:-1]
 
146
            w._names.append(name)
 
147
            w._name_map[name] = ver
 
148
            l = next(lines)
 
149
            ver += 1
 
150
        elif l == b'w\n':
 
151
            break
 
152
        else:
 
153
            raise WeaveFormatError('unexpected line %r' % l)
 
154
 
 
155
    # read weave body
 
156
    while True:
 
157
        l = next(lines)
 
158
        if l == b'W\n':
 
159
            break
 
160
        elif b'. ' == l[0:2]:
 
161
            w._weave.append(l[2:])  # include newline
 
162
        elif b', ' == l[0:2]:
 
163
            w._weave.append(l[2:-1])        # exclude newline
 
164
        elif l == b'}\n':
 
165
            w._weave.append((b'}', None))
 
166
        else:
 
167
            w._weave.append((bytesintern(l[0:1]), int(l[2:].decode('ascii'))))
 
168
    return w