~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/serializer/v07.py

  • Committer: Wouter van Heyst
  • Date: 2006-06-07 16:05:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: larstiq@larstiq.dyndns.org-20060607160527-2b3649154d0e2e84
more code cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
# (C) 2005 Canonical Development Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import errors
23
 
from bzrlib.bundle.serializer import (BundleSerializer,
24
 
                                      BUNDLE_HEADER,
 
22
from bzrlib.bundle.serializer import (BundleSerializer, 
 
23
                                      BUNDLE_HEADER, 
 
24
                                      format_highres_date,
 
25
                                      unpack_highres_date,
25
26
                                     )
26
27
from bzrlib.bundle.serializer import binary_diff
27
 
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
 
28
from bzrlib.delta import compare_trees
28
29
from bzrlib.diff import internal_diff
 
30
import bzrlib.errors as errors
29
31
from bzrlib.osutils import pathjoin
30
32
from bzrlib.progress import DummyProgress
31
33
from bzrlib.revision import NULL_REVISION
 
34
from bzrlib.rio import RioWriter, read_stanzas
32
35
import bzrlib.ui
33
36
from bzrlib.testament import StrictTestament
34
 
from bzrlib.timestamp import (
35
 
    format_highres_date,
36
 
    unpack_highres_date,
37
 
)
38
37
from bzrlib.textfile import text_file
39
 
from bzrlib.trace import mutter
40
38
 
41
39
bool_text = {True: 'yes', False: 'no'}
42
40
 
55
53
        else:
56
54
            self.properties = properties
57
55
 
58
 
    def add_utf8_property(self, name, value):
59
 
        """Add a property whose value is currently utf8 to the action."""
60
 
        self.properties.append((name, value.decode('utf8')))
61
 
 
62
56
    def add_property(self, name, value):
63
57
        """Add a property to the action"""
64
58
        self.properties.append((name, value))
90
84
        to_file.write(text_line+'\n')
91
85
 
92
86
 
93
 
class BundleSerializerV08(BundleSerializer):
 
87
class BundleSerializerV07(BundleSerializer):
94
88
    def read(self, f):
95
89
        """Read the rest of the bundles from the supplied file.
96
90
 
97
91
        :param f: The file to read from
98
92
        :return: A list of bundles
99
93
        """
100
 
        return BundleReader(f).info
101
 
 
102
 
    def check_compatible(self):
103
 
        if self.source.supports_rich_root():
104
 
            raise errors.IncompatibleBundleFormat('0.8', repr(self.source))
 
94
        assert self.version == '0.7'
 
95
        # The first line of the header should have been read
 
96
        raise NotImplementedError
105
97
 
106
98
    def write(self, source, revision_ids, forced_bases, f):
107
99
        """Write the bundless to the supplied files.
115
107
        self.revision_ids = revision_ids
116
108
        self.forced_bases = forced_bases
117
109
        self.to_file = f
118
 
        self.check_compatible()
119
110
        source.lock_read()
120
111
        try:
121
112
            self._write_main_header()
132
123
        """Write the header for the changes"""
133
124
        f = self.to_file
134
125
        f.write(BUNDLE_HEADER)
135
 
        f.write('0.8\n')
 
126
        f.write('0.7\n')
136
127
        f.write('#\n')
137
128
 
138
 
    def _write(self, key, value, indent=1, trailing_space_when_empty=False):
139
 
        """Write out meta information, with proper indenting, etc.
140
 
 
141
 
        :param trailing_space_when_empty: To work around a bug in earlier
142
 
            bundle readers, when writing an empty property, we use "prop: \n"
143
 
            rather than writing "prop:\n".
144
 
            If this parameter is True, and value is the empty string, we will
145
 
            write an extra space.
146
 
        """
 
129
    def _write(self, key, value, indent=1):
 
130
        """Write out meta information, with proper indenting, etc"""
147
131
        assert indent > 0, 'indentation must be greater than 0'
148
132
        f = self.to_file
149
133
        f.write('#' + (' ' * indent))
150
134
        f.write(key.encode('utf-8'))
151
135
        if not value:
152
 
            if trailing_space_when_empty and value == '':
153
 
                f.write(': \n')
154
 
            else:
155
 
                f.write(':\n')
156
 
        elif isinstance(value, str):
157
 
            f.write(': ')
158
 
            f.write(value)
159
 
            f.write('\n')
160
 
        elif isinstance(value, unicode):
 
136
            f.write(':\n')
 
137
        elif isinstance(value, basestring):
161
138
            f.write(': ')
162
139
            f.write(value.encode('utf-8'))
163
140
            f.write('\n')
165
142
            f.write(':\n')
166
143
            for entry in value:
167
144
                f.write('#' + (' ' * (indent+2)))
168
 
                if isinstance(entry, str):
169
 
                    f.write(entry)
170
 
                else:
171
 
                    f.write(entry.encode('utf-8'))
 
145
                f.write(entry.encode('utf-8'))
172
146
                f.write('\n')
173
147
 
174
148
    def _write_revisions(self, pb):
178
152
        last_rev_id = None
179
153
        last_rev_tree = None
180
154
 
181
 
        i_max = len(self.revision_ids)
 
155
        i_max = len(self.revision_ids) 
182
156
        for i, rev_id in enumerate(self.revision_ids):
183
157
            pb.update("Generating revsion data", i, i_max)
184
158
            rev = self.source.get_revision(rev_id)
185
159
            if rev_id == last_rev_id:
186
160
                rev_tree = last_rev_tree
187
161
            else:
188
 
                rev_tree = self.source.revision_tree(rev_id)
 
162
                base_tree = self.source.revision_tree(rev_id)
 
163
            rev_tree = self.source.revision_tree(rev_id)
189
164
            if rev_id in self.forced_bases:
190
165
                explicit_base = True
191
166
                base_id = self.forced_bases[rev_id]
202
177
                base_tree = last_rev_tree
203
178
            else:
204
179
                base_tree = self.source.revision_tree(base_id)
205
 
            force_binary = (i != 0)
 
180
 
206
181
            self._write_revision(rev, rev_tree, base_id, base_tree, 
207
 
                                 explicit_base, force_binary)
 
182
                                 explicit_base)
208
183
 
209
184
            last_rev_id = base_id
210
185
            last_rev_tree = base_tree
211
186
 
212
 
    def _testament_sha1(self, revision_id):
213
 
        return StrictTestament.from_revision(self.source, 
214
 
                                             revision_id).as_sha1()
215
 
 
216
187
    def _write_revision(self, rev, rev_tree, base_rev, base_tree, 
217
 
                        explicit_base, force_binary):
 
188
                        explicit_base):
218
189
        """Write out the information for a revision."""
219
190
        def w(key, value):
220
191
            self._write(key, value, indent=1)
224
195
        w('date', format_highres_date(rev.timestamp, rev.timezone))
225
196
        self.to_file.write('\n')
226
197
 
227
 
        self._write_delta(rev_tree, base_tree, rev.revision_id, force_binary)
 
198
        self._write_delta(rev_tree, base_tree, rev.revision_id)
228
199
 
229
200
        w('revision id', rev.revision_id)
230
 
        w('sha1', self._testament_sha1(rev.revision_id))
 
201
        w('sha1', StrictTestament.from_revision(self.source, 
 
202
                                                rev.revision_id).as_sha1())
231
203
        w('inventory sha1', rev.inventory_sha1)
232
204
        if rev.parent_ids:
233
205
            w('parent ids', rev.parent_ids)
235
207
            w('base id', base_rev)
236
208
        if rev.properties:
237
209
            self._write('properties', None, indent=1)
238
 
            for name, value in sorted(rev.properties.items()):
239
 
                self._write(name, value, indent=3,
240
 
                            trailing_space_when_empty=True)
 
210
            for name, value in rev.properties.items():
 
211
                self._write(name, value, indent=3)
241
212
        
242
213
        # Add an extra blank space at the end
243
214
        self.to_file.write('\n')
251
222
        self.to_file.write(' // '.join(p_texts).encode('utf-8'))
252
223
        self.to_file.write('\n')
253
224
 
254
 
    def _write_delta(self, new_tree, old_tree, default_revision_id, 
255
 
                     force_binary):
 
225
    def _write_delta(self, new_tree, old_tree, default_revision_id):
256
226
        """Write out the changes between the trees."""
257
227
        DEVNULL = '/dev/null'
258
228
        old_label = ''
259
229
        new_label = ''
260
230
 
261
 
        def do_diff(file_id, old_path, new_path, action, force_binary):
 
231
        def do_diff(file_id, old_path, new_path, action):
262
232
            def tree_lines(tree, require_text=False):
263
233
                if file_id in tree:
264
234
                    tree_file = tree.get_file(file_id)
269
239
                    return []
270
240
 
271
241
            try:
272
 
                if force_binary:
273
 
                    raise errors.BinaryFile()
274
242
                old_lines = tree_lines(old_tree, require_text=True)
275
243
                new_lines = tree_lines(new_tree, require_text=True)
276
244
                action.write(self.to_file)
288
256
                          old_path, new_path):
289
257
            entry = new_tree.inventory[file_id]
290
258
            if entry.revision != default_revision_id:
291
 
                action.add_utf8_property('last-changed', entry.revision)
 
259
                action.add_property('last-changed', entry.revision)
292
260
            if meta_modified:
293
261
                action.add_bool_property('executable', entry.executable)
294
262
            if text_modified and kind == "symlink":
295
263
                action.add_property('target', entry.symlink_target)
296
264
            if text_modified and kind == "file":
297
 
                do_diff(file_id, old_path, new_path, action, force_binary)
 
265
                do_diff(file_id, old_path, new_path, action)
298
266
            else:
299
267
                action.write(self.to_file)
300
268
 
301
 
        delta = new_tree.changes_from(old_tree, want_unchanged=True,
302
 
                                      include_root=True)
 
269
        delta = compare_trees(old_tree, new_tree, want_unchanged=True)
303
270
        for path, file_id, kind in delta.removed:
304
271
            action = Action('removed', [kind, path]).write(self.to_file)
305
272
 
331
298
            if new_rev != old_rev:
332
299
                action = Action('modified', [ie.kind, 
333
300
                                             new_tree.id2path(ie.file_id)])
334
 
                action.add_utf8_property('last-changed', ie.revision)
 
301
                action.add_property('last-changed', ie.revision)
335
302
                action.write(self.to_file)
336
 
 
337
 
 
338
 
class BundleReader(object):
339
 
    """This class reads in a bundle from a file, and returns
340
 
    a Bundle object, which can then be applied against a tree.
341
 
    """
342
 
    def __init__(self, from_file):
343
 
        """Read in the bundle from the file.
344
 
 
345
 
        :param from_file: A file-like object (must have iterator support).
346
 
        """
347
 
        object.__init__(self)
348
 
        self.from_file = iter(from_file)
349
 
        self._next_line = None
350
 
        
351
 
        self.info = self._get_info()
352
 
        # We put the actual inventory ids in the footer, so that the patch
353
 
        # is easier to read for humans.
354
 
        # Unfortunately, that means we need to read everything before we
355
 
        # can create a proper bundle.
356
 
        self._read()
357
 
        self._validate()
358
 
 
359
 
    def _get_info(self):
360
 
        return BundleInfo08()
361
 
 
362
 
    def _read(self):
363
 
        self._next().next()
364
 
        while self._next_line is not None:
365
 
            if not self._read_revision_header():
366
 
                break
367
 
            if self._next_line is None:
368
 
                break
369
 
            self._read_patches()
370
 
            self._read_footer()
371
 
 
372
 
    def _validate(self):
373
 
        """Make sure that the information read in makes sense
374
 
        and passes appropriate checksums.
375
 
        """
376
 
        # Fill in all the missing blanks for the revisions
377
 
        # and generate the real_revisions list.
378
 
        self.info.complete_info()
379
 
 
380
 
    def _next(self):
381
 
        """yield the next line, but secretly
382
 
        keep 1 extra line for peeking.
383
 
        """
384
 
        for line in self.from_file:
385
 
            last = self._next_line
386
 
            self._next_line = line
387
 
            if last is not None:
388
 
                #mutter('yielding line: %r' % last)
389
 
                yield last
390
 
        last = self._next_line
391
 
        self._next_line = None
392
 
        #mutter('yielding line: %r' % last)
393
 
        yield last
394
 
 
395
 
    def _read_revision_header(self):
396
 
        found_something = False
397
 
        self.info.revisions.append(RevisionInfo(None))
398
 
        for line in self._next():
399
 
            # The bzr header is terminated with a blank line
400
 
            # which does not start with '#'
401
 
            if line is None or line == '\n':
402
 
                break
403
 
            if not line.startswith('#'):
404
 
                continue
405
 
            found_something = True
406
 
            self._handle_next(line)
407
 
        if not found_something:
408
 
            # Nothing was there, so remove the added revision
409
 
            self.info.revisions.pop()
410
 
        return found_something
411
 
 
412
 
    def _read_next_entry(self, line, indent=1):
413
 
        """Read in a key-value pair
414
 
        """
415
 
        if not line.startswith('#'):
416
 
            raise errors.MalformedHeader('Bzr header did not start with #')
417
 
        line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
418
 
        if line[:indent] == ' '*indent:
419
 
            line = line[indent:]
420
 
        if not line:
421
 
            return None, None# Ignore blank lines
422
 
 
423
 
        loc = line.find(': ')
424
 
        if loc != -1:
425
 
            key = line[:loc]
426
 
            value = line[loc+2:]
427
 
            if not value:
428
 
                value = self._read_many(indent=indent+2)
429
 
        elif line[-1:] == ':':
430
 
            key = line[:-1]
431
 
            value = self._read_many(indent=indent+2)
432
 
        else:
433
 
            raise errors.MalformedHeader('While looking for key: value pairs,'
434
 
                    ' did not find the colon %r' % (line))
435
 
 
436
 
        key = key.replace(' ', '_')
437
 
        #mutter('found %s: %s' % (key, value))
438
 
        return key, value
439
 
 
440
 
    def _handle_next(self, line):
441
 
        if line is None:
442
 
            return
443
 
        key, value = self._read_next_entry(line, indent=1)
444
 
        mutter('_handle_next %r => %r' % (key, value))
445
 
        if key is None:
446
 
            return
447
 
 
448
 
        revision_info = self.info.revisions[-1]
449
 
        if key in revision_info.__dict__:
450
 
            if getattr(revision_info, key) is None:
451
 
                if key in ('file_id', 'revision_id', 'base_id'):
452
 
                    value = value.encode('utf8')
453
 
                elif key in ('parent_ids'):
454
 
                    value = [v.encode('utf8') for v in value]
455
 
                setattr(revision_info, key, value)
456
 
            else:
457
 
                raise errors.MalformedHeader('Duplicated Key: %s' % key)
458
 
        else:
459
 
            # What do we do with a key we don't recognize
460
 
            raise errors.MalformedHeader('Unknown Key: "%s"' % key)
461
 
    
462
 
    def _read_many(self, indent):
463
 
        """If a line ends with no entry, that means that it should be
464
 
        followed with multiple lines of values.
465
 
 
466
 
        This detects the end of the list, because it will be a line that
467
 
        does not start properly indented.
468
 
        """
469
 
        values = []
470
 
        start = '#' + (' '*indent)
471
 
 
472
 
        if self._next_line is None or self._next_line[:len(start)] != start:
473
 
            return values
474
 
 
475
 
        for line in self._next():
476
 
            values.append(line[len(start):-1].decode('utf-8'))
477
 
            if self._next_line is None or self._next_line[:len(start)] != start:
478
 
                break
479
 
        return values
480
 
 
481
 
    def _read_one_patch(self):
482
 
        """Read in one patch, return the complete patch, along with
483
 
        the next line.
484
 
 
485
 
        :return: action, lines, do_continue
486
 
        """
487
 
        #mutter('_read_one_patch: %r' % self._next_line)
488
 
        # Peek and see if there are no patches
489
 
        if self._next_line is None or self._next_line.startswith('#'):
490
 
            return None, [], False
491
 
 
492
 
        first = True
493
 
        lines = []
494
 
        for line in self._next():
495
 
            if first:
496
 
                if not line.startswith('==='):
497
 
                    raise errors.MalformedPatches('The first line of all patches'
498
 
                        ' should be a bzr meta line "==="'
499
 
                        ': %r' % line)
500
 
                action = line[4:-1].decode('utf-8')
501
 
            elif line.startswith('... '):
502
 
                action += line[len('... '):-1].decode('utf-8')
503
 
 
504
 
            if (self._next_line is not None and 
505
 
                self._next_line.startswith('===')):
506
 
                return action, lines, True
507
 
            elif self._next_line is None or self._next_line.startswith('#'):
508
 
                return action, lines, False
509
 
 
510
 
            if first:
511
 
                first = False
512
 
            elif not line.startswith('... '):
513
 
                lines.append(line)
514
 
 
515
 
        return action, lines, False
516
 
            
517
 
    def _read_patches(self):
518
 
        do_continue = True
519
 
        revision_actions = []
520
 
        while do_continue:
521
 
            action, lines, do_continue = self._read_one_patch()
522
 
            if action is not None:
523
 
                revision_actions.append((action, lines))
524
 
        assert self.info.revisions[-1].tree_actions is None
525
 
        self.info.revisions[-1].tree_actions = revision_actions
526
 
 
527
 
    def _read_footer(self):
528
 
        """Read the rest of the meta information.
529
 
 
530
 
        :param first_line:  The previous step iterates past what it
531
 
                            can handle. That extra line is given here.
532
 
        """
533
 
        for line in self._next():
534
 
            self._handle_next(line)
535
 
            if self._next_line is None:
536
 
                break
537
 
            if not self._next_line.startswith('#'):
538
 
                # Consume the trailing \n and stop processing
539
 
                self._next().next()
540
 
                break
541
 
 
542
 
class BundleInfo08(BundleInfo):
543
 
 
544
 
    def _update_tree(self, bundle_tree, revision_id):
545
 
        bundle_tree.note_last_changed('', revision_id)
546
 
        BundleInfo._update_tree(self, bundle_tree, revision_id)
547
 
 
548
 
    def _testament_sha1_from_revision(self, repository, revision_id):
549
 
        testament = StrictTestament.from_revision(repository, revision_id)
550
 
        return testament.as_sha1()
551
 
 
552
 
    def _testament_sha1(self, revision, inventory):
553
 
        return StrictTestament(revision, inventory).as_sha1()