~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/repository.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
    osutils,
31
31
    pack,
32
32
    ui,
33
 
    versionedfile,
34
33
    )
35
34
from bzrlib.bzrdir import BzrDir
36
35
from bzrlib.smart.request import (
82
81
            recreate_search trusts that clients will look for missing things
83
82
            they expected and get it from elsewhere.
84
83
        """
 
84
        if search_bytes == 'everything':
 
85
            return graph.EverythingResult(repository), None
85
86
        lines = search_bytes.split('\n')
86
87
        if lines[0] == 'ancestry-of':
87
88
            heads = lines[1:]
392
393
        if token == '':
393
394
            token = None
394
395
        try:
395
 
            token = repository.lock_write(token=token)
 
396
            token = repository.lock_write(token=token).repository_token
396
397
        except errors.LockContention, e:
397
398
            return FailedSmartServerResponse(('LockContention',))
398
399
        except errors.UnlockableTransport:
413
414
    def do_repository_request(self, repository, to_network_name):
414
415
        """Get a stream for inserting into a to_format repository.
415
416
 
 
417
        The request body is 'search_bytes', a description of the revisions
 
418
        being requested.
 
419
 
 
420
        In 2.3 this verb added support for search_bytes == 'everything'.  Older
 
421
        implementations will respond with a BadSearch error, and clients should
 
422
        catch this and fallback appropriately.
 
423
 
416
424
        :param repository: The repository to stream from.
417
425
        :param to_network_name: The network name of the format of the target
418
426
            repository.
490
498
 
491
499
 
492
500
class SmartServerRepositoryGetStream_1_19(SmartServerRepositoryGetStream):
 
501
    """The same as Repository.get_stream, but will return stream CHK formats to
 
502
    clients.
 
503
 
 
504
    See SmartServerRepositoryGetStream._should_fake_unknown.
 
505
    
 
506
    New in 1.19.
 
507
    """
493
508
 
494
509
    def _should_fake_unknown(self):
495
510
        """Returns False; we don't need to workaround bugs in 1.19+ clients."""
505
520
        for record in substream:
506
521
            if record.storage_kind in ('chunked', 'fulltext'):
507
522
                serialised = record_to_fulltext_bytes(record)
508
 
            elif record.storage_kind == 'inventory-delta':
509
 
                serialised = record_to_inventory_delta_bytes(record)
510
523
            elif record.storage_kind == 'absent':
511
524
                raise ValueError("Absent factory for %s" % (record.key,))
512
525
            else:
544
557
    :ivar first_bytes: The first bytes to give the next NetworkRecordStream.
545
558
    """
546
559
 
547
 
    def __init__(self, byte_stream):
 
560
    def __init__(self, byte_stream, record_counter):
548
561
        """Create a _ByteStreamDecoder."""
549
562
        self.stream_decoder = pack.ContainerPushParser()
550
563
        self.current_type = None
551
564
        self.first_bytes = None
552
565
        self.byte_stream = byte_stream
 
566
        self._record_counter = record_counter
 
567
        self.key_count = 0
553
568
 
554
569
    def iter_stream_decoder(self):
555
570
        """Iterate the contents of the pack from stream_decoder."""
580
595
 
581
596
    def record_stream(self):
582
597
        """Yield substream_type, substream from the byte stream."""
 
598
        def wrap_and_count(pb, rc, substream):
 
599
            """Yield records from stream while showing progress."""
 
600
            counter = 0
 
601
            if rc:
 
602
                if self.current_type != 'revisions' and self.key_count != 0:
 
603
                    # As we know the number of revisions now (in self.key_count)
 
604
                    # we can setup and use record_counter (rc).
 
605
                    if not rc.is_initialized():
 
606
                        rc.setup(self.key_count, self.key_count)
 
607
            for record in substream.read():
 
608
                if rc:
 
609
                    if rc.is_initialized() and counter == rc.STEP:
 
610
                        rc.increment(counter)
 
611
                        pb.update('Estimate', rc.current, rc.max)
 
612
                        counter = 0
 
613
                    if self.current_type == 'revisions':
 
614
                        # Total records is proportional to number of revs
 
615
                        # to fetch. With remote, we used self.key_count to
 
616
                        # track the number of revs. Once we have the revs
 
617
                        # counts in self.key_count, the progress bar changes
 
618
                        # from 'Estimating..' to 'Estimate' above.
 
619
                        self.key_count += 1
 
620
                        if counter == rc.STEP:
 
621
                            pb.update('Estimating..', self.key_count)
 
622
                            counter = 0
 
623
                counter += 1
 
624
                yield record
 
625
 
583
626
        self.seed_state()
 
627
        pb = ui.ui_factory.nested_progress_bar()
 
628
        rc = self._record_counter
584
629
        # Make and consume sub generators, one per substream type:
585
630
        while self.first_bytes is not None:
586
631
            substream = NetworkRecordStream(self.iter_substream_bytes())
587
632
            # after substream is fully consumed, self.current_type is set to
588
633
            # the next type, and self.first_bytes is set to the matching bytes.
589
 
            yield self.current_type, substream.read()
 
634
            yield self.current_type, wrap_and_count(pb, rc, substream)
 
635
        if rc:
 
636
            pb.update('Done', rc.max, rc.max)
 
637
        pb.finished()
590
638
 
591
639
    def seed_state(self):
592
640
        """Prepare the _ByteStreamDecoder to decode from the pack stream."""
597
645
        list(self.iter_substream_bytes())
598
646
 
599
647
 
600
 
def _byte_stream_to_stream(byte_stream):
 
648
def _byte_stream_to_stream(byte_stream, record_counter=None):
601
649
    """Convert a byte stream into a format and a stream.
602
650
 
603
651
    :param byte_stream: A bytes iterator, as output by _stream_to_byte_stream.
604
652
    :return: (RepositoryFormat, stream_generator)
605
653
    """
606
 
    decoder = _ByteStreamDecoder(byte_stream)
 
654
    decoder = _ByteStreamDecoder(byte_stream, record_counter)
607
655
    for bytes in byte_stream:
608
656
        decoder.stream_decoder.accept_bytes(bytes)
609
657
        for record in decoder.stream_decoder.read_pending_records(max=1):