nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2009 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import sys |
| 17 | from time import time |
| 18 | |
| 19 | class Progress(object): |
| 20 | def __init__(self, title, total=0): |
| 21 | self._title = title |
| 22 | self._total = total |
| 23 | self._done = 0 |
| 24 | self._lastp = -1 |
| 25 | self._start = time() |
| 26 | self._show = False |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 27 | self._width = 0 |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 28 | |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 29 | def update(self, inc=1, extra=''): |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 30 | self._done += inc |
| 31 | |
| 32 | if not self._show: |
| 33 | if 0.5 <= time() - self._start: |
| 34 | self._show = True |
| 35 | else: |
| 36 | return |
| 37 | |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 38 | text = None |
| 39 | |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 40 | if self._total <= 0: |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 41 | text = '%s: %3d' % (self._title, self._done) |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 42 | else: |
| 43 | p = (100 * self._done) / self._total |
| 44 | |
| 45 | if self._lastp != p: |
| 46 | self._lastp = p |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 47 | text = '%s: %3d%% (%2d/%2d)' % (self._title, p, |
| 48 | self._done, self._total) |
| 49 | |
| 50 | if text: |
| 51 | text += ' ' + extra |
| 52 | spaces = max(self._width - len(text), 0) |
| 53 | sys.stdout.write('%s%*s\r' % (text, spaces, '')) |
| 54 | sys.stdout.flush() |
| 55 | self._width = len(text) |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 56 | |
| 57 | def end(self): |
| 58 | if not self._show: |
| 59 | return |
| 60 | |
| 61 | if self._total <= 0: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame^] | 62 | text = '%s: %d, done.' % ( |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 63 | self._title, |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame^] | 64 | self._done) |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 65 | else: |
| 66 | p = (100 * self._done) / self._total |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame^] | 67 | text = '%s: %3d%% (%d/%d), done.' % ( |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 68 | self._title, |
| 69 | p, |
| 70 | self._done, |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame^] | 71 | self._total) |
| 72 | |
| 73 | spaces = max(self._width - len(text), 0) |
| 74 | sys.stdout.write('%s%*s\n' % (text, spaces, '')) |
| 75 | sys.stdout.flush() |