~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-09-25 12:47:12 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060925124712-a5bd93dbc33dab45
Remove shove, tweak imports, docs

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