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 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 16 | import os |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 17 | import sys |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 18 | from time import time |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 19 | from trace import IsTrace |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 20 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 21 | _NOT_TTY = not os.isatty(2) |
| 22 | |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 23 | class Progress(object): |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 24 | def __init__(self, title, total=0): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 25 | self._title = title |
| 26 | self._total = total |
| 27 | self._done = 0 |
| 28 | self._lastp = -1 |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 29 | self._start = time() |
| 30 | self._show = False |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 31 | |
| 32 | def update(self, inc=1): |
| 33 | self._done += inc |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 34 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 35 | if _NOT_TTY or IsTrace(): |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 36 | return |
| 37 | |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 38 | if not self._show: |
| 39 | if 0.5 <= time() - self._start: |
| 40 | self._show = True |
| 41 | else: |
| 42 | return |
| 43 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 44 | if self._total <= 0: |
| 45 | sys.stderr.write('\r%s: %d, ' % ( |
| 46 | self._title, |
| 47 | self._done)) |
| 48 | sys.stderr.flush() |
| 49 | else: |
| 50 | p = (100 * self._done) / self._total |
| 51 | |
| 52 | if self._lastp != p: |
| 53 | self._lastp = p |
| 54 | sys.stderr.write('\r%s: %3d%% (%d/%d) ' % ( |
| 55 | self._title, |
| 56 | p, |
| 57 | self._done, |
| 58 | self._total)) |
| 59 | sys.stderr.flush() |
| 60 | |
| 61 | def end(self): |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 62 | if _NOT_TTY or IsTrace() or not self._show: |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 63 | return |
| 64 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 65 | if self._total <= 0: |
| 66 | sys.stderr.write('\r%s: %d, done. \n' % ( |
| 67 | self._title, |
| 68 | self._done)) |
| 69 | sys.stderr.flush() |
| 70 | else: |
| 71 | p = (100 * self._done) / self._total |
| 72 | sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % ( |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 73 | self._title, |
| 74 | p, |
| 75 | self._done, |
| 76 | self._total)) |
| 77 | sys.stderr.flush() |