~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-12-12 16:50:31 UTC
  • Revision ID: abentley@panoramicfeedback.com-20061212165031-51w8gjy1eps1vnw0
update NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.errors import (BzrError,
22
22
                           NotBranchError,
23
23
                           NoWorkingTree,
24
 
                           BzrCommandError,
 
24
                           BzrCommandError, 
25
25
                           NoSuchRevision,
26
26
                           NoRepositoryPresent,
27
27
                          )
84
84
 
85
85
def make_archive(name, location):
86
86
    pb_location = pybaz.ArchiveLocation(location)
87
 
    pb_location.create_master(pybaz.Archive(name),
 
87
    pb_location.create_master(pybaz.Archive(name), 
88
88
                              pybaz.ArchiveLocationParams())
89
89
 
90
90
def test_environ():
275
275
            br_from.bzrdir.clone(to_location, revision_id)
276
276
        except NoSuchRevision:
277
277
            rmtree(to_location)
278
 
            msg = "The branch %s has no revision %s." % (from_location,
 
278
            msg = "The branch %s has no revision %s." % (from_location, 
279
279
                                                         revision_id)
280
280
            raise UserError(msg)
281
281
    finally:
282
282
        br_from.unlock()
283
283
 
284
 
def get_remaining_revisions(output_dir, version, encoding,
 
284
def get_remaining_revisions(output_dir, version, encoding, 
285
285
                            reuse_history_from=[]):
286
286
    last_patch = None
287
287
    old_revno = None
309
309
                    break
310
310
                # try to grab a copy of ancestor
311
311
                # note that is not optimised: we could look for namespace
312
 
                # transitions and only look for the past after the
 
312
                # transitions and only look for the past after the 
313
313
                # transition.
314
314
                for history_root in reuse_history_from:
315
315
                    possible_source = os.path.join(history_root,
332
332
                break
333
333
        else:
334
334
            raise UserError("Directory \"%s\" already exists, and the last "
335
 
                "revision (%s) is not in the ancestry of %s" %
 
335
                "revision (%s) is not in the ancestry of %s" % 
336
336
                (output_dir, last_patch, version))
337
337
        # Strip off all of the ancestors which are already present
338
338
        # And get a directory starting with the latest ancestor
344
344
 
345
345
###class Importer(object):
346
346
###    """An importer.
347
 
###
 
347
###    
348
348
###    Currently this is used as a parameter object, though more behaviour is
349
349
###    possible later.
350
350
###    """
351
351
###
352
352
###    def __init__(self, output_dir, version, fast=False,
353
 
###                 verbose=False, dry_run=False, max_count=None,
 
353
###                 verbose=False, dry_run=False, max_count=None, 
354
354
###                   reuse_history_from=[]):
355
355
###        self.output_dir = output_dir
356
356
###        self.version = version
362
362
                   reuse_history_from=[], standalone=True):
363
363
    """
364
364
    >>> q = test_environ()
365
 
 
 
365
    
366
366
    Progress bars output to stderr, but doctest does not capture that.
367
367
 
368
368
    >>> old_stderr = sys.stderr
424
424
            return
425
425
 
426
426
        progress_bar.note("importing %s into %s" % (version, output_dir))
427
 
 
 
427
    
428
428
        tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
429
429
                                   dir=os.path.dirname(output_dir))
430
430
        try:
433
433
            wt = None
434
434
        try:
435
435
            for result in iter_import_version(output_dir, ancestors, tempdir,
436
 
                    pb=progress_bar, encoding=encoding, fast=fast,
 
436
                    pb=progress_bar, encoding=encoding, fast=fast, 
437
437
                    verbose=verbose, dry_run=dry_run, max_count=max_count,
438
438
                    standalone=standalone):
439
439
                show_progress(progress_bar, result)
440
440
            if dry_run:
441
441
                progress_bar.note('Dry run, not modifying output_dir')
442
442
                return
443
 
 
 
443
    
444
444
            # Update the working tree of the branch
445
445
            try:
446
446
                wt = WorkingTree.open(output_dir)
450
450
                wt.set_last_revision(wt.branch.last_revision())
451
451
                wt.set_root_id(BAZ_IMPORT_ROOT)
452
452
                wt.revert([])
453
 
 
 
453
    
454
454
        finally:
455
 
 
 
455
            
456
456
            progress_bar.note('Cleaning up')
457
457
            shutil.rmtree(tempdir)
458
458
        progress_bar.note("Import complete.")
459
459
    finally:
460
460
        progress_bar.finished()
461
 
 
 
461
            
462
462
class UserError(BzrCommandError):
463
463
    def __init__(self, message):
464
464
        """Exception to throw when a user makes an impossible request
552
552
    if revision_id is None:
553
553
        revision_id = source.last_revision()
554
554
    wt = create_checkout(source, to_location, NULL_REVISION)
555
 
    wt.lock_write()
556
 
    try:
557
 
        wt.set_last_revision(revision_id)
558
 
        wt.flush()
559
 
        if revision_id not in (NULL_REVISION, None):
560
 
            basis = wt.basis_tree()
561
 
            basis.lock_read()
562
 
            try:
563
 
                wt._write_inventory(basis.inventory)
564
 
            finally:
565
 
                basis.unlock()
566
 
    finally:
567
 
        wt.unlock()
 
555
    wt.set_last_revision(revision_id)
 
556
    if revision_id not in (NULL_REVISION, None):
 
557
        wt._write_inventory(wt.basis_tree().inventory)
568
558
    return wt
569
559
 
570
560
 
571
 
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding,
 
561
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding, 
572
562
                        fast=False, verbose=False, dry_run=False,
573
563
                        max_count=None, standalone=False):
574
564
    revdir = None
671
661
                    allow_leftmost_as_ghost=True)
672
662
                missing_ancestor = None
673
663
            for merged_rev in direct_merges:
674
 
                target_tree.add_pending_merge(revision_id(merged_rev,
 
664
                target_tree.add_pending_merge(revision_id(merged_rev, 
675
665
                                                          encoding))
676
666
            target_tree.set_root_id(BAZ_IMPORT_ROOT)
677
 
            target_tree.flush()
678
667
            target_tree.set_inventory(baz_inv)
679
668
            commitobj = Commit(reporter=ImportCommitReporter())
680
669
            commitobj.commit(working_tree=target_tree,
691
680
    previous_version = revision.version
692
681
    if pybaz.WorkingTree(revdir).tree_version != previous_version:
693
682
        pybaz.WorkingTree(revdir).set_tree_version(previous_version)
694
 
    log_path = "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (revdir,
695
 
        revision.category.nonarch, revision.branch.nonarch,
 
683
    log_path = "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (revdir, 
 
684
        revision.category.nonarch, revision.branch.nonarch, 
696
685
        revision.version.nonarch, revision.archive, revision.patchlevel)
697
686
    temp_path = tempfile.mktemp(dir=os.path.dirname(revdir))
698
687
    os.rename(log_path, temp_path)
718
707
    tree = revision.get(revdir)
719
708
    log = get_log(tree, revision)
720
709
    try:
721
 
        return tree, bzr_inventory_data(tree), log
 
710
        return tree, bzr_inventory_data(tree), log 
722
711
    except BadFileKind, e:
723
 
        raise UserError("Cannot convert %s because %s is a %s" %
 
712
        raise UserError("Cannot convert %s because %s is a %s" % 
724
713
                        (revision,e.path, e.kind))
725
714
 
726
715
 
730
719
    try:
731
720
        return bzr_inventory_data(tree), log
732
721
    except BadFileKind, e:
733
 
        raise UserError("Cannot convert %s because %s is a %s" %
 
722
        raise UserError("Cannot convert %s because %s is a %s" % 
734
723
                        (revision,e.path, e.kind))
735
724
 
736
725
 
749
738
    inv_map = {}
750
739
    for arch_id, path in inv_iter:
751
740
        bzr_file_id = map_file_id(arch_id)
752
 
        inv_map[path] = bzr_file_id
 
741
        inv_map[path] = bzr_file_id 
753
742
 
754
743
    bzr_inv = []
755
744
    for path, file_id in inv_map.iteritems():
767
756
    return bzr_inv
768
757
 
769
758
 
770
 
def baz_import_branch(to_location, from_branch, fast, max_count, verbose,
 
759
def baz_import_branch(to_location, from_branch, fast, max_count, verbose, 
771
760
                      encoding, dry_run, reuse_history_list):
772
761
    to_location = os.path.realpath(str(to_location))
773
762
    if from_branch is not None:
778
767
            return 1
779
768
    if reuse_history_list is None:
780
769
        reuse_history_list = []
781
 
    import_version(to_location, from_branch, encoding, max_count=max_count,
 
770
    import_version(to_location, from_branch, encoding, max_count=max_count, 
782
771
                   reuse_history_from=reuse_history_list)
783
772
 
784
773
 
789
778
 
790
779
 
791
780
 
792
 
def baz_import(to_root_dir, from_archive, encoding, verbose=False,
 
781
def baz_import(to_root_dir, from_archive, encoding, verbose=False, 
793
782
               reuse_history_list=[], prefixes=None):
794
783
    if reuse_history_list is None:
795
784
        reuse_history_list = []
836
825
                os.makedirs(os.path.dirname(target))
837
826
            try:
838
827
                import_version(target, version, encoding,
839
 
                               reuse_history_from=reuse_history_from,
 
828
                               reuse_history_from=reuse_history_from, 
840
829
                               standalone=standalone)
841
830
            except pybaz.errors.ExecProblem,e:
842
831
                if str(e).find('The requested revision cannot be built.') != -1: