~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-05-03 18:54:10 UTC
  • mto: This revision was merged to the branch mainline in revision 366.
  • Revision ID: abentley@panoramicfeedback.com-20060503185410-2e0cc2db4fad30a0
Merge progress bar updates from robertc

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
from progress import *
58
58
 
59
59
class ImportCommitReporter(NullCommitReporter):
60
 
    def __init__(self, pb):
61
 
        self.pb = pb
62
60
 
63
61
    def escaped(self, escape_count, message):
64
 
        self.pb.clear()
65
62
        bzrlib.trace.warning("replaced %d control characters in message" %
66
63
                             escape_count)
67
64
 
345
342
###    possible later.
346
343
###    """
347
344
###
348
 
###    def __init__(self, output_dir, version, printer, fancy=True, fast=False,
 
345
###    def __init__(self, output_dir, version, fast=False,
349
346
###                 verbose=False, dry_run=False, max_count=None, 
350
347
###                   reuse_history_from=[]):
351
348
###        self.output_dir = output_dir
353
350
###        self.
354
351
 
355
352
 
356
 
def import_version(output_dir, version, printer, fancy=True, fast=False,
 
353
def import_version(output_dir, version, fast=False,
357
354
                   verbose=False, dry_run=False, max_count=None,
358
355
                   reuse_history_from=[], standalone=True):
359
356
    """
360
357
    >>> q = test_environ()
 
358
    
 
359
    Progress bars output to stderr, but doctest does not capture that.
 
360
 
 
361
    >>> old_stderr = sys.stderr
 
362
    >>> sys.stderr = sys.stdout
 
363
 
361
364
    >>> result_path = os.path.join(q, "result")
362
365
    >>> commit_test_revisions()
363
366
    >>> version = pybaz.Version("test@example.com/test--test--0.1")
364
 
    >>> def printer(message): print message
365
 
    >>> import_version('/', version, printer, fancy=False, dry_run=True)
 
367
    >>> old_ui = bzrlib.ui.ui_factory
 
368
    >>> bzrlib.ui.ui_factory = bzrlib.ui.text.TextUIFactory(
 
369
    ...     bar_type=bzrlib.progress.DotsProgressBar)
 
370
 
 
371
    >>> import_version('/', version, dry_run=True)
366
372
    Traceback (most recent call last):
367
373
    NotPreviousImport: / is not the location of a previous import.
368
 
    >>> import_version(result_path, version, printer, fancy=False, dry_run=True)
 
374
    >>> import_version(result_path, version, dry_run=True)
369
375
    Traceback (most recent call last):
370
376
    UserError: The version test@example.com/test--test--0.1 does not exist.
371
377
    >>> version = pybaz.Version("test@example.com/test--test--0")
372
 
    >>> import_version(result_path, version, printer, fancy=False, dry_run=True)
373
 
    not fancy
374
 
    ....
 
378
    >>> import_version(result_path, version, dry_run=True) #doctest: +ELLIPSIS
 
379
    importing test@example.com/test--test--0 into ...
 
380
    ...
 
381
    revisions: ..........................................
375
382
    Dry run, not modifying output_dir
376
383
    Cleaning up
377
 
    >>> import_version(result_path, version, printer, fancy=False)
378
 
    not fancy
379
 
    ....
 
384
    >>> import_version(result_path, version) #doctest: +ELLIPSIS
 
385
    importing test@example.com/test--test--0 into ...
 
386
    ...
 
387
    revisions: .....................................................................
380
388
    Cleaning up
381
389
    Import complete.
382
 
    >>> import_version(result_path, version, printer, fancy=False)
 
390
    >>> import_version(result_path, version) #doctest: +ELLIPSIS
383
391
    Tree is up-to-date with test@example.com/test--test--0--patch-2
384
392
    >>> commit_more_test_revisions()
385
 
    >>> import_version(result_path, version, printer, fancy=False)
386
 
    not fancy
387
 
    ..
 
393
    >>> import_version(result_path, version) #doctest: +ELLIPSIS
 
394
    importing test@example.com/test--test--0 into ...
 
395
    revisions: ....................................................
388
396
    Cleaning up
389
397
    Import complete.
 
398
    >>> bzrlib.ui.ui_factory = old_ui
 
399
    >>> sys.stderr = old_stderr
390
400
    >>> teardown_environ(q)
391
401
    """
 
402
    progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
392
403
    try:
393
 
        ancestors, old_revno = get_remaining_revisions(output_dir, version,
394
 
                                                       reuse_history_from)
395
 
    except NotBranchError, e:
396
 
        raise NotPreviousImport(e.path)
397
 
    if old_revno is None and len(ancestors) == 0:
398
 
        print 'Version %s has no revisions.' % version
399
 
        return
400
 
    if len(ancestors) == 0:
401
 
        last_revision = get_last_revision(Branch.open(output_dir))
402
 
        print 'Tree is up-to-date with %s' % last_revision
403
 
        return
 
404
        try:
 
405
            ancestors, old_revno = get_remaining_revisions(output_dir, version,
 
406
                                                           reuse_history_from)
 
407
        except NotBranchError, e:
 
408
            raise NotPreviousImport(e.path)
 
409
        if old_revno is None and len(ancestors) == 0:
 
410
            progress_bar.note('Version %s has no revisions.' % version)
 
411
            return
 
412
        if len(ancestors) == 0:
 
413
            last_revision = get_last_revision(Branch.open(output_dir))
 
414
            progress_bar.note('Tree is up-to-date with %s' % last_revision)
 
415
            return
404
416
 
405
 
    progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
406
 
    tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
407
 
                               dir=os.path.dirname(output_dir))
