~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-03-14 18:10:04 UTC
  • mto: This revision was merged to the branch mainline in revision 329.
  • Revision ID: abentley@panoramicfeedback.com-20060314181004-ea3edbc59ddc8ae3
Handle aliases in bzr shell

Show diffs side-by-side

added added

removed removed

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