341
345
###class Importer(object):
342
346
### """An importer.
344
348
### Currently this is used as a parameter object, though more behaviour is
345
349
### possible later.
348
### def __init__(self, output_dir, version, printer, fancy=True, fast=False,
349
### verbose=False, dry_run=False, max_count=None,
352
### def __init__(self, output_dir, version, fast=False,
353
### verbose=False, dry_run=False, max_count=None,
350
354
### reuse_history_from=[]):
351
355
### self.output_dir = output_dir
352
356
### self.version = version
356
def import_version(output_dir, version, printer, fancy=True, fast=False,
360
def import_version(output_dir, version, encoding, fast=False,
357
361
verbose=False, dry_run=False, max_count=None,
358
362
reuse_history_from=[], standalone=True):
360
364
>>> q = test_environ()
366
Progress bars output to stderr, but doctest does not capture that.
368
>>> old_stderr = sys.stderr
369
>>> sys.stderr = sys.stdout
361
371
>>> result_path = os.path.join(q, "result")
362
372
>>> commit_test_revisions()
363
373
>>> version = pybaz.Version("test@example.com/test--test--0.1")
364
>>> def printer(message): print message
365
>>> import_version('/', version, printer, fancy=False, dry_run=True)
374
>>> old_ui = bzrlib.ui.ui_factory
375
>>> bzrlib.ui.ui_factory = bzrlib.ui.text.TextUIFactory(
376
... bar_type=bzrlib.progress.DotsProgressBar)
378
>>> import_version('/', version, None, dry_run=True)
366
379
Traceback (most recent call last):
367
380
NotPreviousImport: / is not the location of a previous import.
368
>>> import_version(result_path, version, printer, fancy=False, dry_run=True)
381
>>> import_version(result_path, version, None, dry_run=True)
369
382
Traceback (most recent call last):
370
383
UserError: The version test@example.com/test--test--0.1 does not exist.
371
384
>>> version = pybaz.Version("test@example.com/test--test--0")
372
>>> import_version(result_path, version, printer, fancy=False, dry_run=True)
385
>>> import_version(result_path, version, None, dry_run=True) #doctest: +ELLIPSIS
386
importing test@example.com/test--test--0 into ...
388
revisions: ..........................................
375
389
Dry run, not modifying output_dir
377
>>> import_version(result_path, version, printer, fancy=False)
391
>>> import_version(result_path, version, None) #doctest: +ELLIPSIS
392
importing test@example.com/test--test--0 into ...
394
revisions: .....................................................................
382
>>> import_version(result_path, version, printer, fancy=False)
397
>>> import_version(result_path, version, None) #doctest: +ELLIPSIS
383
398
Tree is up-to-date with test@example.com/test--test--0--patch-2
384
399
>>> commit_more_test_revisions()
385
>>> import_version(result_path, version, printer, fancy=False)
400
>>> import_version(result_path, version, None) #doctest: +ELLIPSIS
401
importing test@example.com/test--test--0 into ...
402
revisions: ....................................................
405
>>> bzrlib.ui.ui_factory = old_ui
406
>>> sys.stderr = old_stderr
390
407
>>> teardown_environ(q)
393
ancestors, old_revno = get_remaining_revisions(output_dir, version,
395
except NotBranchError, e:
396
raise NotPreviousImport(e.path)
397
if old_revno is None and len(ancestors) == 0:
398
print 'Version %s has no revisions.' % version
400
if len(ancestors) == 0:
401
last_revision = get_last_revision(Branch.open(output_dir))
402
print 'Tree is up-to-date with %s' % last_revision
405
409
progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
406
tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
407
dir=os.path.dirname(output_dir))
409
wt = WorkingTree.open(output_dir)
410
except (NotBranchError, NoWorkingTree):
413
old_basis = EmptyTree()
415
old_basis = wt.basis_tree()
420
for result in iter_import_version(output_dir, ancestors, tempdir,
421
progress_bar, fast=fast, verbose=verbose, dry_run=dry_run,
422
max_count=max_count, standalone=standalone):
424
show_progress(progress_bar, result)
426
sys.stdout.write('.')
429
progress_bar.finished()
431
sys.stdout.write('\n')
434
print 'Dry run, not modifying output_dir'
437
# Update the working tree of the branch
412
ancestors, old_revno = get_remaining_revisions(output_dir, version,
415
except NotBranchError, e:
416
raise NotPreviousImport(e.path)
417
if old_revno is None and len(ancestors) == 0:
418
progress_bar.note('Version %s has no revisions.' % version)
420
if len(ancestors) == 0:
421
last_revision, last_encoding = \
422
get_last_revision(Branch.open(output_dir))
423
progress_bar.note('Tree is up-to-date with %s' % last_revision)
426
progress_bar.note("importing %s into %s" % (version, output_dir))
428
tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
429
dir=os.path.dirname(output_dir))
439
431
wt = WorkingTree.open(output_dir)
440
except NoWorkingTree:
432
except (NotBranchError, NoWorkingTree):
443
wt.set_last_revision(wt.branch.last_revision())
444
merge_inner(wt.branch, wt.basis_tree(), old_basis,
445
ignore_zero=True, this_tree=wt)
435
for result in iter_import_version(output_dir, ancestors, tempdir,
436
pb=progress_bar, encoding=encoding, fast=fast,
437
verbose=verbose, dry_run=dry_run, max_count=max_count,
438
standalone=standalone):
439
show_progress(progress_bar, result)
441
progress_bar.note('Dry run, not modifying output_dir')
444
# Update the working tree of the branch
446
wt = WorkingTree.open(output_dir)
447
except NoWorkingTree:
450
wt.set_last_revision(wt.branch.last_revision())
451
wt.set_root_id(BAZ_IMPORT_ROOT)
456
progress_bar.note('Cleaning up')
457
shutil.rmtree(tempdir)
458
progress_bar.note("Import complete.")
449
printer('Cleaning up')
450
shutil.rmtree(tempdir)
451
printer("Import complete.")
460
progress_bar.finished()
453
462
class UserError(BzrCommandError):
454
463
def __init__(self, message):
455
464
"""Exception to throw when a user makes an impossible request
627
668
if missing_ancestor:
628
669
# if we want it to be in revision-history, do that here.
629
target_tree.add_pending_merge(revision_id(missing_ancestor))
670
target_tree.set_parent_ids(
671
[revision_id(missing_ancestor, encoding)],
672
allow_leftmost_as_ghost=True)
630
673
missing_ancestor = None
631
674
for merged_rev in direct_merges:
632
target_tree.add_pending_merge(revision_id(merged_rev))
675
target_tree.add_pending_merge(revision_id(merged_rev,
677
target_tree.set_root_id(BAZ_IMPORT_ROOT)
633
679
target_tree.set_inventory(baz_inv)
634
commitobj = Commit(reporter=ImportCommitReporter(pb))
680
commitobj = Commit(reporter=ImportCommitReporter())
635
681
commitobj.commit(working_tree=target_tree,
636
message=log_message.decode('ascii', 'replace'),
637
verbose=False, committer=log_creator,
638
timestamp=timestamp, timezone=0, rev_id=rev_id,
682
message=log_message.decode(log_encoding, 'replace'),
683
verbose=False, committer=log_creator, timestamp=timestamp,
684
timezone=0, rev_id=rev_id, revprops={})
641
686
target_tree.unlock()
725
_global_option('max-count', type = int)
726
class cmd_baz_import_branch(Command):
727
"""Import an Arch or Baz branch into a bzr branch. <BZRTOOLS>"""
728
takes_args = ['to_location', 'from_branch?', 'reuse_history*']
729
takes_options = ['verbose', 'max-count']
731
def printer(self, name):
734
def run(self, to_location, from_branch=None, fast=False, max_count=None,
735
verbose=False, dry_run=False, reuse_history_list=[]):
736
to_location = os.path.realpath(str(to_location))
737
if from_branch is not None:
739
from_branch = pybaz.Version(from_branch)
740
except pybaz.errors.NamespaceError:
741
print "%s is not a valid Arch branch." % from_branch
743
if reuse_history_list is None:
744
reuse_history_list = []
745
import_version(to_location, from_branch, self.printer,
747
reuse_history_from=reuse_history_list)
771
def baz_import_branch(to_location, from_branch, fast, max_count, verbose,
772
encoding, dry_run, reuse_history_list):
773
to_location = os.path.realpath(str(to_location))
774
if from_branch is not None:
776
from_branch = pybaz.Version(from_branch)
777
except pybaz.errors.NamespaceError:
778
print "%s is not a valid Arch branch." % from_branch
780
if reuse_history_list is None:
781
reuse_history_list = []
782
import_version(to_location, from_branch, encoding, max_count=max_count,
783
reuse_history_from=reuse_history_list)
750
786
class NotInABranch(Exception):
756
class cmd_baz_import(Command):
757
"""Import an Arch or Baz archive into bzr branches. <BZRTOOLS>
759
This command should be used on local archives (or mirrors) only. It is
760
quite slow on remote archives.
762
reuse_history allows you to specify any previous imports you
763
have done of different archives, which this archive has branches
764
tagged from. This will dramatically reduce the time to convert
765
the archive as it will not have to convert the history already
766
converted in that other branch.
768
If you specify prefixes, only branches whose names start with that prefix
769
will be imported. Skipped branches will be listed, so you can import any
770
branches you missed by accident. Here's an example of doing a partial
771
import from thelove@canonical.com:
772
bzr baz-import thelove thelove@canonical.com --prefixes dists:talloc-except
774
takes_args = ['to_root_dir', 'from_archive', 'reuse_history*']
775
takes_options = ['verbose', Option('prefixes', type=str,
776
help="Prefixes of branches to import, colon-separated")]
778
def printer(self, name):
781
def run(self, to_root_dir, from_archive, verbose=False,
782
reuse_history_list=[], prefixes=None):
783
if reuse_history_list is None:
784
reuse_history_list = []
785
to_root = str(os.path.realpath(to_root_dir))
786
if not os.path.exists(to_root):
788
if prefixes is not None:
789
prefixes = prefixes.split(':')
790
import_archive(to_root, from_archive, verbose, self.printer,
791
reuse_history_list, prefixes=prefixes)
794
def import_archive(to_root, from_archive, verbose, printer,
795
reuse_history_from=[], standalone=False,
793
def baz_import(to_root_dir, from_archive, encoding, verbose=False,
794
reuse_history_list=[], prefixes=None):
795
if reuse_history_list is None:
796
reuse_history_list = []
797
to_root = str(os.path.realpath(to_root_dir))
798
if not os.path.exists(to_root):
800
if prefixes is not None:
801
prefixes = prefixes.split(':')
802
import_archive(to_root, from_archive, verbose, encoding,
803
reuse_history_list, prefixes=prefixes)
806
def import_archive(to_root, from_archive, verbose,
807
encoding, reuse_history_from=[], standalone=False,
797
809
def selected(version):
798
810
if prefixes is None:
813
825
except NoRepositoryPresent:
814
826
raise BzrCommandError("Can't create repository at existing branch.")
815
827
versions = list(pybaz.Archive(str(from_archive)).iter_versions())
816
# progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
828
progress_bar = bzrlib.ui.ui_factory.nested_progress_bar()
818
830
for num, version in enumerate(versions):
819
# progress_bar.update("Branch", num, len(versions))
831
progress_bar.update("Branch", num, len(versions))
820
832
if not selected(version):
821
833
print "Skipping %s" % version
823
835
target = os.path.join(to_root, map_namespace(version))
824
printer("importing %s into %s" % (version, target))
825
836
if not os.path.exists(os.path.dirname(target)):
826
837
os.makedirs(os.path.dirname(target))
828
import_version(target, version, printer,
829
reuse_history_from=reuse_history_from,
839
import_version(target, version, encoding,
840
reuse_history_from=reuse_history_from,
830
841
standalone=standalone)
831
842
except pybaz.errors.ExecProblem,e:
832
843
if str(e).find('The requested revision cannot be built.') != -1:
833
printer("Skipping version %s as it cannot be built due"
834
" to a missing parent archive." % version)
845
"Skipping version %s as it cannot be built due"
846
" to a missing parent archive." % version)
837
849
except UserError, e:
838
850
if str(e).find('already exists, and the last revision ') != -1:
839
printer("Skipping version %s as it has had commits made"
840
" since it was converted to bzr." % version)
852
"Skipping version %s as it has had commits made"
853
" since it was converted to bzr." % version)
845
# progress_bar.finished()
857
progress_bar.finished()
847
860
def map_namespace(a_version):
848
861
a_version = pybaz.Version("%s" % a_version)