1
# Copyright (C) 2005 Aaron Bentley <aaron.bentley@utoronto.ca>
 
 
2
# Copyright (C) 2005, 2006 Canonical <canonical.com>
 
 
4
#    This program is free software; you can redistribute it and/or modify
 
 
5
#    it under the terms of the GNU General Public License as published by
 
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
 
7
#    (at your option) any later version.
 
 
9
#    This program is distributed in the hope that it will be useful,
 
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
12
#    GNU General Public License for more details.
 
 
14
#    You should have received a copy of the GNU General Public License
 
 
15
#    along with this program; if not, write to the Free Software
 
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
19
"""Simple text-mode progress indicator.
 
 
21
To display an indicator, create a ProgressBar object.  Call it,
 
 
22
passing Progress objects indicating the current state.  When done,
 
 
25
Progress is suppressed when output is not sent to a terminal, so as
 
 
26
not to clutter log files.
 
 
29
# TODO: should be a global option e.g. --silent that disables progress
 
 
30
# indicators, preferably without needing to adjust all code that
 
 
31
# potentially calls them.
 
 
33
# TODO: If not on a tty perhaps just print '......' for the benefit of IDEs, etc
 
 
35
# TODO: Optionally show elapsed time instead/as well as ETA; nicer
 
 
36
# when the rate is unpredictable
 
 
42
from collections import deque
 
 
45
import bzrlib.errors as errors
 
 
46
from bzrlib.trace import mutter 
 
 
49
def _supports_progress(f):
 
 
50
    if not hasattr(f, 'isatty'):
 
 
54
    if os.environ.get('TERM') == 'dumb':
 
 
55
        # e.g. emacs compile window
 
 
61
def ProgressBar(to_file=sys.stderr, **kwargs):
 
 
62
    """Abstract factory"""
 
 
63
    if _supports_progress(to_file):
 
 
64
        return TTYProgressBar(to_file=to_file, **kwargs)
 
 
66
        return DotsProgressBar(to_file=to_file, **kwargs)
 
 
69
class ProgressBarStack(object):
 
 
70
    """A stack of progress bars."""
 
 
79
                 to_messages_file=sys.stdout,
 
 
81
        """Setup the stack with the parameters the progress bars should have."""
 
 
82
        self._to_file = to_file
 
 
83
        self._show_pct = show_pct
 
 
84
        self._show_spinner = show_spinner
 
 
85
        self._show_eta = show_eta
 
 
86
        self._show_bar = show_bar
 
 
87
        self._show_count = show_count
 
 
88
        self._to_messages_file = to_messages_file
 
 
90
        self._klass = klass or TTYProgressBar
 
 
93
        if len(self._stack) != 0:
 
 
94
            return self._stack[-1]
 
 
99
        """Return a nested progress bar."""
 
 
100
        if len(self._stack) == 0:
 
 
103
            func = self.top().child_progress
 
 
104
        new_bar = func(to_file=self._to_file,
 
 
105
                       show_pct=self._show_pct,
 
 
106
                       show_spinner=self._show_spinner,
 
 
107
                       show_eta=self._show_eta,
 
 
108
                       show_bar=self._show_bar,
 
 
109
                       show_count=self._show_count,
 
 
110
                       to_messages_file=self._to_messages_file,
 
 
112
        self._stack.append(new_bar)
 
 
115
    def return_pb(self, bar):
 
 
116
        """Return bar after its been used."""
 
 
117
        if bar is not self._stack[-1]:
 
 
118
            raise errors.MissingProgressBarFinish()
 
 
122
class _BaseProgressBar(object):
 
 
131
                 to_messages_file=sys.stdout,
 
 
133
        object.__init__(self)
 
 
134
        self.to_file = to_file
 
 
135
        self.to_messages_file = to_messages_file
 
 
138
        self.last_total = None
 
 
139
        self.show_pct = show_pct
 
 
140
        self.show_spinner = show_spinner
 
 
141
        self.show_eta = show_eta
 
 
142
        self.show_bar = show_bar
 
 
143
        self.show_count = show_count
 
 
146
        self.MIN_PAUSE = 0.1 # seconds
 
 
149
        self.start_time = now
 
 
150
        # next update should not throttle
 
 
151
        self.last_update = now - self.MIN_PAUSE - 1
 
 
154
        """Return this bar to its progress stack."""
 
 
156
        assert self._stack is not None
 
 
157
        self._stack.return_pb(self)
 
 
159
    def note(self, fmt_string, *args, **kwargs):
 
 
160
        """Record a note without disrupting the progress bar."""
 
 
162
        self.to_messages_file.write(fmt_string % args)
 
 
163
        self.to_messages_file.write('\n')
 
 
165
    def child_progress(self, **kwargs):
 
 
166
        return ChildProgress(**kwargs)
 
 
169
class DummyProgress(_BaseProgressBar):
 
 
170
    """Progress-bar standin that does nothing.
 
 
172
    This can be used as the default argument for methods that
 
 
173
    take an optional progress indicator."""
 
 
177
    def update(self, msg=None, current=None, total=None):
 
 
180
    def child_update(self, message, current, total):
 
 
186
    def note(self, fmt_string, *args, **kwargs):
 
 
187
        """See _BaseProgressBar.note()."""
 
 
189
    def child_progress(self, **kwargs):
 
 
190
        return DummyProgress(**kwargs)
 
 
192
class DotsProgressBar(_BaseProgressBar):
 
 
194
    def __init__(self, **kwargs):
 
 
195
        _BaseProgressBar.__init__(self, **kwargs)
 
 
202
    def update(self, msg=None, current_cnt=None, total_cnt=None):
 
 
203
        if msg and msg != self.last_msg:
 
 
205
                self.to_file.write('\n')
 
 
207
            self.to_file.write(msg + ': ')
 
 
210
        self.to_file.write('.')
 
 
214
            self.to_file.write('\n')
 
 
216
    def child_update(self, message, current, total):
 
 
219
class TTYProgressBar(_BaseProgressBar):
 
 
220
    """Progress bar display object.
 
 
222
    Several options are available to control the display.  These can
 
 
223
    be passed as parameters to the constructor or assigned at any time:
 
 
226
        Show percentage complete.
 
 
228
        Show rotating baton.  This ticks over on every update even
 
 
229
        if the values don't change.
 
 
231
        Show predicted time-to-completion.
 
 
235
        Show numerical counts.
 
 
237
    The output file should be in line-buffered or unbuffered mode.
 
 
242
    def __init__(self, **kwargs):
 
 
243
        from bzrlib.osutils import terminal_width
 
 
244
        _BaseProgressBar.__init__(self, **kwargs)
 
 
246
        self.width = terminal_width()
 
 
247
        self.start_time = None
 
 
248
        self.last_updates = deque()
 
 
249
        self.child_fraction = 0
 
 
253
        """Return True if the bar was updated too recently"""
 
 
254
        # time.time consistently takes 40/4000 ms = 0.01 ms.
 
 
255
        # but every single update to the pb invokes it.
 
 
256
        # so we use time.clock which takes 20/4000 ms = 0.005ms
 
 
257
        # on the downside, time.clock() appears to have approximately
 
 
258
        # 10ms granularity, so we treat a zero-time change as 'throttled.'
 
 
261
        interval = now - self.last_update
 
 
263
        if interval < self.MIN_PAUSE:
 
 
266
        self.last_updates.append(now - self.last_update)
 
 
267
        self.last_update = now
 
 
