~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-01-17 14:30:19 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070117143019-rj3n4op1aqav7bs5
Update versions to 0.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
import email.Utils
57
57
from progress import *
58
58
 
 
59
 
 
60
BAZ_IMPORT_ROOT = 'TREE_ROOT'
 
61
 
 
62
 
59
63
class ImportCommitReporter(NullCommitReporter):
60
64
 
61
65
    def escaped(self, escape_count, message):
277
281
    finally:
278
282
        br_from.unlock()
279
283
 
280
 
def get_remaining_revisions(output_dir, version, reuse_history_from=[]):
 
284
def get_remaining_revisions(output_dir, version, encoding, 
 
285
                            reuse_history_from=[]):
281
286
    last_patch = None
282
287
    old_revno = None
283
288
    output_exists = os.path.exists(output_dir)
285
290
        # We are starting from an existing directory, figure out what
286
291
        # the current version is
287
292
        branch = Branch.open(output_dir)
288
 
        last_patch = get_last_revision(branch)
 
293
        last_patch, last_encoding = get_last_revision(branch)
 
294
        assert encoding == last_encoding
289
295
        if last_patch is None:
290
296
            if branch.last_revision() != None:
291
297
                raise NotPreviousImport(branch.base)
310
316
                        map_namespace(ancestor.version))
311
317
                    try:
312
318
                        source = Branch.open(possible_source)
313
 
                        rev_id = revision_id(ancestor)
 
319
                        rev_id = revision_id(ancestor, encoding)
314
320
                        if rev_id in source.revision_history():
315
321
                            do_branch(source, output_dir, rev_id)
316
322
                            last_patch = ancestor
351
357
###        self.
352
358
 
353
359
 
354
 
