~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2005-07-25 14:17:15 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050725141715-d410836f6e4a32c0
Got baz2bzr/annotate working now that ProgressBar is a function

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.branch import Branch
 
16
from bzrlib import Branch
17
17
from bzrlib.commands import Command
18
 
from errors import NoPyBaz
 
18
 
 
19
class NoPyBaz(Exception):
 
20
    def __init__(self):
 
21
        Exception.__init__(self, "PyBaz is not installed.")
19
22
try:
20
23
    import pybaz
21
24
    import pybaz.errors
22
 
    from pybaz.backends.baz import null_cmd
23
25
except ImportError:
24
26
    raise NoPyBaz
 
27
from pybaz.backends.baz import null_cmd
25
28
import tempfile
26
29
import os
27
30
import os.path
28
31
import shutil
29
32
import bzrlib
30
 
from bzrlib.errors import BzrError, NotBranchError, BzrCommandError
 
33
from bzrlib.errors import BzrError
31
34
import bzrlib.trace
32
35
import bzrlib.merge
33
36
import bzrlib.inventory
206
209
    return ancestors
207
210
 
208
211
def get_last_revision(branch):
209
 
    last_patch = branch.last_revision()
 
212
    last_patch = branch.last_patch()
210
213
    try:
211
214
        return arch_revision(last_patch)
212
215
    except NotArchRevision:
223
226
        # the current version is
224
227
        branch = find_branch(output_dir)
225
228
        last_patch = get_last_revision(branch)
226
 
        if last_patch is None:
227
 
            raise NotPreviousImport(branch.base)
228
229
        if version is None:
229
230
            version = last_patch.version
230
231
    elif version is None:
232
233
 
233
234
    try:
234
235
        ancestors = version_ancestry(version)
235
 
        if len(ancestors) > 0 and not ancestors[0].archive.is_registered():
236
 
            ancestors = ancestors[1:]
237
236
    except NoSuchVersion, e:
238
237
        raise UserError(e)
239
238
 
289
288
    try:
290
289
        ancestors, old_revno = get_remaining_revisions(output_dir, version)
291
290
    except NotInABranch, e:
292
 
        raise NotPreviousImport(e.path)
 
291
        raise UserError("%s exists, but is not a bzr branch." % e.path)
293
292
    if len(ancestors) == 0:
294
293
        last_revision = get_last_revision(find_branch(output_dir))
295
294
        print 'Tree is up-to-date with %s' % last_revision
345
344
        shutil.rmtree(tempdir)
346
345
    print "Import complete."
347
346
            
348
 
class UserError(BzrCommandError):
 
347
class UserError(Exception):
349
348
    def __init__(self, message):
350
349
        """Exception to throw when a user makes an impossible request
351
350
        :param message: The message to emit when printing this exception
352
351
        :type message: string
353
352
        """
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
 
 
 
353
        Exception.__init__(self, message)
361
354
 
362
355
def revision_id(arch_revision):
363
356
    """
447
440
                # next revision of the baz tree
448
441
                branch = find_branch(revdir)
449
442
            else:
450
 
                branch = Branch.initialize(revdir)
 
443
                branch = bzrlib.Branch(revdir, init=True)
451
444
        else:
452
445
            old = os.path.join(revdir, ".bzr")
453
446
            new = os.path.join(tempdir, ".bzr")
460
453
        rev_id = revision_id(revision)
461
454
        branch.lock_write()
462
455
        try:
463
 
            branch.working_tree().set_inventory(baz_inv)
 
456
            branch.set_inventory(baz_inv)
464
457
            bzrlib.trace.silent = True
465
458
            branch.commit(log.summary, verbose=False, committer=log.creator,
466
459
                          timestamp=timestamp, timezone=0, rev_id=rev_id)
480
473
 
481
474
def get_log(tree, revision):
482
475
    log = tree.iter_logs(version=revision.version, reverse=True).next()
483
 
    assert str(log.revision) == str(revision), (log.revision, revision)
 
476
    assert log.revision == revision
484
477
    return log
485
478
 
486
479
def get_revision(revdir, revision, skip_symlinks=False):
517
510
def bzr_inventory_data(tree, skip_symlinks=False):
518
511
    inv_iter = tree.iter_inventory_ids(source=True, both=True)
519
512
    inv_map = {}
520
 
    for arch_id, path in inv_iter:
521
 
        bzr_file_id = arch_id.replace('%', '%25').replace('/', '%2f')
522
 
        inv_map[path] = bzr_file_id 
 
513
    for file_id, path in inv_iter:
 
514
        inv_map[path] = file_id 
523
515
 
524
516
    bzr_inv = []
525
517
    for path, file_id in inv_map.iteritems():
550
542
    Traceback (most recent call last):
551
543
    NotInABranch: / is not in a branch.
552
544
    >>> sb = bzrlib.ScratchBranch()
553
 
    >>> isinstance(find_branch(sb.base), Branch)
 
545
    >>> isinstance(find_branch(sb.base), bzrlib.Branch)
554
546
    True
555
547
    """
556
548
    try:
557
 
        return Branch.open(path)
558
 
    except NotBranchError, e:
559
 
        raise NotInABranch(path)
 
549
        return bzrlib.Branch(path)
 
550
    except BzrError, e:
 
551
        if e.args[0].endswith("' is not in a branch"):
 
552
            raise NotInABranch(path)
560
553
 
561
554
class cmd_baz_import(Command):
562
555
    """Import an Arch or Baz branch into a bzr branch"""