408
 
    try:
409
 
        wt = WorkingTree.open(output_dir)
410
 
    except (NotBranchError, NoWorkingTree):
411
 
        wt = None
412
 
    if wt is None:
413
 
        old_basis = EmptyTree()
414
 
    else:
415
 
        old_basis = wt.basis_tree()
416
 
    try:
417
 
        if not fancy:
418
 
            print "not fancy"
 
417
        progress_bar.note("importing %s into %s" % (version, output_dir))
 
418
    
 
419
        tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
 
420
                                   dir=os.path.dirname(output_dir))
 
421
        try:
 
422
            wt = WorkingTree.open(output_dir)
 
423
        except (NotBranchError, NoWorkingTree):
 
424
            wt = None
 
425
        if wt is None:
 
426
            old_basis = EmptyTree()
 
427
        else:
 
428
            old_basis = wt.basis_tree()
419
429
        try:
420
430
            for result in iter_import_version(output_dir, ancestors, tempdir,
421
 
                    progress_bar, fast=fast, verbose=verbose, dry_run=dry_run,
 
431
                    pb=progress_bar,
 
432
                    fast=fast, verbose=verbose, dry_run=dry_run,
422
433
                    max_count=max_count, standalone=standalone):
423
 
                if fancy:
424
 
                    show_progress(progress_bar, result)
425
 
                else:
426
 
                    sys.stdout.write('.')
 
434
                show_progress(progress_bar, result)
 
435
            if dry_run:
 
436
                progress_bar.note('Dry run, not modifying output_dir')
 
437
                return
 
438
    
 
439
            # Update the working tree of the branch
 
440
            try:
 
441
                wt = WorkingTree.open(output_dir)
 
442
            except NoWorkingTree:
 
443
                wt = None
 
444
            if wt is not None:
 
445
                wt.set_last_revision(wt.branch.last_revision())
 
446
                merge_inner(wt.branch, wt.basis_tree(), old_basis, 
 
447
                            ignore_zero=True, this_tree=wt)
 
448
                wt.revert([])
 
449
    
427
450
        finally:
428
 
            if fancy:
429
 
                progress_bar.finished()
430
 
            else:
431
 
                sys.stdout.write('\n')
432
 
 
433
 
        if dry_run:
434
 
            print 'Dry run, not modifying output_dir'
435
 
            return
436
 
 
437
 
        # Update the working tree of the branch
438
 
        try:
439
 
            wt = WorkingTree.open(output_dir)
440
 
        except NoWorkingTree:
441
 
            wt = None
442
 
        if wt is not None:
443
 
            wt.set_last_revision(wt.branch.last_revision())
444
 
            merge_inner(wt.branch, wt.basis_tree(), old_basis, 
445
 
                        ignore_zero=True, this_tree=wt)
446
 
            wt.revert([])
447
 
 
 
451
            
 
452
            progress_bar.note('Cleaning up')
 
453
            shutil.rmtree(tempdir)
 
454
        progress_bar.note("Import complete.")
448
455
    finally:
449
 
        printer('Cleaning up')
450
 
        shutil.rmtree(tempdir)
451
 
    printer("Import complete.")
 
456
        progress_bar.finished()
452
457
            
453
458
class UserError(BzrCommandError):
454
459
    def __init__(self, message):
573
578
        if verbose:
574
579
            version = str(revision.version)
575
580
            if version != previous_version:
576
 
                clear_progress_bar()
577
 
                print '\rOn version: %s' % version
 
581
                pb.note('On version: %s' % version)
578
582
            yield Progress(str(revision.patchlevel), i, len(ancestors))
579
583
            previous_version = version
580
584
        else:
592
596
                    raise
593
597
                missing_ancestor = revision
594
598
                revdir = None
595
 
                print ("unable to access ancestor %s, making into a merge."
 
599
                pb.note("unable to access ancestor %s, making into a merge."
596
600
                       % missing_ancestor)
597
601
                continue
598
602
            target_tree = create_checkout_metadata(target_branch, revdir)
631
635
            for merged_rev in direct_merges:
632
636
                target_tree.add_pending_merge(revision_id(merged_rev))
633
637
            target_tree.set_inventory(baz_inv)
634
 
            commitobj = Commit(reporter=ImportCommitReporter(pb))
 
638
            commitobj = Commit(reporter=ImportCommitReporter())
635
639
            commitobj.commit(working_tree=target_tree,
636
640
                             message=log_message.decode('ascii', 'replace'), 
637
641
                             verbose=False, committer=log_creator,
728
732
    takes_args = ['to_location', 'from_branch?', 'reuse_history*']
729
733
    takes_options = ['verbose', 'max-count']
730
734
 
731
 
    def printer(self, name):
732
 
        print name
733
 
 
734
735
    def run(self, to_location, from_branch=None, fast=False, max_count=None,
735
736
            verbose=False, dry_run=False, reuse_history_list=[]):
736
737
        to_location = os.path.realpath(str(to_location))
742
743
                return 1
743
744
        if reuse_history_list is None:
744
745
            reuse_history_list = []
745
 
        import_version(to_location, from_branch, self.printer, 
 
746
        import_version(to_location, from_branch, 
746
747
                       max_count=max_count, 
747
748
                       reuse_history_from=reuse_history_list)
748
749
 
775
776
    takes_options = ['verbose', Option('prefixes', type=str,
776
777
                     help="Prefixes of branches to import, colon-separated")]
777
778
 
778
 
    def printer(self, name):
779
 
        print name
780
 
 
781
779
    def run(self, to_root_dir, from_archive, verbose=False,
782
780
            reuse_history_list=[], prefixes=None):
783
781
        if reuse_history_list is None:
787
785
            os.mkdir(to_root)
788
786
        if prefixes is not None:
789
787
            prefixes = prefixes.split(':')
790
 
        import_archive(to_root, from_archive, verbose, self.printer, 
 
788
        import_archive(to_root, from_archive, verbose,
791
789
                       reuse_history_list, prefixes=prefixes)
792
790
 
793
791
 
794
 
def import_archive(to_root, from_archive, verbose, printer,
 
792
def import_archive(to_root, from_archive, verbose,
795
793
                   reuse_history_from=[], standalone=False,
796
794
                   prefixes=None):
797
795
    def selected(version):
813
811
        except NoRepositoryPresent:
814
812
            raise BzrCommandError("Can't create repository at existing branch.")
815
813
    versions = list(pybaz.Archive(str(from_archive)).iter_versions())
816
 
#    progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
 
814
    progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
817
815
    try:
818
816
        for num, version in enumerate(versions):
819
 
#            progress_bar.update("Branch", num, len(versions))
 
817
            progress_bar.update("Branch", num, len(versions))
820
818
            if not selected(version):
821
819
                print "Skipping %s" % version
822
820
                continue
823
821
            target = os.path.join(to_root, map_namespace(version))
824
 
            printer("importing %s into %s" % (version, target))
825
822
            if not os.path.exists(os.path.dirname(target)):
826
823
                os.makedirs(os.path.dirname(target))
827
824
            try:
828
 
                import_version(target, version, printer,
 
825
                import_version(target, version,
829
826
                               reuse_history_from=reuse_history_from, 
830
827
                               standalone=standalone)
831
828
            except pybaz.errors.ExecProblem,e:
832
829
                if str(e).find('The requested revision cannot be built.') != -1:
833
 
                    printer("Skipping version %s as it cannot be built due"
834
 
                            " to a missing parent archive." % version)
 
830
                    progress_bar.note(
 
831
                        "Skipping version %s as it cannot be built due"
 
832
                        " to a missing parent archive." % version)
835
833
                else:
836
834
                    raise
837
835
            except UserError, e:
838
836
                if str(e).find('already exists, and the last revision ') != -1:
839
 
                    printer("Skipping version %s as it has had commits made"
840
 
                            " since it was converted to bzr." % version)
 
837
                    progress_bar.note(
 
838
                        "Skipping version %s as it has had commits made"
 
839
                        " since it was converted to bzr." % version)
841
840
                else:
842
841
                    raise
843
842
    finally:
844
 
        pass
845
 
#        progress_bar.finished()
 
843
        progress_bar.finished()
 
844
 
846
845
 
847
846
def map_namespace(a_version):
848
847
    a_version = pybaz.Version("%s" % a_version)
856
855
        return "%s/%s" % (category, branch)
857
856
    return "%s/%s/%s" % (category, version, branch)
858
857
 
 
858
 
859
859
def map_file_id(file_id):
860
860
    """Convert a baz file id to a bzr one."""
861
861
    return file_id.replace('%', '%25').replace('/', '%2f')