~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-06-12 22:09:44 UTC
  • mfrom: (540.1.2 bzrtools-0.17)
  • Revision ID: aaron.bentley@utoronto.ca-20070612220944-5zw4hlzp1ctq6mkl
Merge fixes from 0.17

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.set_last_revision(revision_id)
556
 
    if revision_id not in (NULL_REVISION, None):
557
 
        wt._write_inventory(wt.basis_tree().inventory)
 
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()
558
568
    return wt
559
569
 
560
570
 
561
 
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding, 
 
571
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding,
562
572
                        fast=False, verbose=False, dry_run=False,
563
573
                        max_count=None, standalone=False):
564
574
    revdir = None
661
671
                    allow_leftmost_as_ghost=True)
662
672
                missing_ancestor = None
663
673
            for merged_rev in direct_merges:
664
 
                target_tree.add_pending_merge(revision_id(merged_rev, 
 
674
                target_tree.add_pending_merge(revision_id(merged_rev,
665
675
                                                          encoding))
666
676
            target_tree.set_root_id(BAZ_IMPORT_ROOT)
 
677
            target_tree.flush()
667
678
            target_tree.set_inventory(baz_inv)
668
679
            commitobj = Commit(reporter=ImportCommitReporter())
669
680
            commitobj.commit(working_tree=target_tree,
680
691
    previous_version = revision.version
681
692
    if pybaz.WorkingTree(revdir).tree_version != previous_version:
682
693
        pybaz.WorkingTree(revdir).set_tree_version(previous_version)
683
 
    log_path = "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (revdir, 
684
 
        revision.category.nonarch, revision.branch.nonarch, 
 
694
    log_path = "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (revdir,
 
695
        revision.category.nonarch, revision.branch.nonarch,
685
696
        revision.version.nonarch, revision.archive, revision.patchlevel)
686
697
    temp_path = tempfile.mktemp(dir=os.path.dirname(revdir))
687
698
    os.rename(log_path, temp_path)
707
718
    tree = revision.get(revdir)
708
719
    log = get_log(tree, revision)
709
720
    try:
710
 
        return tree, bzr_inventory_data(tree), log 
 
721
        return tree, bzr_inventory_data(tree), log
711
722
    except BadFileKind, e:
712
 
        raise UserError("Cannot convert %s because %s is a %s" % 
 
723
        raise UserError("Cannot convert %s because %s is a %s" %
713
724
                        (revision,e.path, e.kind))
714
725
 
715
726
 
719
730
    try:
720
731
        return bzr_inventory_data(tree), log
721
732
    except BadFileKind, e:
722
 
        raise UserError("Cannot convert %s because %s is a %s" % 
 
733
        raise UserError("Cannot convert %s because %s is a %s" %
723
734
                        (revision,e.path, e.kind))
724
735
 
725
736
 
738
749
    inv_map = {}
739
750
    for arch_id, path in inv_iter:
740
751
        bzr_file_id = map_file_id(arch_id)
741
 
        inv_map[path] = bzr_file_id 
 
752
        inv_map[path] = bzr_file_id
742
753
 
743
754
    bzr_inv = []
744
755
    for path, file_id in inv_map.iteritems():
756
767
    return bzr_inv
757
768
 
758
769
 
759
 
def baz_import_branch(to_location, from_branch, fast, max_count, verbose, 
 
770
def baz_import_branch(to_location, from_branch, fast, max_count, verbose,
760
771
                      encoding, dry_run, reuse_history_list):
761
772
    to_location = os.path.realpath(str(to_location))
762
773
    if from_branch is not None:
767
778
            return 1
768
779
    if reuse_history_list is None:
769
780
        reuse_history_list = []
770
 
    import_version(to_location, from_branch, encoding, max_count=max_count, 
 
781
    import_version(to_location, from_branch, encoding, max_count=max_count,
771
782
                   reuse_history_from=reuse_history_list)
772
783
 
773
784
 
778
789
 
779
790
 
780
791
 
781
 
def baz_import(to_root_dir, from_archive, encoding, verbose=False, 
 
792
def baz_import(to_root_dir, from_archive, encoding, verbose=False,
782
793
               reuse_history_list=[], prefixes=None):
783
794
    if reuse_history_list is None:
784
795
        reuse_history_list = []
825
836
                os.makedirs(os.path.dirname(target))
826
837
            try:
827
838
                import_version(target, version, encoding,
828
 
                               reuse_history_from=reuse_history_from, 
 
839
                               reuse_history_from=reuse_history_from,
829
840
                               standalone=standalone)
830
841
            except pybaz.errors.ExecProblem,e:
831
842
                if str(e).find('The requested revision cannot be built.') != -1: