~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-05 16:55:12 UTC
  • mfrom: (4906.1.10 2.1.0rc1-dbytes)
  • Revision ID: pqm@pqm.ubuntu.com-20100105165512-ip2jyuy8f999raxm
(jam) Add -Dbytes and log how many bytes are transferred for
        operations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    progress,
33
33
    osutils,
34
34
    symbol_versioning,
 
35
    trace,
35
36
    )
36
37
 
37
38
""")
203
204
        self._progress_view.show_transport_activity(transport,
204
205
            direction, byte_count)
205
206
 
 
207
    def log_transport_activity(self, display=False):
 
208
        """See UIFactory.log_transport_activity()"""
 
209
        log = getattr(self._progress_view, 'log_transport_activity', None)
 
210
        if log is not None:
 
211
            log(display=display)
 
212
 
206
213
    def show_error(self, msg):
207
214
        self.clear_term()
208
215
        self.stderr.write("bzr: error: %s\n" % msg)
257
264
        self._last_task = None
258
265
        self._total_byte_count = 0
259
266
        self._bytes_since_update = 0
 
267
        self._bytes_by_direction = {'unknown': 0, 'read': 0, 'write': 0}
 
268
        self._first_byte_time = None
260
269
        self._fraction = 0
261
270
        # force the progress bar to be off, as at the moment it doesn't 
262
271
        # correspond reliably to overall command progress
369
378
        This may update a progress bar, spinner, or similar display.
370
379
        By default it does nothing.
371
380
        """
372
 
        # XXX: Probably there should be a transport activity model, and that
373
 
        # too should be seen by the progress view, rather than being poked in
374
 
        # here.
 
381
        # XXX: there should be a transport activity model, and that too should
 
382
        #      be seen by the progress view, rather than being poked in here.
 
383
        self._total_byte_count += byte_count
 
384
        self._bytes_since_update += byte_count
 
385
        if self._first_byte_time is None:
 
386
            # Note that this isn't great, as technically it should be the time
 
387
            # when the bytes started transferring, not when they completed.
 
388
            # However, we usually start with a small request anyway.
 
389
            self._first_byte_time = time.time()
 
390
        if direction in self._bytes_by_direction:
 
391
            self._bytes_by_direction[direction] += byte_count
 
392
        else:
 
393
            self._bytes_by_direction['unknown'] += byte_count
375
394
        if 'no_activity' in debug.debug_flags:
376
395
            # Can be used as a workaround if
377
396
            # <https://launchpad.net/bugs/321935> reappears and transport
378
397
            # activity is cluttering other output.  However, thanks to
379
398
            # TextUIOutputStream this shouldn't be a problem any more.
380
399
            return
381
 
        self._total_byte_count += byte_count
382
 
        self._bytes_since_update += byte_count
383
400
        now = time.time()
384
401
        if self._total_byte_count < 2000:
385
402
            # a little resistance at first, so it doesn't stay stuck at 0
399
416
            self._last_transport_msg = msg
400
417
            self._repaint()
401
418
 
 
419
    def _format_bytes_by_direction(self):
 
420
        if self._first_byte_time is None:
 
421
            bps = 0.0
 
422
        else:
 
423
            transfer_time = time.time() - self._first_byte_time
 
424
            if transfer_time < 0.001:
 
425
                transfer_time = 0.001
 
426
            bps = self._total_byte_count / transfer_time
 
427
 
 
428
        msg = ('Transferred: %.0fKiB'
 
429
               ' (%.1fK/s r:%.0fK w:%.0fK'
 
430
               % (self._total_byte_count / 1024.,
 
431
                  bps / 1024.,
 
432
                  self._bytes_by_direction['read'] / 1024.,
 
433
                  self._bytes_by_direction['write'] / 1024.,
 
434
                 ))
 
435
        if self._bytes_by_direction['unknown'] > 0:
 
436
            msg += ' u:%.0fK)' % (
 
437
                self._bytes_by_direction['unknown'] / 1024.
 
438
                )
 
439
        else:
 
440
            msg += ')'
 
441
        return msg
 
442
 
 
443
    def log_transport_activity(self, display=False):
 
444
        msg = self._format_bytes_by_direction()
 
445
        trace.mutter(msg)
 
446
        if display and self._total_byte_count > 0:
 
447
            self.clear()
 
448
            self._term_file.write(msg + '\n')
 
449
 
402
450
 
403
451
class TextUIOutputStream(object):
404
452
    """Decorates an output stream so that the terminal is cleared before writing.