~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-12-27 16:30:52 UTC
  • Revision ID: abentley@panoramicfeedback.com-20071227163052-l4wmn2vsl91nbfjh
Remove test due to intentional behavior change

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
                          )
30
30
from bzrlib.commands import Command
31
31
from bzrlib.option import _global_option, Option
32
32
from bzrlib.merge import merge_inner
33
 
from bzrlib.revision import NULL_REVISION
 
33
from bzrlib.revision import NULL_REVISION, is_null
34
34
import bzrlib.ui
35
35
import bzrlib.ui.text
36
36
from bzrlib.workingtree import WorkingTree
42
42
    from pybaz.backends.baz import null_cmd
43
43
except ImportError:
44
44
    raise NoPyBaz
45
 
from fai import iter_new_merges, direct_merges
 
45
from baz_helper import iter_new_merges, direct_merges
46
46
import tempfile
47
47
import os
48
48
import os.path
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
293
293
        last_patch, last_encoding = get_last_revision(branch)
294
294
        assert encoding == last_encoding
295
295
        if last_patch is None:
296
 
            if branch.last_revision() != None:
 
296
            if not is_null(branch.last_revision()):
297
297
                raise NotPreviousImport(branch.base)
298
298
        elif version is None:
299
299
            version = last_patch.version
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)
449
449
            if wt is not None:
450
450
                wt.set_last_revision(wt.branch.last_revision())
451
451
                wt.set_root_id(BAZ_IMPORT_ROOT)
452
 
                wt.revert([])
453
 
    
 
452
                wt.revert()
 
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
514
514
    'None'
515
515
    >>> str(arch_revision("Arch-1-utf-8:jrandom@example.com%test--test--0--patch-5")[1])
516
516
    'utf-8'
 
517
    >>> str(arch_revision('null:'))
 
518
    '(None, None)'
517
519
    """
518
 
    if revision_id is None:
 
520
    if is_null(revision_id):
519
521
        return None, None
520
522
    if revision_id[:7] not in ('Arch-1:', 'Arch-1-'):
521
523
        raise NotArchRevision(revision_id)
552
554
    if revision_id is None:
553
555
        revision_id = source.last_revision()
554
556
    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)
 
557
    wt.lock_write()
 
558
    try:
 
559
        wt.set_last_revision(revision_id)
 
560
        wt.flush()
 
561
        if revision_id not in (NULL_REVISION, None):
 
562
            basis = wt.basis_tree()
 
563
            basis.lock_read()
 
564
            try:
 
565
                wt._write_inventory(basis.inventory)
 
566
            finally:
 
567
                basis.unlock()
 
568
    finally:
 
569
        wt.unlock()
558
570
    return wt
559
571
 
560
572
 
561
 
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding, 
 
573
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding,
562
574
                        fast=False, verbose=False, dry_run=False,
563
575
                        max_count=None, standalone=False):
564
576
    revdir = None
597
609
        else:
598
610
            target_branch = create_branch(output_dir)
599
611
 
600
 
    for i in range(len(ancestors)):
601
 
        revision = ancestors[i]
 
612
    for i, revision in enumerate(ancestors):
602
613
        rev_id = revision_id(revision, encoding)
603
614
        direct_merges = []
604
615
        if verbose:
611
622
            yield Progress("revisions", i, len(ancestors))
612
623
 
613
624
        if target_branch.repository.has_revision(rev_id):
614
 
            target_branch.append_revision(rev_id)
 
625
            target_branch.set_last_revision_info(i+1, rev_id)
615
626
            continue
616
627
        if revdir is None:
617
628
            revdir = os.path.join(tempdir, "rd")
661
672
                    allow_leftmost_as_ghost=True)
662
673
                missing_ancestor = None
663
674
            for merged_rev in direct_merges:
664
 
                target_tree.add_pending_merge(revision_id(merged_rev, 
 
675
                target_tree.add_pending_merge(revision_id(merged_rev,
665
676
                                                          encoding))
666
677
            target_tree.set_root_id(BAZ_IMPORT_ROOT)
 
678
            target_tree.flush()
667
679
            target_tree.set_inventory(baz_inv)
668
680
            commitobj = Commit(reporter=ImportCommitReporter())
669
681
            commitobj.commit(working_tree=target_tree,
680
692
    previous_version = revision.version
681
693
    if pybaz.WorkingTree(revdir).tree_version != previous_version:
682
694
        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, 
 
695
    log_path = "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (revdir,
 
696
        revision.category.nonarch, revision.branch.nonarch,
685
697
        revision.version.nonarch, revision.archive, revision.patchlevel)
686
698
    temp_path = tempfile.mktemp(dir=os.path.dirname(revdir))
687
699
    os.rename(log_path, temp_path)
707
719
    tree = revision.get(revdir)
708
720
    log = get_log(tree, revision)
709
721
    try:
710
 
        return tree, bzr_inventory_data(tree), log 
 
722
        return tree, bzr_inventory_data(tree), log
711
723
    except BadFileKind, e:
712
 
        raise UserError("Cannot convert %s because %s is a %s" % 
 
724
        raise UserError("Cannot convert %s because %s is a %s" %
713
725
                        (revision,e.path, e.kind))
714
726
 
715
727
 
719
731
    try:
720
732
        return bzr_inventory_data(tree), log
721
733
    except BadFileKind, e:
722
 
        raise UserError("Cannot convert %s because %s is a %s" % 
 
734
        raise UserError("Cannot convert %s because %s is a %s" %
723
735
                        (revision,e.path, e.kind))
724
736
 
725
737
 
738
750
    inv_map = {}
739
751
    for arch_id, path in inv_iter:
740
752
        bzr_file_id = map_file_id(arch_id)
741
 
        inv_map[path] = bzr_file_id 
 
753
        inv_map[path] = bzr_file_id
742
754
 
743
755
    bzr_inv = []
744
756
    for path, file_id in inv_map.iteritems():
756
768
    return bzr_inv
757
769
 
758
770
 
759
 
def baz_import_branch(to_location, from_branch, fast, max_count, verbose, 
 
771
def baz_import_branch(to_location, from_branch, fast, max_count, verbose,
760
772
                      encoding, dry_run, reuse_history_list):
761
773
    to_location = os.path.realpath(str(to_location))
762
774
    if from_branch is not None:
767
779
            return 1
768
780
    if reuse_history_list is None:
769
781
        reuse_history_list = []
770
 
    import_version(to_location, from_branch, encoding, max_count=max_count, 
 
782
    import_version(to_location, from_branch, encoding, max_count=max_count,
771
783
                   reuse_history_from=reuse_history_list)
772
784
 
773
785
 
778
790
 
779
791
 
780
792
 
781
 
def baz_import(to_root_dir, from_archive, encoding, verbose=False, 
 
793
def baz_import(to_root_dir, from_archive, encoding, verbose=False,
782
794
               reuse_history_list=[], prefixes=None):
783
795
    if reuse_history_list is None:
784
796
        reuse_history_list = []
825
837
                os.makedirs(os.path.dirname(target))
826
838
            try:
827
839
                import_version(target, version, encoding,
828
 
                               reuse_history_from=reuse_history_from, 
 
840
                               reuse_history_from=reuse_history_from,
829
841
                               standalone=standalone)
830
842
            except pybaz.errors.ExecProblem,e:
831
843
                if str(e).find('The requested revision cannot be built.') != -1: