/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 read_changeset.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-06-30 22:54:21 UTC
  • mto: (0.5.85) (1185.82.1 bzr-w-changeset)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: aaron.bentley@utoronto.ca-20050630225421-a6a4f5ae500dcddf
Added non-working ChangesetTree

Show diffs side-by-side

added added

removed removed

Lines of Context:
420
420
    info = cr.get_info()
421
421
    return info
422
422
 
 
423
 
 
424
class ChangesetTree:
 
425
    def __init__(self, base_tree):
 
426
        self.base_tree = base_tree
 
427
        self._renamed = {}
 
428
        self._renamed_r = {}
 
429
        self._new_id = {}
 
430
        self._new_id_r = {}
 
431
        self.patches = {}
 
432
 
 
433
    def note_rename(self, old_path, new_path):
 
434
        assert not self._renamed.has_key(old_path)
 
435
        assert not self._renamed_r.has_key(new_path)
 
436
        self._renamed[new_path] = old_path
 
437
        self._renamed_r[old_path] = new_path
 
438
 
 
439
    def note_id(self, new_path, new_id):
 
440
        self._new_id[new_path] = new_id
 
441
        self._new_id_r[new_id] = new_path
 
442
 
 
443
    def note_patch(new_path, patch):
 
444
        self._patches[new_path] = patch
 
445
 
 
446
    def old_path(self, new_path):
 
447
        import os.path
 
448
        old_path = self._renamed.get(new_path)
 
449
        if old_path is not None:
 
450
            return old_path
 
451
        if self._renamed_r.has_key(new_path):
 
452
            return None
 
453
        dirname,basename = os.path.split(new_path)
 
454
        if dirname is not "":
 
455
            old_dir = self.old_path(dirname)
 
456
            if old_dir is None:
 
457
                return None
 
458
            return os.path.join(old_dir, basename)
 
459
        else:
 
460
            return new_path
 
461
 
 
462
    def new_path(self, old_path):
 
463
        import os.path
 
464
        new_path = self._renamed_r.get(old_path)
 
465
        if new_path is not None:
 
466
            return new_path
 
467
        if self._renamed.has_key(new_path):
 
468
            return None
 
469
        dirname,basename = os.path.split(old_path)
 
470
        if dirname is not '':
 
471
            new_dir = self.new_path(dirname)
 
472
            if new_dir is None:
 
473
                return None
 
474
            return os.path.join(new_dir, basename)
 
475
        else:
 
476
            return old_path
 
477
 
 
478
    def path2id(self, path):
 
479
        file_id = self._new_id.get(path)
 
480
        if file_id is not None:
 
481
            return file_id
 
482
        return self.base_tree.path2id(self.old_path(path))
 
483
 
 
484
    def id2path(self, file_id):
 
485
        path = self._new_id_r.get(file_id)
 
486
        if path is not None:
 
487
            return path
 
488
        return self.new_path(self.base_tree.id2path(file_id))
 
489
 
 
490
    def get_file(self, file_id):
 
491
        if self.base_tree.has_id(file_id):
 
492
            patch_original = self.base_tree.get_file(file_id)
 
493
        else:
 
494
            patch_original = None
 
495
        file_patch = patches.get(self.id2path(id))
 
496
        if file_patch is None:
 
497
            return patch_base
 
498
        return patched_file(file_patch, patch_original)
 
499
 
 
500
def patched_file(file_patch, original):
 
501
    from bzrlib.patch import patch
 
502
    from tempfile import mkdtemp
 
503
    from shutil import rmtree
 
504
    from StringIO import StringIO
 
505
    from osutils import pumpfile
 
506
    temp_dir = mkdtemp()
 
507
    try:
 
508
        if original is None:
 
509
            original_path = "/dev/null"
 
510
        else:
 
511
            original_path = os.path.join(temp_dir, "originalfile")
 
512
            temp_original = file(original_path, "wb")
 
513
            pumpfile(original, temp_original)
 
514
            temp_original.close()
 
515
        patched_path = os.path.join(temp_dir, "patchfile")
 
516
        patch(file_patch, original_path, patched_path)
 
517
        result = StringIO()
 
518
        temp_patched = file(patched_file, "rb")
 
519
        pumpfile(temp_patched, result)
 
520
        temp_patched.close()
 
521
        result.seek(0,0)
 
522
 
 
523
    finally:
 
524
        rmtree(temp_dir)
 
