~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-05-10 01:29:41 UTC
  • mfrom: (364.1.4 bzrtools)
  • Revision ID: aaron.bentley@utoronto.ca-20060510012941-c46cf94b001d8d3e
Merge

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