~bzr-pqm/bzr/bzr.dev

5247.1.4 by Vincent Ladeuil
Merge cleanup into first-try
1
# Copyright (C) 2005-2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
16
#
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
17
# Author: Martin Pool <mbp@canonical.com>
18
19
"""Store and retrieve weaves in files.
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
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
1083 by Martin Pool
- add space to store revision-id in weave files
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.
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
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.
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
35
"""
36
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
37
from __future__ import absolute_import
38
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
39
# TODO: When extracting a single version it'd be enough to just pass
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
40
# an iterator returning the weave lines...  We don't really need to
41
# deserialize it into memory.
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
42
1083 by Martin Pool
- add space to store revision-id in weave files
43
FORMAT_1 = '# bzr weave file v5\n'
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
44
45
0.1.74 by Martin Pool
Add format-hidden readwrite methods
46
def write_weave(weave, f, format=None):
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
47
    if format is None or format == 1:
1083 by Martin Pool
- add space to store revision-id in weave files
48
        return write_weave_v5(weave, f)
0.1.74 by Martin Pool
Add format-hidden readwrite methods
49
    else:
50
        raise ValueError("unknown weave format %r" % format)
51
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
52
1083 by Martin Pool
- add space to store revision-id in weave files
53
def write_weave_v5(weave, f):
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
54
    """Write weave to file f."""
2911.6.3 by Blake Winton
Implemented suggestions from John Arbash Meinel.
55
    f.write(FORMAT_1)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
56
944 by Martin Pool
- refactor member names in Weave code
57
    for version, included in enumerate(weave._parents):
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
58
        if included:
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
59
            # mininc = weave.minimal_parents(version)
60
            mininc = included
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
61
            f.write('i ')
2911.6.4 by Blake Winton
Fix test failures
62
            f.write(' '.join(str(i) for i in mininc))
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
63
            f.write('\n')
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
64
        else:
2911.6.2 by Blake Winton
Fix typos.
65
            f.write('i\n')
66
        f.write('1 ' + weave._sha1s[version] + '\n')
67
        f.write('n ' + weave._names[version] + '\n')
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
68
        f.write('\n')
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
69
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
70
    f.write('w\n')
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
71
944 by Martin Pool
- refactor member names in Weave code
72
    for l in weave._weave:
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
73
        if isinstance(l, tuple):
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
74
            if l[0] == '}':
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
75
                f.write('}\n')
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
76
            else:
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
77
                f.write('%s %d\n' % l)
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
78
        else: # text line
79
            if not l:
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
80
                f.write(', \n')
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
81
            elif l[-1] == '\n':
2911.6.4 by Blake Winton
Fix test failures
82
                f.write('. ' + l)
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
83
            else:
2911.6.2 by Blake Winton
Fix typos.
84
                f.write(', ' + l + '\n')
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
85
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
86
    f.write('W\n')
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
87
88
0.1.74 by Martin Pool
Add format-hidden readwrite methods
89
1563.2.14 by Robert Collins
Prepare weave store to delegate copy details to the versioned file.
90
def read_weave(f):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
91
    # FIXME: detect the weave type and dispatch
6379.6.8 by Jelmer Vernooij
Fix two more relative imports.
92
    from bzrlib.weave import Weave
1209 by Martin Pool
- Add Weave._weave_name for debugging purposes
93
    w = Weave(getattr(f, 'name', None))
1563.2.14 by Robert Collins
Prepare weave store to delegate copy details to the versioned file.
94
    _read_weave_v5(f, w)
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
95
    return w
96
97
1563.2.14 by Robert Collins
Prepare weave store to delegate copy details to the versioned file.
98
def _read_weave_v5(f, w):
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
99
    """Private helper routine to read a weave format 5 file into memory.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
100
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
101
    This is only to be used by read_weave and WeaveFile.__init__.
102
    """
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
103
    #  200   0   2075.5080   1084.0360   bzrlib.weavefile:104(_read_weave_v5)
104
    # +60412 0    366.5900    366.5900   +<method 'readline' of 'file' objects>
105
    # +59982 0    320.5280    320.5280   +<method 'startswith' of 'str' objects>
106
    # +59363 0    297.8080    297.8080   +<method 'append' of 'list' objects>
107
    # replace readline call with iter over all lines ->
108
    # safe because we already suck on memory.
109
    #  200   0   1492.7170    802.6220   bzrlib.weavefile:104(_read_weave_v5)
110
    # +59982 0    329.9100    329.9100   +<method 'startswith' of 'str' objects>
111
    # +59363 0    320.2980    320.2980   +<method 'append' of 'list' objects>
112
    # replaced startswith with slice lookups:
113
    #  200   0    851.7250    501.1120   bzrlib.weavefile:104(_read_weave_v5)
114
    # +59363 0    311.8780    311.8780   +<method 'append' of 'list' objects>
115
    # +200   0     30.2500     30.2500   +<method 'readlines' of 'file' objects>
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
116
6379.6.8 by Jelmer Vernooij
Fix two more relative imports.
117
    from bzrlib.weave import WeaveFormatError
5582.10.41 by Jelmer Vernooij
revert weavefile changes
118
4708.2.1 by Martin
Ensure all files opened by bazaar proper are explicitly closed
119
    try:
6352.4.5 by Jelmer Vernooij
Fix weave test.
120
        lines = iter(f.readlines())
4708.2.1 by Martin
Ensure all files opened by bazaar proper are explicitly closed
121
    finally:
122
        f.close()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
123
2024.1.1 by John Arbash Meinel
When a weave file is empty, we should get WeaveFormatError, not StopIteration
124
    try:
125
        l = lines.next()
126
    except StopIteration:
127
        raise WeaveFormatError('invalid weave file: no header')
128
0.1.73 by Martin Pool
Clean up assertions for weavefile
129
    if l != FORMAT_1:
130
        raise WeaveFormatError('invalid weave file header: %r' % l)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
131
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
132
    ver = 0
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
133
    # read weave header.
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
134
    while True:
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
135
        l = lines.next()
0.1.90 by Martin Pool
Remove redundant 'v' lines from weave file
136
        if l[0] == 'i':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
137
            if len(l) > 2:
1079 by Martin Pool
- weavefile can just use lists for read-in ancestry, not frozensets
138
                w._parents.append(map(int, l[2:].split(' ')))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
139
            else:
1079 by Martin Pool
- weavefile can just use lists for read-in ancestry, not frozensets
140
                w._parents.append([])
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
141
            l = lines.next()[:-1]
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
142
            w._sha1s.append(l[2:])
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
143
            l = lines.next()
1083 by Martin Pool
- add space to store revision-id in weave files
144
            name = l[2:-1]
145
            w._names.append(name)
146
            w._name_map[name] = ver
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
147
            l = lines.next()
1083 by Martin Pool
- add space to store revision-id in weave files
148
            ver += 1
0.1.73 by Martin Pool
Clean up assertions for weavefile
149
        elif l == 'w\n':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
150
            break
151
        else:
0.1.73 by Martin Pool
Clean up assertions for weavefile
152
            raise WeaveFormatError('unexpected line %r' % l)
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
153
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
154
    # read weave body
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
155
    while True:
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
156
        l = lines.next()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
157
        if l == 'W\n':
158
            break
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
159
        elif '. ' == l[0:2]:
944 by Martin Pool
- refactor member names in Weave code
160
            w._weave.append(l[2:])  # include newline
1596.2.27 by Robert Collins
Note potential improvements in knit adds.
161
        elif ', ' == l[0:2]:
944 by Martin Pool
- refactor member names in Weave code
162
            w._weave.append(l[2:-1])        # exclude newline
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
163
        elif l == '}\n':
164
            w._weave.append(('}', None))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
165
        else:
944 by Martin Pool
- refactor member names in Weave code
166
            w._weave.append((intern(l[0]), int(l[2:])))
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
167
    return w