~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-01-08 17:27:48 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070108172748-1b22qtszaadoby89
Improve bzr import docs

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