~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz_import.py

  • Committer: Aaron Bentley
  • Date: 2005-11-11 17:43:12 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051111174312-1c627d82a07bf8fd
Added patch for tab-in-patch-filename support

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
465
            branch.commit(log.summary, verbose=False, committer=log.creator,
455
466
                          timestamp=timestamp, timezone=0, rev_id=rev_id)
469
480
 
470
481
def get_log(tree, revision):
471
482
    log = tree.iter_logs(version=revision.version, reverse=True).next()
472
 
    assert log.revision == revision
 
483
    assert str(log.revision) == str(revision), (log.revision, revision)
473
484
    return log
474
485
 
475
486
def get_revision(revdir, revision, skip_symlinks=False):
506
517
def bzr_inventory_data(tree, skip_symlinks=False):
507
518
    inv_iter = tree.iter_inventory_ids(source=True, both=True)
508
519
    inv_map = {}
509
 
    for file_id, path in inv_iter:
510
 
        inv_map[path] = file_id 
 
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 
511
523
 
512
524
    bzr_inv = []
513
525
    for path, file_id in inv_map.iteritems():
538
550
    Traceback (most recent call last):
539
551
    NotInABranch: / is not in a branch.
540
552
    >>> sb = bzrlib.ScratchBranch()
541
 
    >>> isinstance(find_branch(sb.base), bzrlib.Branch)
 
553
    >>> isinstance(find_branch(sb.base), Branch)
542
554
    True
543
555
    """
544
556
    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)
 
557
        return Branch.open(path)
 
558
    except NotBranchError, e:
 
559
        raise NotInABranch(path)
549
560
 
550
561
class cmd_baz_import(Command):
551
562
    """Import an Arch or Baz branch into a bzr branch"""
554
565
 
555
566
    def run(self, to_location, from_branch=None, skip_symlinks=False, 
556
567
            fast=False, max_count=None, verbose=False, dry_run=False):
557
 
        fancy = rewriting_supported()
558
568
        to_location = os.path.realpath(str(to_location))
559
569
        if from_branch is not None:
560
570
            try:
561
 
                from_branch = pybaz.Version(version)
 
571
                from_branch = pybaz.Version(from_branch)
562
572
            except pybaz.errors.NamespaceError:
563
573
                print "%s is not a valid Arch branch." % from_branch
564
574
                return 1
565
 
        import_version(to_location, from_branch, fancy=fancy)
 
575
        import_version(to_location, from_branch)