525
 
 
526
    return result
 
527
 
 
528
def test():
 
529
    import unittest
 
530
    from StringIO import StringIO
 
531
    class MockTree(object):
 
532
        def __init__(self):
 
533
            object.__init__(self)
 
534
            self.paths = {}
 
535
            self.ids = {}
 
536
            self.contents = {}
 
537
 
 
538
        def add_dir(self, file_id, path):
 
539
            self.paths[file_id] = path
 
540
            self.ids[path] = file_id
 
541
        
 
542
        def add_file(self, file_id, path, contents):
 
543
            self.add_dir(file_id, path)
 
544
            self.contents[file_id] = contents
 
545
 
 
546
        def path2id(self, path):
 
547
            return self.ids.get(path)
 
548
 
 
549
        def id2path(self, file_id):
 
550
            return self.paths.get(file_id)
 
551
 
 
552
        def get_file(file_id):
 
553
            result = StringIO()
 
554
            result.write(self.contents[file_id])
 
555
            result.seek(0,0)
 
556
            return result
 
557
 
 
558
    class CTreeTester(unittest.TestCase):
 
559
        def testRenames(self):
 
560
            mtree = MockTree()
 
561
            mtree.add_dir("a", "grandparent")
 
562
            mtree.add_dir("b", "grandparent/parent")
 
563
            mtree.add_file("c", "grandparent/parent/file", "Hello")
 
564
            ctree = ChangesetTree(mtree)
 
565
            print ctree.id2path("a")
 
566
            assert ctree.old_path("grandparent") == "grandparent"
 
567
            assert ctree.old_path("grandparent/parent") == "grandparent/parent"
 
568
            assert ctree.old_path("grandparent/parent/file") ==\
 
569
                "grandparent/parent/file"
 
570
 
 
571
            assert ctree.id2path("a") == "grandparent"
 
572
            assert ctree.id2path("b") == "grandparent/parent"
 
573
            assert ctree.id2path("c") == "grandparent/parent/file"
 
574
 
 
575
            assert ctree.path2id("grandparent") == "a"
 
576
            assert ctree.path2id("grandparent/parent") == "b"
 
577
            assert ctree.path2id("grandparent/parent/file") == "c"
 
578
 
 
579
            assert ctree.path2id("grandparent2") is None
 
580
            assert ctree.path2id("grandparent2/parent") is None
 
581
            assert ctree.path2id("grandparent2/parent/file") is None
 
582
 
 
583
            ctree.note_rename("grandparent", "grandparent2")
 
584
            assert ctree.old_path("grandparent") is None 
 
585
            assert ctree.old_path("grandparent/parent") is None 
 
586
            assert ctree.old_path("grandparent/parent/file") is None 
 
587
 
 
588
            assert ctree.id2path("a") == "grandparent2"
 
589
            assert ctree.id2path("b") == "grandparent2/parent"
 
590
            assert ctree.id2path("c") == "grandparent2/parent/file"
 
591
 
 
592
            assert ctree.path2id("grandparent2") == "a"
 
593
            assert ctree.path2id("grandparent2/parent") == "b"
 
594
            assert ctree.path2id("grandparent2/parent/file") == "c"
 
595
 
 
596
            assert ctree.path2id("grandparent") is None
 
597
            assert ctree.path2id("grandparent/parent") is None
 
598
            assert ctree.path2id("grandparent/parent/file") is None
 
599
 
 
600
            ctree.note_rename("grandparent/parent", "grandparent2/parent2")
 
601
            assert ctree.id2path("a") == "grandparent2"
 
602
            assert ctree.id2path("b") == "grandparent2/parent2"
 
603
            assert ctree.id2path("c") == "grandparent2/parent2/file"
 
604
 
 
605
            assert ctree.path2id("grandparent2") == "a"
 
606
            assert ctree.path2id("grandparent2/parent2") == "b"
 
607
            assert ctree.path2id("grandparent2/parent2/file") == "c"
 
608
 
 
609
            assert ctree.path2id("grandparent2/parent") is None
 
610
            assert ctree.path2id("grandparent2/parent/file") is None
 
611
        
 
612
    patchesTestSuite = unittest.makeSuite(CTreeTester,'test')
 
613
    runner = unittest.TextTestRunner()
 
614
    runner.run(patchesTestSuite)
 
615
 
423
616
if __name__ == '__main__':
424
617
    import sys
425
618
    print read_changeset(sys.stdin)