def import_version(output_dir, version, fast=False,
 
360
def import_version(output_dir, version, encoding, fast=False,
355
361
                   verbose=False, dry_run=False, max_count=None,
356
362
                   reuse_history_from=[], standalone=True):
357
363
    """
369
375
    >>> bzrlib.ui.ui_factory = bzrlib.ui.text.TextUIFactory(
370
376
    ...     bar_type=bzrlib.progress.DotsProgressBar)
371
377
 
372
 
    >>> import_version('/', version, dry_run=True)
 
378
    >>> import_version('/', version, None, dry_run=True)
373
379
    Traceback (most recent call last):
374
380
    NotPreviousImport: / is not the location of a previous import.
375
 
    >>> import_version(result_path, version, dry_run=True)
 
381
    >>> import_version(result_path, version, None, dry_run=True)
376
382
    Traceback (most recent call last):
377
383
    UserError: The version test@example.com/test--test--0.1 does not exist.
378
384
    >>> version = pybaz.Version("test@example.com/test--test--0")
379
 
    >>> import_version(result_path, version, dry_run=True) #doctest: +ELLIPSIS
 
385
    >>> import_version(result_path, version, None, dry_run=True) #doctest: +ELLIPSIS
380
386
    importing test@example.com/test--test--0 into ...
381
387
    ...
382
388
    revisions: ..........................................
383
389
    Dry run, not modifying output_dir
384
390
    Cleaning up
385
 
    >>> import_version(result_path, version) #doctest: +ELLIPSIS
 
391
    >>> import_version(result_path, version, None) #doctest: +ELLIPSIS
386
392
    importing test@example.com/test--test--0 into ...
387
393
    ...
388
394
    revisions: .....................................................................
389
395
    Cleaning up
390
396
    Import complete.
391
 
    >>> import_version(result_path, version) #doctest: +ELLIPSIS
 
397
    >>> import_version(result_path, version, None) #doctest: +ELLIPSIS
392
398
    Tree is up-to-date with test@example.com/test--test--0--patch-2
393
399
    >>> commit_more_test_revisions()
394
 
    >>> import_version(result_path, version) #doctest: +ELLIPSIS
 
400
    >>> import_version(result_path, version, None) #doctest: +ELLIPSIS
395
401
    importing test@example.com/test--test--0 into ...
396
402
    revisions: ....................................................
397
403
    Cleaning up
404
410
    try:
405
411
        try:
406
412
            ancestors, old_revno = get_remaining_revisions(output_dir, version,
 
413
                                                           encoding,
407
414
                                                           reuse_history_from)
408
415
        except NotBranchError, e:
409
416
            raise NotPreviousImport(e.path)
411
418
            progress_bar.note('Version %s has no revisions.' % version)
412
419
            return
413
420
        if len(ancestors) == 0:
414
 
            last_revision = get_last_revision(Branch.open(output_dir))
 
421
            last_revision, last_encoding = \
 
422
                get_last_revision(Branch.open(output_dir))
415
423
            progress_bar.note('Tree is up-to-date with %s' % last_revision)
416
424
            return
417
425
 
425
433
            wt = None
426
434
        try:
427
435
            for result in iter_import_version(output_dir, ancestors, tempdir,
428
 
                    pb=progress_bar,
429
 
                    fast=fast, verbose=verbose, dry_run=dry_run,
430
 
                    max_count=max_count, standalone=standalone):
 
436
                    pb=progress_bar, encoding=encoding, fast=fast, 
 
437
                    verbose=verbose, dry_run=dry_run, max_count=max_count,
 
438
                    standalone=standalone):
431
439
                show_progress(progress_bar, result)
432
440
            if dry_run:
433
441
                progress_bar.note('Dry run, not modifying output_dir')
440
448
                wt = None
441
449
            if wt is not None:
442
450
                wt.set_last_revision(wt.branch.last_revision())
 
451
                wt.set_root_id(BAZ_IMPORT_ROOT)
443
452
                wt.revert([])
444
453
    
445
454
        finally:
464
473
                           % path)
465
474
 
466
475
 
467
 
def revision_id(arch_revision):
 
476
def revision_id(arch_revision, encoding):
468
477
    """
469
478
    Generate a Bzr revision id from an Arch revision id.  'x' in the id
470
479
    designates a revision imported with an experimental algorithm.  A number
472
481
 
473
482
    :param arch_revision: The Arch revision to generate an ID for.
474
483
 
475
 
    >>> revision_id(pybaz.Revision("you@example.com/cat--br--0--base-0"))
 
484
    >>> revision_id(pybaz.Revision("you@example.com/cat--br--0--base-0"), None)
476
485
    'Arch-1:you@example.com%cat--br--0--base-0'
 
486
    >>> revision_id(pybaz.Revision("you@example.com/cat--br--0--base-0"), 'utf-8')
 
487
    'Arch-1-utf-8:you@example.com%cat--br--0--base-0'
477
488
    """
478
 
    return "Arch-1:%s" % str(arch_revision).replace('/', '%')
 
489
    if encoding is None:
 
490
        encoding = ''
 
491
    else:
 
492
        encoding = '-' + encoding
 
493
    return "Arch-1%s:%s" % (encoding, str(arch_revision).replace('/', '%'))
479
494
 
480
495
class NotArchRevision(Exception):
481
496
    def __init__(self, revision_id):
491
506
    >>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--base-5"))
492
507
    Traceback (most recent call last):
493
508
    NotArchRevision: The revision id Arch-1:jrandom@example.com%test--test--0--base-5 does not look like it came from Arch.
494
 
    >>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5"))
495
 
    'jrandom@example.com/test--test--0--patch-5'
 
509
    >>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5")[0])
 
510
    'jrandom@example.com/test--test--0--patch-5'
 
511
    >>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5")[0])
 
512
    'jrandom@example.com/test--test--0--patch-5'
 
513
    >>> str(arch_revision("Arch-1:jrandom@example.com%test--test--0--patch-5")[1])
 
514
    'None'
 
515
    >>> str(arch_revision("Arch-1-utf-8:jrandom@example.com%test--test--0--patch-5")[1])
 
516
    'utf-8'
496
517
    """
497
518
    if revision_id is None:
498
 
        return None
499
 
    if revision_id[:7] != 'Arch-1:':
 
519
        return None, None
 
520
    if revision_id[:7] not in ('Arch-1:', 'Arch-1-'):
500
521
        raise NotArchRevision(revision_id)
501
522
    else:
502
523
        try:
503
 
            return pybaz.Revision(revision_id[7:].replace('%', '/'))
 
524
            encoding, arch_name = revision_id[6:].split(':', 1)
 
525
            arch_name = arch_name.replace('%', '/')
 
526
            if encoding == '':
 
527
                encoding = None
 
528
            else:
 
529
                encoding = encoding[1:]
 
530
            return pybaz.Revision(arch_name), encoding
504
531
        except pybaz.errors.NamespaceError, e:
505
532
            raise NotArchRevision(revision_id)
506
533
 
526
553
        revision_id = source.last_revision()
527
554
    wt = create_checkout(source, to_location, NULL_REVISION)
528
555
    wt.set_last_revision(revision_id)
529
 
    wt._write_inventory(wt.basis_tree().inventory)
 
556
    if revision_id not in (NULL_REVISION, None):
 
557
        wt._write_inventory(wt.basis_tree().inventory)
530
558
    return wt
531
559
 
532
560
 
533
 
def iter_import_version(output_dir, ancestors, tempdir, pb, fast=False,
534
 
                        verbose=False, dry_run=False, max_count=None,
535
 
                        standalone=False):
 
561
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding, 
 
562
                        fast=False, verbose=False, dry_run=False,
 
563
                        max_count=None, standalone=False):
