~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2005-12-14 14:33:05 UTC
  • Revision ID: abentley@panoramicfeedback.com-20051214143305-42718d97f27c03bd
Avoided leaving junk all over the place when running standalone tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
from bzrlib import Branch
 
16
from bzrlib.branch import Branch
17
17
from bzrlib.commands import Command
 
18
from errors import NoPyBaz
18
19
try:
19
20
    import pybaz
20
21
    import pybaz.errors
 
22
    from pybaz.backends.baz import null_cmd
21
23
except ImportError:
22
 
    print "This command requires PyBaz.  Please ensure that it is installed."
23
 
    import sys
24
 
    sys.exit(1)
25
 
from pybaz.backends.baz import null_cmd
 
24
    raise NoPyBaz
26
25
import tempfile
27
26
import os
28
27
import os.path
29
28
import shutil
30
29
import bzrlib
31
 
from bzrlib.errors import BzrError
 
30
from bzrlib.errors import BzrError, NotBranchError, BzrCommandError
32
31
import bzrlib.trace
33
32
import bzrlib.merge
 
33
import bzrlib.inventory
 
34
import bzrlib.osutils
34
35
import sys
35
36
import email.Utils
36
37
from progress import *
205
206
    return ancestors
206
207
 
207
208
def get_last_revision(branch):
208
 
    last_patch = branch.last_patch()
 
209
    last_patch = branch.last_revision()
209
210
    try:
210
211
        return arch_revision(last_patch)
211
212
    except NotArchRevision:
222
223
        # the current version is
223
224
        branch = find_branch(output_dir)
224
225
        last_patch = get_last_revision(branch)
 
226
        if last_patch is None:
 
227
            raise NotPreviousImport(branch.base)
225
228
        if version is None:
226
229
            version = last_patch.version
227
230
    elif version is None:
229
232
 
230
233
    try:
231
234
        ancestors = version_ancestry(version)
 
235
        if len(ancestors) > 0 and not ancestors[0].archive.is_registered():
 
236
            ancestors = ancestors[1:]
232
237
    except NoSuchVersion, e:
233
238
        raise UserError(e)
234
239
 
284
289
    try:
285
290
        ancestors, old_revno = get_remaining_revisions(output_dir, version)
286
291
    except NotInABranch, e:
287
 
        raise UserError("%s exists, but is not a bzr branch." % e.path)
 
292
        raise NotPreviousImport(e.path)
288
293
    if len(ancestors) == 0:
289
294
        last_revision = get_last_revision(find_branch(output_dir))
290
295
        print 'Tree is up-to-date with %s' % last_revision
301
306
                    fast=fast, verbose=verbose, dry_run=dry_run, 
302
307
                    max_count=max_count, skip_symlinks=skip_symlinks):
303
308
                if fancy:
304
 
                    progress_bar(result)
 
309
                    show_progress(progress_bar, result)
305
310
                else:
306
311
                    sys.stdout.write('.')
307
312
        finally:
308
313
            if fancy:
309
 
                clear_progress_bar()
 
314
                progress_bar.clear()
310
315
            else:
311
316
                sys.stdout.write('\n')
312
317
 
340
345
        shutil.rmtree(tempdir)
341
346
    print "Import complete."
342
347
            
343
 
class UserError(Exception):
 
348
class UserError(BzrCommandError):
344
349
    def __init__(self, message):
345
350
        """Exception to throw when a user makes an impossible request
346
351
        :param message: The message to emit when printing this exception
347
352
        :type message: string
348
353
        """
349
 
        Exception.__init__(self, message)
 
354
        BzrCommandError.__init__(self, message)
 
355
 
 
356
class NotPreviousImport(UserError):
 
357
    def __init__(self, path):
 
358
        UserError.__init__(self, "%s is not the location of a previous import."
 
359
                           % path)
 
360
 
350
361
 
351
362
def revision_id(arch_revision):
352
363
    """
436
447
                # next revision of the baz tree
437
448
                branch = find_branch(revdir)
438
449
            else:
439
 
                branch = bzrlib.Branch(revdir, init=True)
 
450
                branch = Branch.initialize(revdir)
440
451
        else:
441
452
            old = os.path.join(revdir, ".bzr")
442
453
            new = os.path.join(tempdir, ".bzr")
449
460
        rev_id = revision_id(revision)
450
461
        branch.lock_write()
451
462
        try:
452
 
            branch.set_inventory(baz_inv)
 
463
            branch.working_tree().set_inventory(baz_inv)
453
464
            bzrlib.trace.silent = True
454
 
            branch.commit(log.summary, verbose=False, committer=log.creator,
455
 
                          timestamp=timestamp, timezone=0, rev_id=rev_id)
 
465
            wt = branch.working_tree()
 
466
            wt.commit(log.summary, verbose=False, committer=log.creator,
 
467
                      timestamp=timestamp, timezone=0, rev_id=rev_id)
456
468
        finally:
457
469
            bzrlib.trace.silent = False   
458
470
            branch.unlock()
469
481
 
470
482
def get_log(tree, revision):
471
483
    log = tree.iter_logs(version=revision.version, reverse=True).next()
472
 
    assert log.revision == revision
 
484
    assert str(log.revision) == str(revision), (log.revision, revision)
473
485
    return log
474
486
 
475
487
def get_revision(revdir, revision, skip_symlinks=False):
506
518
def bzr_inventory_data(tree, skip_symlinks=False):
507
519
    inv_iter = tree.iter_inventory_ids(source=True, both=True)
508
520
    inv_map = {}
509
 
    for file_id, path in inv_iter:
510
 
        inv_map[path] = file_id 
 
521
    for arch_id, path in inv_iter:
 
522
        bzr_file_id = arch_id.replace('%', '%25').replace('/', '%2f')
 
523
        inv_map[path] = bzr_file_id 
511
524
 
512
525
    bzr_inv = []
513
526
    for path, file_id in inv_map.iteritems():
538
551
    Traceback (most recent call last):
539
552
    NotInABranch: / is not in a branch.
540
553
    >>> sb = bzrlib.ScratchBranch()
541
 
    >>> isinstance(find_branch(sb.base), bzrlib.Branch)
 
554
    >>> isinstance(find_branch(sb.base), Branch)
542
555
    True
543
556
    """
544
557
    try:
545
 
        return bzrlib.Branch(path)
546
 
    except BzrError, e:
547
 
        if e.args[0].endswith("' is not in a branch"):
548
 
            raise NotInABranch(path)
 
558
        return Branch.open(path)
 
559
    except NotBranchError, e:
 
560
        raise NotInABranch(path)
549
561
 
550
562
class cmd_baz_import(Command):
551
563
    """Import an Arch or Baz branch into a bzr branch"""
554
566
 
555
567
    def run(self, to_location, from_branch=None, skip_symlinks=False, 
556
568
            fast=False, max_count=None, verbose=False, dry_run=False):
557
 
        fancy = rewriting_supported()
558
569
        to_location = os.path.realpath(str(to_location))
559
570
        if from_branch is not None:
560
571
            try:
561
 
                from_branch = pybaz.Version(version)
 
572
                from_branch = pybaz.Version(from_branch)
562
573
            except pybaz.errors.NamespaceError:
563
574
                print "%s is not a valid Arch branch." % from_branch
564
575
                return 1
565
 
        import_version(to_location, from_branch, fancy=fancy)
 
576
        import_version(to_location, from_branch)