Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [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 | |
| 18 | class Progress(object): |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame^] | 19 | def __init__(self, title, total=0): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 20 | self._title = title |
| 21 | self._total = total |
| 22 | self._done = 0 |
| 23 | self._lastp = -1 |
| 24 | |
| 25 | def update(self, inc=1): |
| 26 | self._done += inc |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 27 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame^] | 28 | if self._total <= 0: |
| 29 | sys.stderr.write('\r%s: %d, ' % ( |
| 30 | self._title, |
| 31 | self._done)) |
| 32 | sys.stderr.flush() |
| 33 | else: |
| 34 | p = (100 * self._done) / self._total |
| 35 | |
| 36 | if self._lastp != p: |
| 37 | self._lastp = p |
| 38 | sys.stderr.write('\r%s: %3d%% (%d/%d) ' % ( |
| 39 | self._title, |
| 40 | p, |
| 41 | self._done, |
| 42 | self._total)) |
| 43 | sys.stderr.flush() |
| 44 | |
| 45 | def end(self): |
| 46 | if self._total <= 0: |
| 47 | sys.stderr.write('\r%s: %d, done. \n' % ( |
| 48 | self._title, |
| 49 | self._done)) |
| 50 | sys.stderr.flush() |
| 51 | else: |
| 52 | p = (100 * self._done) / self._total |
| 53 | sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % ( |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 54 | self._title, |
| 55 | p, |
| 56 | self._done, |
| 57 | self._total)) |
| 58 | sys.stderr.flush() |