272
        self.update(self.last_msg, self.last_cnt, self.last_total, 
 
 
275
    def child_update(self, message, current, total):
 
 
276
        if current is not None and total != 0:
 
 
277
            child_fraction = float(current) / total
 
 
278
            if self.last_cnt is None:
 
 
280
            elif self.last_cnt + child_fraction <= self.last_total:
 
 
281
                self.child_fraction = child_fraction
 
 
283
                mutter('not updating child fraction')
 
 
284
        if self.last_msg is None:
 
 
289
    def update(self, msg, current_cnt=None, total_cnt=None, 
 
 
291
        """Update and redraw progress bar."""
 
 
296
        if current_cnt > total_cnt:
 
 
297
            total_cnt = current_cnt
 
 
299
        ## # optional corner case optimisation 
 
 
300
        ## # currently does not seem to fire so costs more than saved.
 
 
301
        ## # trivial optimal case:
 
 
302
        ## # NB if callers are doing a clear and restore with
 
 
303
        ## # the saved values, this will prevent that:
 
 
304
        ## # in that case add a restore method that calls
 
 
305
        ## # _do_update or some such
 
 
306
        ## if (self.last_msg == msg and
 
 
307
        ##     self.last_cnt == current_cnt and
 
 
308
        ##     self.last_total == total_cnt and
 
 
309
        ##     self.child_fraction == child_fraction):
 
 
312
        old_msg = self.last_msg
 
 
313
        # save these for the tick() function
 
 
315
        self.last_cnt = current_cnt
 
 
316
        self.last_total = total_cnt
 
 
317
        self.child_fraction = child_fraction
 
 
319
        # each function call takes 20ms/4000 = 0.005 ms, 
 
 
320
        # but multiple that by 4000 calls -> starts to cost.
 
 
321
        # so anything to make this function call faster
 
 
322
        # will improve base 'diff' time by up to 0.1 seconds.
 
 
323
        if old_msg == self.last_msg and self.throttle():
 
 
326
        if self.show_eta and self.start_time and self.last_total:
 
 
327
            eta = get_eta(self.start_time, self.last_cnt + self.child_fraction, 
 
 
328
                    self.last_total, last_updates = self.last_updates)
 
 
329
            eta_str = " " + str_tdelta(eta)
 
 
333
        if self.show_spinner:
 
 
334
            spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '            
 
 
338
        # always update this; it's also used for the bar
 
 
341
        if self.show_pct and self.last_total and self.last_cnt:
 
 
342
            pct = 100.0 * ((self.last_cnt + self.child_fraction) / self.last_total)
 
 
343
            pct_str = ' (%5.1f%%)' % pct
 
 
347
        if not self.show_count:
 
 
349
        elif self.last_cnt is None:
 
 
351
        elif self.last_total is None:
 
 
352
            count_str = ' %i' % (self.last_cnt)
 
 
354
            # make both fields the same size
 
 
355
            t = '%i' % (self.last_total)
 
 
356
            c = '%*i' % (len(t), self.last_cnt)
 
 
357
            count_str = ' ' + c + '/' + t 
 
 
360
            # progress bar, if present, soaks up all remaining space
 
 
361
            cols = self.width - 1 - len(self.last_msg) - len(spin_str) - len(pct_str) \
 
 
362
                   - len(eta_str) - len(count_str) - 3
 
 
365
                # number of markers highlighted in bar
 
 
366
                markers = int(round(float(cols) * 
 
 
367
                              (self.last_cnt + self.child_fraction) / self.last_total))
 
 
368
                bar_str = '[' + ('=' * markers).ljust(cols) + '] '
 
 
370
                # don't know total, so can't show completion.
 
 
371
                # so just show an expanded spinning thingy
 
 
372
                m = self.spin_pos % cols
 
 
373
                ms = (' ' * m + '*').ljust(cols)
 
 
375
                bar_str = '[' + ms + '] '
 
 
381
        m = spin_str + bar_str + self.last_msg + count_str + pct_str + eta_str
 
 
383
        assert len(m) < self.width
 
 
384
        self.to_file.write('\r' + m.ljust(self.width - 1))
 
 
385
        #self.to_file.flush()
 
 
388
        self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
 
 
389
        #self.to_file.flush()        
 
 
392
class ChildProgress(_BaseProgressBar):
 
 
393
    """A progress indicator that pushes its data to the parent"""
 
 
394
    def __init__(self, _stack, **kwargs):
 
 
395
        _BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
 
 
396
        self.parent = _stack.top()
 
 
399
        self.child_fraction = 0
 
 
402
    def update(self, msg, current_cnt=None, total_cnt=None):
 
 
403
        self.current = current_cnt
 
 
404
        self.total = total_cnt
 
 
406
        self.child_fraction = 0
 
 
409
    def child_update(self, message, current, total):
 
 
410
        if current is None or total == 0:
 
 
411
            self.child_fraction = 0
 
 
413
            self.child_fraction = float(current) / total
 
 
417
        if self.current is None:
 
 
420
            count = self.current+self.child_fraction
 
 
421
            if count > self.total:
 
 
423
                    mutter('clamping count of %d to %d' % (count, self.total))
 
 
425
        self.parent.child_update(self.message, count, self.total)
 
 
431
def str_tdelta(delt):
 
 
434
    delt = int(round(delt))
 
 
435
    return '%d:%02d:%02d' % (delt/3600,
 
 
440
def get_eta(start_time, current, total, enough_samples=3, last_updates=None, n_recent=10):
 
 
441
    if start_time is None:
 
 
447
    if current < enough_samples:
 
 
453
    elapsed = time.clock() - start_time
 
 
455
    if elapsed < 2.0:                   # not enough time to estimate
 
 
458
    total_duration = float(elapsed) * float(total) / float(current)
 
 
460
    assert total_duration >= elapsed
 
 
462
    if last_updates and len(last_updates) >= n_recent:
 
 
463
        while len(last_updates) > n_recent:
 
 
464
            last_updates.popleft()
 
 
465
        avg = sum(last_updates) / float(len(last_updates))
 
 
466
        time_left = avg * (total - current)
 
 
468
        old_time_left = total_duration - elapsed
 
 
470
        # We could return the average, or some other value here
 
 
471
        return (time_left + old_time_left) / 2
 
 
473
    return total_duration - elapsed
 
 
476
class ProgressPhase(object):
 
 
477
    """Update progress object with the current phase"""
 
 
478
    def __init__(self, message, total, pb):
 
 
479
        object.__init__(self)
 
 
481
        self.message = message
 
 
483
        self.cur_phase = None
 
 
485
    def next_phase(self):
 
 
486
        if self.cur_phase is None:
 
 
490
        assert self.cur_phase < self.total 
 
 
491
        self.pb.update(self.message, self.cur_phase, self.total)
 
 
496
    result = doctest.testmod()
 
 
499
            print "All tests passed"
 
 
501
        print "No tests to run"
 
 
507
    print 'dumb-terminal test:'
 
 
508
    pb = DotsProgressBar()
 
 
510
        pb.update('Leoparden', i, 99)
 
 
516
    print 'smart-terminal test:'
 
 
517
    pb = ProgressBar(show_pct=True, show_bar=True, show_spinner=False)
 
 
519
        pb.update('Elephanten', i, 99)
 
 
527
if __name__ == "__main__":