536
564
    revdir = None
 
565
    log_encoding = 'ascii'
 
566
    if encoding is not None:
 
567
        log_encoding = encoding
537
568
 
538
569
    # Uncomment this for testing, it basically just has baz2bzr only update
539
570
    # 5 patches at a time
568
599
 
569
600
    for i in range(len(ancestors)):
570
601
        revision = ancestors[i]
571
 
        rev_id = revision_id(revision)
 
602
        rev_id = revision_id(revision, encoding)
572
603
        direct_merges = []
573
604
        if verbose:
574
605
            version = str(revision.version)
625
656
        try:
626
657
            if missing_ancestor:
627
658
                # if we want it to be in revision-history, do that here.
628
 
                target_tree.set_parent_ids([revision_id(missing_ancestor)],
629
 
                                           allow_leftmost_as_ghost=True)
 
659
                target_tree.set_parent_ids(
 
660
                    [revision_id(missing_ancestor, encoding)],
 
661
                    allow_leftmost_as_ghost=True)
630
662
                missing_ancestor = None
631
663
            for merged_rev in direct_merges:
632
 
                target_tree.add_pending_merge(revision_id(merged_rev))
 
664
                target_tree.add_pending_merge(revision_id(merged_rev, 
 
665
                                                          encoding))
 
666
            target_tree.set_root_id(BAZ_IMPORT_ROOT)
633
667
            target_tree.set_inventory(baz_inv)
634
668
            commitobj = Commit(reporter=ImportCommitReporter())
635
669
            commitobj.commit(working_tree=target_tree,
636
 
                             message=log_message.decode('ascii', 'replace'), 
637
 
                             verbose=False, committer=log_creator,
638
 
                             timestamp=timestamp, timezone=0, rev_id=rev_id,
639
 
                             revprops={})
 
670
                message=log_message.decode(log_encoding, 'replace'),
 
671
                verbose=False, committer=log_creator, timestamp=timestamp,
 
672
                timezone=0, rev_id=rev_id, revprops={})
640
673
        finally:
641
674
            target_tree.unlock()
642
675
            branch.unlock()
724
757
 
725
758
 
726
759
def baz_import_branch(to_location, from_branch, fast, max_count, verbose, 
727
 
                      dry_run, reuse_history_list):
 
760
                      encoding, dry_run, reuse_history_list):
728
761
    to_location = os.path.realpath(str(to_location))
729
762
    if from_branch is not None:
730
763
        try:
734
767
            return 1
735
768
    if reuse_history_list is None:
736
769
        reuse_history_list = []
737
 
    import_version(to_location, from_branch, 
738
 
                   max_count=max_count, 
 
770
    import_version(to_location, from_branch, encoding, max_count=max_count, 
739
771
                   reuse_history_from=reuse_history_list)
740
772
 
741
773
 
746
778
 
747
779
 
748
780
 
749
 
def baz_import(to_root_dir, from_archive, verbose=False, reuse_history_list=[],
750
 
               prefixes=None):
 
781
def baz_import(to_root_dir, from_archive, encoding, verbose=False, 
 
782
               reuse_history_list=[], prefixes=None):
751
783
    if reuse_history_list is None:
752
784
        reuse_history_list = []
753
785
    to_root = str(os.path.realpath(to_root_dir))
755
787
        os.mkdir(to_root)
756
788
    if prefixes is not None:
757
789
        prefixes = prefixes.split(':')
758
 
    import_archive(to_root, from_archive, verbose,
 
790
    import_archive(to_root, from_archive, verbose, encoding,
759
791
                   reuse_history_list, prefixes=prefixes)
760
792
 
761
793
 
762
794
def import_archive(to_root, from_archive, verbose,
763
 
                   reuse_history_from=[], standalone=False,
 
795
                   encoding, reuse_history_from=[], standalone=False,
764
796
                   prefixes=None):
765
797
    def selected(version):
766
798
        if prefixes is None:
792
824
            if not os.path.exists(os.path.dirname(target)):
793
825
                os.makedirs(os.path.dirname(target))
794
826
            try:
795
 
                import_version(target, version,
 
827
                import_version(target, version, encoding,
796
828
                               reuse_history_from=reuse_history_from, 
797
829
                               standalone=standalone)
798
830
            except pybaz.errors.ExecProblem,e: