/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

Add finished() notifications to transactions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
 
45
45
import bzrlib.errors as errors
46
 
from bzrlib.trace import mutter 
47
46
 
48
47
 
49
48
def _supports_progress(f):
72
71
    def __init__(self,
73
72
                 to_file=sys.stderr,
74
73
                 show_pct=False,
75
 
                 show_spinner=True,
76
 
                 show_eta=False,
 
74
                 show_spinner=False,
 
75
                 show_eta=True,
77
76
                 show_bar=True,
78
77
                 show_count=True,
79
78
                 to_messages_file=sys.stdout,
89
88
        self._stack = []
90
89
        self._klass = klass or TTYProgressBar
91
90
 
92
 
    def top(self):
93
 
        if len(self._stack) != 0:
94
 
            return self._stack[-1]
95
 
        else:
96
 
            return None
97
 
 
98
 
    def bottom(self):
99
 
        if len(self._stack) != 0:
100
 
            return self._stack[0]
101
 
        else:
102
 
            return None
103
 
 
104
91
    def get_nested(self):
105
92
        """Return a nested progress bar."""
106
 
        if len(self._stack) == 0:
107
 
            func = self._klass
108
 
        else:
109
 
            func = self.top().child_progress
110
 
        new_bar = func(to_file=self._to_file,
111
 
                       show_pct=self._show_pct,
112
 
                       show_spinner=self._show_spinner,
113
 
                       show_eta=self._show_eta,
114
 
                       show_bar=self._show_bar,
115
 
                       show_count=self._show_count,
116
 
                       to_messages_file=self._to_messages_file,
117
 
                       _stack=self)
 
93
        # initial implementation - return a new bar each time.
 
94
        new_bar = self._klass(to_file=self._to_file,
 
95
                              show_pct=self._show_pct,
 
96
                              show_spinner=self._show_spinner,
 
97
                              show_eta=self._show_eta,
 
98
                              show_bar=self._show_bar,
 
99
                              show_count=self._show_count,
 
100
                              to_messages_file=self._to_messages_file,
 
101
                              _stack=self)
118
102
        self._stack.append(new_bar)
119
103
        return new_bar
120
104
 
148
132
        self.show_bar = show_bar
149
133
        self.show_count = show_count
150
134
        self._stack = _stack
151
 
        # seed throttler
152
 
        self.MIN_PAUSE = 0.1 # seconds
153
 
        now = time.clock()
154
 
        # starting now
155
 
        self.start_time = now
156
 
        # next update should not throttle
157
 
        self.last_update = now - self.MIN_PAUSE - 1
158
135
 
159
136
    def finished(self):
160
137
        """Return this bar to its progress stack."""
168
145
        self.to_messages_file.write(fmt_string % args)
169
146
        self.to_messages_file.write('\n')
170
147
 
171
 
    def child_progress(self, **kwargs):
172
 
        return ChildProgress(**kwargs)
173
 
 
174
148
 
175
149
class DummyProgress(_BaseProgressBar):
176
150
    """Progress-bar standin that does nothing.
183
157
    def update(self, msg=None, current=None, total=None):
184
158
        pass
185
159
 
186
 
    def child_update(self, message, current, total):
187
 
        pass
188
 
 
189
160
    def clear(self):
190
161
        pass
191
162
        
192
163
    def note(self, fmt_string, *args, **kwargs):
193
164
        """See _BaseProgressBar.note()."""
194
165
 
195
 
    def child_progress(self, **kwargs):
196
 
        return DummyProgress(**kwargs)
197
166
 
198
167
class DotsProgressBar(_BaseProgressBar):
199
168
 
219
188
        if self.need_nl:
220
189
            self.to_file.write('\n')
221
190
        
222
 
    def child_update(self, message, current, total):
223
 
        self.tick()
224
191
    
225
192
class TTYProgressBar(_BaseProgressBar):
226
193
    """Progress bar display object.
243
210
    The output file should be in line-buffered or unbuffered mode.
244
211
    """
245
212
    SPIN_CHARS = r'/-\|'
 
213
    MIN_PAUSE = 0.1 # seconds
246
214
 
247
215
 
248
216
    def __init__(self, **kwargs):
251
219
        self.spin_pos = 0
252
220
        self.width = terminal_width()
253
221
        self.start_time = None
 
222
        self.last_update = None
254
223
        self.last_updates = deque()
255
 
        self.child_fraction = 0
256
224
    
257
225
 
258
226
    def throttle(self):
259
227
        """Return True if the bar was updated too recently"""
260
 
        # time.time consistently takes 40/4000 ms = 0.01 ms.
261
 
        # but every single update to the pb invokes it.
262
 
        # so we use time.clock which takes 20/4000 ms = 0.005ms
263
 
        # on the downside, time.clock() appears to have approximately
264
 
        # 10ms granularity, so we treat a zero-time change as 'throttled.'
265
 
        
266
 
        now = time.clock()
267
 
        interval = now - self.last_update
268
 
        # if interval > 0
269
 
        if interval < self.MIN_PAUSE:
270
 
            return True
 
228
        now = time.time()
 
229
        if self.start_time is None:
 
230
            self.start_time = self.last_update = now
 
231
            return False
 
232
        else:
 
233
            interval = now - self.last_update
 
234
            if interval > 0 and interval < self.MIN_PAUSE:
 
235
                return True
271
236
 
272
237
        self.last_updates.append(now - self.last_update)
273
238
        self.last_update = now
275
240
        
276
241
 
277
242
    def tick(self):
278
 
        self.update(self.last_msg, self.last_cnt, self.last_total, 
279
 
                    self.child_fraction)
280
 
 
281
 
    def child_update(self, message, current, total):
282
 
        if current is not None and total != 0:
283
 
            child_fraction = float(current) / total
284
 
            if self.last_cnt is None:
285
 
                pass
286
 
            elif self.last_cnt + child_fraction <= self.last_total:
287
 
                self.child_fraction = child_fraction
288
 
            else:
289
 
                mutter('not updating child fraction')
290
 
        if self.last_msg is None:
291
 
            self.last_msg = ''
292
 
        self.tick()
293
 
 
294
 
 
295
 
    def update(self, msg, current_cnt=None, total_cnt=None, 
296
 
               child_fraction=0):
 
243
        self.update(self.last_msg, self.last_cnt, self.last_total)
 
244
                 
 
245
 
 
246
 
 
247
    def update(self, msg, current_cnt=None, total_cnt=None):
297
248
        """Update and redraw progress bar."""
298
249
 
299
250
        if current_cnt < 0:
302
253
        if current_cnt > total_cnt:
303
254
            total_cnt = current_cnt
304
255
        
305
 
        ## # optional corner case optimisation 
306
 
        ## # currently does not seem to fire so costs more than saved.
307
 
        ## # trivial optimal case:
308
 
        ## # NB if callers are doing a clear and restore with
309
 
        ## # the saved values, this will prevent that:
310
 
        ## # in that case add a restore method that calls
311
 
        ## # _do_update or some such
312
 
        ## if (self.last_msg == msg and
313
 
        ##     self.last_cnt == current_cnt and
314
 
        ##     self.last_total == total_cnt and
315
 
        ##     self.child_fraction == child_fraction):
316
 
        ##     return
317
 
 
318
256
        old_msg = self.last_msg
319
257
        # save these for the tick() function
320
258
        self.last_msg = msg
321
259
        self.last_cnt = current_cnt
322
260
        self.last_total = total_cnt
323
 
        self.child_fraction = child_fraction
324
 
 
325
 
        # each function call takes 20ms/4000 = 0.005 ms, 
326
 
        # but multiple that by 4000 calls -> starts to cost.
327
 
        # so anything to make this function call faster
328
 
        # will improve base 'diff' time by up to 0.1 seconds.
 
261
            
329
262
        if old_msg == self.last_msg and self.throttle():
330
 
            return
331
 
 
332
 
        if self.show_eta and self.start_time and self.last_total:
333
 
            eta = get_eta(self.start_time, self.last_cnt + self.child_fraction, 
334
 
                    self.last_total, last_updates = self.last_updates)
 
263
            return 
 
264
        
 
265
        if self.show_eta and self.start_time and total_cnt:
 
266
            eta = get_eta(self.start_time, current_cnt, total_cnt,
 
267
                    last_updates = self.last_updates)
335
268
            eta_str = " " + str_tdelta(eta)
336
269
        else:
337
270
            eta_str = ""
344
277
        # always update this; it's also used for the bar
345
278
        self.spin_pos += 1
346
279
 
347
 
        if self.show_pct and self.last_total and self.last_cnt:
348
 
            pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
 
280
        if self.show_pct and total_cnt and current_cnt:
 
281
            pct = 100.0 * current_cnt / total_cnt
349
282
            pct_str = ' (%5.1f%%)' % pct
350
283
        else:
351
284
            pct_str = ''
352
285
 
353
286
        if not self.show_count:
354
287
            count_str = ''
355
 
        elif self.last_cnt is None:
 
288
        elif current_cnt is None:
356
289
            count_str = ''
357
 
        elif self.last_total is None:
358
 
            count_str = ' %i' % (self.last_cnt)
 
290
        elif total_cnt is None:
 
291
            count_str = ' %i' % (current_cnt)
359
292
        else:
360
293
            # make both fields the same size
361
 
            t = '%i' % (self.last_total)
362
 
            c = '%*i' % (len(t), self.last_cnt)
 
294
            t = '%i' % (total_cnt)
 
295
            c = '%*i' % (len(t), current_cnt)
363
296
            count_str = ' ' + c + '/' + t 
364
297
 
365
298
        if self.show_bar:
366
299
            # progress bar, if present, soaks up all remaining space
367
 
            cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
 
300
            cols = self.width - 1 - len(msg) - len(spin_str) - len(pct_str) \
368
301
                   - len(eta_str) - len(count_str) - 3
369
302
 
370
 
            if self.last_total:
 
303
            if total_cnt:
371
304
                # number of markers highlighted in bar
372
 
                markers = int(round(float(cols) * 
373
 
                              (self.last_cnt + self.child_fraction) / self.last_total))
 
305
                markers = int(round(float(cols) * current_cnt / total_cnt))
374
306
                bar_str = '[' + ('=' * markers).ljust(cols) + '] '
375
307
            elif False:
376
308
                # don't know total, so can't show completion.
384
316
        else:
385
317
            bar_str = ''
386
318
 
387
 
        m = spin_str + bar_str + self.last_msg + count_str + pct_str + eta_str
 
319
        m = spin_str + bar_str + msg + count_str + pct_str + eta_str
388
320
 
389
321
        assert len(m) < self.width
390
322
        self.to_file.write('\r' + m.ljust(self.width - 1))
394
326
        self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
395
327
        #self.to_file.flush()        
396
328
 
397
 
 
398
 
class ChildProgress(_BaseProgressBar):
399
 
    """A progress indicator that pushes its data to the parent"""
400
 
    def __init__(self, _stack, **kwargs):
401
 
        _BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
402
 
        self.parent = _stack.top()
403
 
        self.current = None
404
 
        self.total = None
405
 
        self.child_fraction = 0
406
 
        self.message = None
407
 
 
408
 
    def update(self, msg, current_cnt=None, total_cnt=None):
409
 
        self.current = current_cnt
410
 
        self.total = total_cnt
411
 
        self.message = msg
412
 
        self.child_fraction = 0
413
 
        self.tick()
414
 
 
415
 
    def child_update(self, message, current, total):
416
 
        if current is None or total == 0:
417
 
            self.child_fraction = 0
418
 
        else:
419
 
            self.child_fraction = float(current) / total
420
 
        self.tick()
421
 
 
422
 
    def tick(self):
423
 
        if self.current is None:
424
 
            count = None
425
 
        else:
426
 
            count = self.current+self.child_fraction
427
 
            if count > self.total:
428
 
                if __debug__:
429
 
                    mutter('clamping count of %d to %d' % (count, self.total))
430
 
                count = self.total
431
 
        self.parent.child_update(self.message, count, self.total)
432
 
 
433
 
    def clear(self):
434
 
        pass
435
 
 
436
 
    def note(self, *args, **kwargs):
437
 
        self.parent.note(*args, **kwargs)
438
 
 
439
 
 
 
329
        
440
330
def str_tdelta(delt):
441
331
    if delt is None:
442
332
        return "-:--:--"
459
349
    if current > total:
460
350
        return None                     # wtf?
461
351
 
462
 
    elapsed = time.clock() - start_time
 
352
    elapsed = time.time() - start_time
463
353
 
464
354
    if elapsed < 2.0:                   # not enough time to estimate
465
355
        return None
482
372
    return total_duration - elapsed
483
373
 
484
374
 
485
 
class ProgressPhase(object):
486
 
    """Update progress object with the current phase"""
487
 
    def __init__(self, message, total, pb):
488
 
        object.__init__(self)
489
 
        self.pb = pb
490
 
        self.message = message
491
 
        self.total = total
492
 
        self.cur_phase = None
493
 
 
494
 
    def next_phase(self):
495
 
        if self.cur_phase is None:
496
 
            self.cur_phase = 0
497
 
        else:
498
 
            self.cur_phase += 1
499
 
        assert self.cur_phase < self.total 
500
 
        self.pb.update(self.message, self.cur_phase, self.total)
501
 
 
502
 
 
503
375
def run_tests():
504
376
    import doctest
505
377
    result = doctest.testmod()