~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-06-10 17:55:08 UTC
  • mfrom: (531.2.2 bzrtools)
  • Revision ID: aaron.bentley@utoronto.ca-20070610175508-gex1oxvmfv0qoagi
Merge whitespace cleanups

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
568
568
    return wt
569
569
 
570
570
 
571
 
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding, 
 
571
def iter_import_version(output_dir, ancestors, tempdir, pb, encoding,
572
572
                        fast=False, verbose=False, dry_run=False,
573
573
                        max_count=None, standalone=False):
574
574
    revdir = None
671
671
                    allow_leftmost_as_ghost=True)
672
672
                missing_ancestor = None
673
673
            for merged_rev in direct_merges:
674
 
                target_tree.add_pending_merge(revision_id(merged_rev, 
 
674
                target_tree.add_pending_merge(revision_id(merged_rev,
675
675
                                                          encoding))
676
676
            target_tree.set_root_id(BAZ_IMPORT_ROOT)
677
677
            target_tree.flush()
691
691
    previous_version = revision.version
692
692
    if pybaz.WorkingTree(revdir).tree_version != previous_version:
693
693
        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, 
 
694
    log_path = "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (revdir,
 
695
        revision.category.nonarch, revision.branch.nonarch,
696
696
        revision.version.nonarch, revision.archive, revision.patchlevel)
697
697
    temp_path = tempfile.mktemp(dir=os.path.dirname(revdir))
698
698
    os.rename(log_path, temp_path)
718
718
    tree = revision.get(revdir)
719
719
    log = get_log(tree, revision)
720
720
    try:
721
 
        return tree, bzr_inventory_data(tree), log 
 
721
        return tree, bzr_inventory_data(tree), log
722
722
    except BadFileKind, e:
723
 
        raise UserError("Cannot convert %s because %s is a %s" % 
 
723
        raise UserError("Cannot convert %s because %s is a %s" %
724
724
                        (revision,e.path, e.kind))
725
725
 
726
726
 
730
730
    try:
731
731
        return bzr_inventory_data(tree), log
732
732
    except BadFileKind, e:
733
 
        raise UserError("Cannot convert %s because %s is a %s" % 
 
733
        raise UserError("Cannot convert %s because %s is a %s" %
734
734
                        (revision,e.path, e.kind))
735
735
 
736
736
 
749
749
    inv_map = {}
750
750
    for arch_id, path in inv_iter:
751
751
        bzr_file_id = map_file_id(arch_id)
752
 
        inv_map[path] = bzr_file_id 
 
752
        inv_map[path] = bzr_file_id
753
753
 
754
754
    bzr_inv = []
755
755
    for path, file_id in inv_map.iteritems():
767
767
    return bzr_inv
768
768
 
769
769
 
770
 
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,
771
771
                      encoding, dry_run, reuse_history_list):
772
772
    to_location = os.path.realpath(str(to_location))
773
773
    if from_branch is not None:
778
778
            return 1
779
779
    if reuse_history_list is None:
780
780
        reuse_history_list = []
781
 
    import_version(to_location, from_branch, encoding, max_count=max_count, 
 
781
    import_version(to_location, from_branch, encoding, max_count=max_count,
782
782
                   reuse_history_from=reuse_history_list)
783
783
 
784
784
 
789
789
 
790
790
 
791
791
 
792
 
def baz_import(to_root_dir, from_archive, encoding, verbose=False, 
 
792
def baz_import(to_root_dir, from_archive, encoding, verbose=False,
793
793
               reuse_history_list=[], prefixes=None):
794
794
    if reuse_history_list is None:
795
795
        reuse_history_list = []
836
836
                os.makedirs(os.path.dirname(target))
837
837
            try:
838
838
                import_version(target, version, encoding,
839
 
                               reuse_history_from=reuse_history_from, 
 
839
                               reuse_history_from=reuse_history_from,
840
840
                               standalone=standalone)
841
841
            except pybaz.errors.ExecProblem,e:
842
842
                if str(e).find('The requested revision cannot be built.') != -1: