0.1.1
by Martin Pool
Check in old existing knit code. |
1 |
#! /usr/bin/python
|
2 |
||
3 |
# Copyright (C) 2005 Canonical Ltd
|
|
4 |
||
0.1.33
by Martin Pool
add gpl text |
5 |
# This program is free software; you can redistribute it and/or modify
|
6 |
# it under the terms of the GNU General Public License as published by
|
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
|
8 |
# (at your option) any later version.
|
|
9 |
||
10 |
# This program is distributed in the hope that it will be useful,
|
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
# GNU General Public License for more details.
|
|
14 |
||
15 |
# You should have received a copy of the GNU General Public License
|
|
16 |
# along with this program; if not, write to the Free Software
|
|
17 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
0.1.1
by Martin Pool
Check in old existing knit code. |
18 |
|
19 |
# Author: Martin Pool <mbp@canonical.com>
|
|
20 |
||
21 |
||
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
22 |
"""Weave - storage of related text file versions"""
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
23 |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
24 |
# TODO: Perhaps have copy and comparison methods of Weave instances?
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
25 |
|
0.1.34
by Martin Pool
remove dead code |
26 |
|
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
27 |
class VerInfo(object): |
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
28 |
"""Information about a version in a Weave."""
|
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
29 |
included = frozenset() |
30 |
def __init__(self, included=None): |
|
31 |
if included: |
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
32 |
self.included = frozenset(included) |
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
33 |
|
0.1.18
by Martin Pool
Better Knit.dump method |
34 |
def __repr__(self): |
35 |
s = self.__class__.__name__ + '(' |
|
36 |
if self.included: |
|
37 |
s += 'included=%r' % (list(self.included)) |
|
38 |
s += ')' |
|
39 |
return s |
|
40 |
||
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
41 |
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
42 |
class Weave(object): |
43 |
"""weave - versioned text file storage.
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
44 |
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
45 |
A Weave manages versions of line-based text files, keeping track of the
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
46 |
originating version for each line.
|
47 |
||
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
48 |
Texts can be identified in either of two ways:
|
49 |
||
50 |
* a nonnegative index number.
|
|
51 |
||
52 |
* a version-id string.
|
|
53 |
||
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
54 |
Typically the index number will be valid only inside this weave and
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
55 |
the version-id is used to reference it in the larger world.
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
56 |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
57 |
The weave is represented as a list mixing edit instructions and
|
58 |
literal text. Each entry in _l can be either a string (or
|
|
59 |
unicode), or a tuple. If a string, it means that the given line
|
|
60 |
should be output in the currently active revisions.
|
|
61 |
||
62 |
If a tuple, it gives a processing instruction saying in which
|
|
63 |
revisions the enclosed lines are active. The tuple has the form
|
|
64 |
(instruction, version).
|
|
65 |
||
66 |
The instruction can be '{' or '}' for an insertion block, and '['
|
|
67 |
and ']' for a deletion block respectively. The version is the
|
|
0.1.45
by Martin Pool
doc |
68 |
integer version index. There is no replace operator, only deletes
|
69 |
and inserts.
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
70 |
|
0.1.41
by Martin Pool
Doc |
71 |
Constraints/notes:
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
72 |
|
73 |
* A later version can delete lines that were introduced by any
|
|
74 |
number of ancestor versions; this implies that deletion
|
|
75 |
instructions can span insertion blocks without regard to the
|
|
76 |
insertion block's nesting.
|
|
77 |
||
0.1.41
by Martin Pool
Doc |
78 |
* Similarly, deletions need not be properly nested with regard to
|
79 |
each other, because they might have been generated by
|
|
80 |
independent revisions.
|
|
81 |
||
0.1.45
by Martin Pool
doc |
82 |
* Insertions are always made by inserting a new bracketed block
|
83 |
into a single point in the previous weave. This implies they
|
|
84 |
can nest but not overlap, and the nesting must always have later
|
|
85 |
insertions on the inside.
|
|
86 |
||
0.1.41
by Martin Pool
Doc |
87 |
* It doesn't seem very useful to have an active insertion
|
88 |
inside an inactive insertion, but it might happen.
|
|
0.1.45
by Martin Pool
doc |
89 |
|
0.1.41
by Martin Pool
Doc |
90 |
* Therefore, all instructions are always"considered"; that
|
91 |
is passed onto and off the stack. An outer inactive block
|
|
92 |
doesn't disable an inner block.
|
|
93 |
||
94 |
* Lines are enabled if the most recent enclosing insertion is
|
|
95 |
active and none of the enclosing deletions are active.
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
96 |
|
97 |
||
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
98 |
_l
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
99 |
Text of the weave.
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
100 |
|
101 |
_v
|
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
102 |
List of versions, indexed by index number.
|
103 |
||
104 |
For each version we store the tuple (included_versions), which
|
|
105 |
lists the previous versions also considered active.
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
106 |
"""
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
107 |
def __init__(self): |
108 |
self._l = [] |
|
109 |
self._v = [] |
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
110 |
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
111 |
|
0.1.26
by Martin Pool
Refactor parameters to add command |
112 |
def add(self, parents, text): |
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
113 |
"""Add a single text on top of the weave.
|
0.1.36
by Martin Pool
doc |
114 |
|
0.1.26
by Martin Pool
Refactor parameters to add command |
115 |
Returns the index number of the newly added version.
|
116 |
||
117 |
parents
|
|
118 |
List or set of parent version numbers.
|
|
119 |
||
120 |
text
|
|
121 |
Sequence of lines to be added in the new version."""
|
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
122 |
self._check_versions(parents) |
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
123 |
self._check_lines(text) |
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
124 |
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
125 |
idx = len(self._v) |
0.1.5
by Martin Pool
Add test for storing two text versions. |
126 |
|
0.1.26
by Martin Pool
Refactor parameters to add command |
127 |
if parents: |
128 |
parents = frozenset(parents) |
|
129 |
delta = self._delta(parents, text) |
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
130 |
|
0.1.31
by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go. |
131 |
# offset gives the number of lines that have been inserted
|
132 |
# into the weave up to the current point; if the original edit instruction
|
|
133 |
# says to change line A then we actually change (A+offset)
|
|
134 |
offset = 0 |
|
135 |
||
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
136 |
for i1, i2, newlines in delta: |
0.1.29
by Martin Pool
Better internal error |
137 |
assert 0 <= i1 |
138 |
assert i1 <= i2 |
|
139 |
assert i2 <= len(self._l) |
|
140 |
||
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
141 |
if i1 != i2: |
0.1.29
by Martin Pool
Better internal error |
142 |
raise NotImplementedError("can't handle replacing weave [%d:%d] yet" |
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
143 |
% (i1, i2)) |
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
144 |
|
145 |
self._l.insert(i1 + offset, ('{', idx)) |
|
146 |
i = i1 + offset + 1 |
|
147 |
self._l[i:i] = newlines |
|
148 |
self._l.insert(i + 1, ('}', idx)) |
|
149 |
offset += 2 + len(newlines) |
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
150 |
|
0.1.26
by Martin Pool
Refactor parameters to add command |
151 |
self._v.append(VerInfo(parents)) |
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
152 |
else: |
0.1.26
by Martin Pool
Refactor parameters to add command |
153 |
# special case; adding with no parents revision; can do this
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
154 |
# more quickly by just appending unconditionally
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
155 |
self._l.append(('{', idx)) |
156 |
self._l += text |
|
157 |
self._l.append(('}', idx)) |
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
158 |
|
159 |
self._v.append(VerInfo()) |
|
160 |
||
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
161 |
return idx |
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
162 |
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
163 |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
164 |
def _check_lines(self, text): |
165 |
if not isinstance(text, list): |
|
166 |
raise ValueError("text should be a list, not %s" % type(text)) |
|
167 |
||
168 |
for l in text: |
|
169 |
if not isinstance(l, basestring): |
|
170 |
raise ValueError("text line should be a string or unicode, not %s" % type(l)) |
|
171 |
||
172 |
||
173 |
||
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
174 |
def _check_versions(self, indexes): |
175 |
"""Check everything in the sequence of indexes is valid"""
|
|
176 |
for i in indexes: |
|
177 |
try: |
|
178 |
self._v[i] |
|
179 |
except IndexError: |
|
180 |
raise IndexError("invalid version number %r" % i) |
|
181 |
||
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
182 |
|
0.1.7
by Martin Pool
Add trivial annotate text |
183 |
def annotate(self, index): |
184 |
return list(self.annotate_iter(index)) |
|
185 |
||
186 |
||
187 |
def annotate_iter(self, index): |
|
188 |
"""Yield list of (index-id, line) pairs for the specified version.
|
|
189 |
||
190 |
The index indicates when the line originated in the weave."""
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
191 |
try: |
192 |
vi = self._v[index] |
|
193 |
except IndexError: |
|
194 |
raise IndexError('version index %d out of range' % index) |
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
195 |
included = set(vi.included) |
196 |
included.add(index) |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
197 |
for origin, lineno, text in self._extract(included): |
198 |
yield origin, text |
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
199 |
|
200 |
||
201 |
def _extract(self, included): |
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
202 |
"""Yield annotation of lines in included set.
|
203 |
||
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
204 |
Yields a sequence of tuples (origin, lineno, text), where
|
205 |
origin is the origin version, lineno the index in the weave,
|
|
206 |
and text the text of the line.
|
|
207 |
||
0.1.20
by Martin Pool
Factor out Knit.extract() method |
208 |
The set typically but not necessarily corresponds to a version.
|
209 |
"""
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
210 |
stack = [] |
211 |
isactive = False |
|
212 |
lineno = 0 |
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
213 |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
214 |
for l in self._l: |
215 |
if isinstance(l, tuple): |
|
216 |
c, v = l |
|
217 |
if c == '{': |
|
218 |
stack.append(l) |
|
219 |
isactive = (v in included) |
|
220 |
elif c == '}': |
|
221 |
oldc, oldv = stack.pop() |
|
222 |
assert oldc == '{' |
|
223 |
assert oldv == v |
|
0.1.40
by Martin Pool
Add test for extracting from weave with nested insertions |
224 |
isactive = stack and (stack[-1][1] in included) |
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
225 |
else: |
0.1.43
by Martin Pool
More assertions on weave extraction |
226 |
raise ValueError("invalid processing instruction %r on line %d" |
227 |
% (l, lineno)) |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
228 |
else: |
229 |
assert isinstance(l, basestring) |
|
0.1.43
by Martin Pool
More assertions on weave extraction |
230 |
if not stack: |
231 |
raise ValueError("literal at top level on line %d" |
|
232 |
% lineno) |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
233 |
if isactive: |
234 |
origin = stack[-1][1] |
|
235 |
yield origin, lineno, l |
|
236 |
lineno += 1 |
|
0.1.7
by Martin Pool
Add trivial annotate text |
237 |
|
0.1.40
by Martin Pool
Add test for extracting from weave with nested insertions |
238 |
if stack: |
239 |
raise ValueError("unclosed blocks at end of weave", |
|
240 |
stack) |
|
241 |
||
0.1.7
by Martin Pool
Add trivial annotate text |
242 |
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
243 |
def getiter(self, index): |
244 |
"""Yield lines for the specified version."""
|
|
0.1.8
by Martin Pool
Unify get/annotate code |
245 |
for origin, line in self.annotate_iter(index): |
246 |
yield line |
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
247 |
|
248 |
||
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
249 |
def get(self, index): |
0.1.5
by Martin Pool
Add test for storing two text versions. |
250 |
return list(self.getiter(index)) |
0.1.1
by Martin Pool
Check in old existing knit code. |
251 |
|
252 |
||
0.1.11
by Martin Pool
Add Knit.dump method |
253 |
def dump(self, to_file): |
254 |
from pprint import pprint |
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
255 |
print >>to_file, "Weave._l = ", |
0.1.11
by Martin Pool
Add Knit.dump method |
256 |
pprint(self._l, to_file) |
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
257 |
print >>to_file, "Weave._v = ", |
0.1.18
by Martin Pool
Better Knit.dump method |
258 |
pprint(self._v, to_file) |
0.1.11
by Martin Pool
Add Knit.dump method |
259 |
|
260 |
||
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
261 |
def check(self): |
262 |
for vers_info in self._v: |
|
263 |
included = set() |
|
264 |
for vi in vers_info[0]: |
|
265 |
if vi < 0 or vi >= index: |
|
0.1.16
by Martin Pool
formatting |
266 |
raise ValueError("invalid included version %d for index %d" |
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
267 |
% (vi, index)) |
268 |
if vi in included: |
|
0.1.16
by Martin Pool
formatting |
269 |
raise ValueError("repeated included version %d for index %d" |
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
270 |
% (vi, index)) |
271 |
included.add(vi) |
|
0.1.18
by Martin Pool
Better Knit.dump method |
272 |
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
273 |
|
274 |
||
0.1.21
by Martin Pool
Start computing a delta to insert a new revision |
275 |
def _delta(self, included, lines): |
276 |
"""Return changes from basis to new revision.
|
|
277 |
||
278 |
The old text for comparison is the union of included revisions.
|
|
279 |
||
280 |
This is used in inserting a new text.
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
281 |
|
282 |
Delta is returned as a sequence of (line1, line2, newlines),
|
|
283 |
indicating that line1 through line2 of the old weave should be
|
|
284 |
replaced by the sequence of lines in newlines. Note that
|
|
285 |
these line numbers are positions in the total weave and don't
|
|
286 |
correspond to the lines in any extracted version, or even the
|
|
287 |
extracted union of included versions.
|
|
288 |
||
289 |
If line1=line2, this is a pure insert; if newlines=[] this is a
|
|
290 |
pure delete. (Similar to difflib.)
|
|
0.1.21
by Martin Pool
Start computing a delta to insert a new revision |
291 |
"""
|
292 |
||
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
293 |
self._check_versions(included) |
294 |
||
0.1.23
by Martin Pool
tidy up |
295 |
##from pprint import pprint
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
296 |
|
297 |
# first get basis for comparison
|
|
298 |
# basis holds (lineno, origin, line)
|
|
299 |
basis = [] |
|
300 |
||
0.1.23
by Martin Pool
tidy up |
301 |
##print 'my lines:'
|
302 |
##pprint(self._l)
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
303 |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
304 |
basis = list(self._extract(included)) |
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
305 |
|
306 |
# now make a parallel list with only the text, to pass to the differ
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
307 |
basis_lines = [line for (origin, lineno, line) in basis] |
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
308 |
|
309 |
# add a sentinal, because we can also match against the final line
|
|
310 |
basis.append((len(self._l), None)) |
|
311 |
||
312 |
# XXX: which line of the weave should we really consider matches the end of the file?
|
|
313 |
# the current code says it's the last line of the weave?
|
|
314 |
||
315 |
from difflib import SequenceMatcher |
|
316 |
s = SequenceMatcher(None, basis_lines, lines) |
|
317 |
||
0.1.23
by Martin Pool
tidy up |
318 |
##print 'basis sequence:'
|
319 |
##pprint(basis)
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
320 |
|
321 |
for tag, i1, i2, j1, j2 in s.get_opcodes(): |
|
0.1.23
by Martin Pool
tidy up |
322 |
##print tag, i1, i2, j1, j2
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
323 |
|
324 |
if tag == 'equal': |
|
325 |
continue
|
|
326 |
||
327 |
# i1,i2 are given in offsets within basis_lines; we need to map them
|
|
328 |
# back to offsets within the entire weave
|
|
329 |
real_i1 = basis[i1][0] |
|
330 |
real_i2 = basis[i2][0] |
|
331 |
||
0.1.35
by Martin Pool
Clean up Knit._delta method |
332 |
assert 0 <= j1 |
333 |
assert j1 <= j2 |
|
334 |
assert j2 <= len(lines) |
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
335 |
|
0.1.35
by Martin Pool
Clean up Knit._delta method |
336 |
yield real_i1, real_i2, lines[j1:j2] |
0.1.21
by Martin Pool
Start computing a delta to insert a new revision |
337 |
|
0.1.1
by Martin Pool
Check in old existing knit code. |
338 |