~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/bundle_data.py

  • Committer: Martin Pool
  • Date: 2008-04-24 07:22:53 UTC
  • mto: This revision was merged to the branch mainline in revision 3415.
  • Revision ID: mbp@sourcefrog.net-20080424072253-opmjij7xfy38w27f
Remove every assert statement from bzrlib!

Depending on the context they are:

 * turned into an explicit if/raise of either AssertionError 
   or something more specific -- particularly where they protect
   programming interfaces, complex invariants, or data file integrity
 * removed, if they're redundant with a later check, not protecting
   a meaningful invariant
 * turned into a selftest method on tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
            for property in self.properties:
79
79
                key_end = property.find(': ')
80
80
                if key_end == -1:
81
 
                    assert property.endswith(':')
 
81
                    if not property.endswith(':'):
 
82
                        raise ValueError(property)
82
83
                    key = str(property[:-1])
83
84
                    value = ''
84
85
                else:
196
197
    def revision_tree(self, repository, revision_id, base=None):
197
198
        revision = self.get_revision(revision_id)
198
199
        base = self.get_base(revision)
199
 
        assert base != revision_id
 
200
        if base == revision_id:
 
201
            raise AssertionError()
200
202
        if not self._validated_revisions_against_repo:
201
203
            self._validate_references_from_repository(repository)
202
204
        revision_info = self.get_revision_info(revision_id)
286
288
        """At this point we should have generated the BundleTree,
287
289
        so build up an inventory, and make sure the hashes match.
288
290
        """
289
 
 
290
 
        assert inv is not None
291
 
 
292
291
        # Now we should have a complete inventory entry.
293
292
        s = serializer_v5.write_inventory_to_string(inv)
294
293
        sha1 = sha_string(s)
295
294
        # Target revision is the last entry in the real_revisions list
296
295
        rev = self.get_revision(revision_id)
297
 
        assert rev.revision_id == revision_id
 
296
        if rev.revision_id != revision_id:
 
297
            raise AssertionError()
298
298
        if sha1 != rev.inventory_sha1:
299
299
            open(',,bogus-inv', 'wb').write(s)
300
300
            warning('Inventory sha hash mismatch for revision %s. %s'
308
308
        
309
309
        rev = self.get_revision(revision_id)
310
310
        rev_info = self.get_revision_info(revision_id)
311
 
        assert rev.revision_id == rev_info.revision_id
312
 
        assert rev.revision_id == revision_id
 
311
        if not (rev.revision_id == rev_info.revision_id):
 
312
            raise AssertionError()
 
313
        if not (rev.revision_id == revision_id):
 
314
            raise AssertionError()
313
315
        sha1 = self._testament_sha1(rev, inventory)
314
316
        if sha1 != rev_info.sha1:
315
317
            raise TestamentMismatch(rev.revision_id, rev_info.sha1, sha1)
347
349
                if name == 'last-changed':
348
350
                    last_changed = value
349
351
                elif name == 'executable':
350
 
                    assert value in ('yes', 'no'), value
351
352
                    val = (value == 'yes')
352
353
                    bundle_tree.note_executable(new_path, val)
353
354
                elif name == 'target':
357
358
            return last_changed, encoding
358
359
 
359
360
        def do_patch(path, lines, encoding):
360
 
            if encoding is not None:
361
 
                assert encoding == 'base64'
 
361
            if encoding == 'base64':
362
362
                patch = base64.decodestring(''.join(lines))
363
 
            else:
 
363
            elif encoding is None:
364
364
                patch =  ''.join(lines)
 
365
            else:
 
366
                raise ValueError(encoding)
365
367
            bundle_tree.note_patch(path, patch)
366
368
 
367
369
        def renamed(kind, extra, lines):
495
497
 
496
498
    def note_rename(self, old_path, new_path):
497
499
        """A file/directory has been renamed from old_path => new_path"""
498
 
        assert new_path not in self._renamed
499
 
        assert old_path not in self._renamed_r
 
500
        if new_path in self._renamed:
 
501
            raise AssertionError(new_path)
 
502
        if old_path in self._renamed_r:
 
503
            raise AssertionError(old_path)
500
504
        self._renamed[new_path] = old_path
501
505
        self._renamed_r[old_path] = new_path
502
506
 
532
536
 
533
537
    def old_path(self, new_path):
534
538
        """Get the old_path (path in the base_tree) for the file at new_path"""
535
 
        assert new_path[:1] not in ('\\', '/')
 
539
        if new_path[:1] in ('\\', '/'):
 
540
            raise ValueError(new_path)
536
541
        old_path = self._renamed.get(new_path)
537
542
        if old_path is not None:
538
543
            return old_path
558
563
        """Get the new_path (path in the target_tree) for the file at old_path
559
564
        in the base tree.
560
565
        """
561
 
        assert old_path[:1] not in ('\\', '/')
 
566
        if old_path[:1] in ('\\', '/'):
 
567
            raise ValueError(old_path)
562
568
        new_path = self._renamed_r.get(old_path)
563
569
        if new_path is not None:
564
570
            return new_path
637
643
            if (patch_original is None and 
638
644
                self.get_kind(file_id) == 'directory'):
639
645
                return StringIO()
640
 
            assert patch_original is not None, "None: %s" % file_id
 
646
            if patch_original is None:
 
647
                raise AssertionError("None: %s" % file_id)
641
648
            return patch_original
642
649
 
643
 
        assert not file_patch.startswith('\\'), \
644
 
            'Malformed patch for %s, %r' % (file_id, file_patch)
 
650
        if file_patch.startswith('\\'):
 
651
            raise ValueError(
 
652
                'Malformed patch for %s, %r' % (file_id, file_patch))
645
653
        return patched_file(file_patch, patch_original)
646
654
 
647
655
    def get_symlink_target(self, file_id):
694
702
        This need to be called before ever accessing self.inventory
695
703
        """
696
704
        from os.path import dirname, basename
697
 
 
698
 
        assert self.base_tree is not None
699
705
        base_inv = self.base_tree.inventory
700
706
        inv = Inventory(None, self.revision_id)
701
707