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): |
Tim Schumacher | 7be072e | 2017-06-28 18:29:23 +0200 | [diff] [blame^] | 24 | def __init__(self, title, total=0, units='', print_newline=False, |
| 25 | always_print_percentage=False): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 26 | self._title = title |
| 27 | self._total = total |
| 28 | self._done = 0 |
| 29 | self._lastp = -1 |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 30 | self._start = time() |
| 31 | self._show = False |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 32 | self._units = units |
Tim Schumacher | 913327f | 2017-06-05 15:01:41 +0200 | [diff] [blame] | 33 | self._print_newline = print_newline |
Tim Schumacher | 7be072e | 2017-06-28 18:29:23 +0200 | [diff] [blame^] | 34 | self._always_print_percentage = always_print_percentage |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 35 | |
| 36 | def update(self, inc=1): |
| 37 | self._done += inc |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 38 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 39 | if _NOT_TTY or IsTrace(): |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 40 | return |
| 41 | |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 42 | if not self._show: |
| 43 | if 0.5 <= time() - self._start: |
| 44 | self._show = True |
| 45 | else: |
| 46 | return |
| 47 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 48 | if self._total <= 0: |
| 49 | sys.stderr.write('\r%s: %d, ' % ( |
| 50 | self._title, |
| 51 | self._done)) |
| 52 | sys.stderr.flush() |
| 53 | else: |
| 54 | p = (100 * self._done) / self._total |
| 55 | |
Tim Schumacher | 7be072e | 2017-06-28 18:29:23 +0200 | [diff] [blame^] | 56 | if self._lastp != p or self._always_print_percentage: |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 57 | self._lastp = p |
Tim Schumacher | 913327f | 2017-06-05 15:01:41 +0200 | [diff] [blame] | 58 | sys.stderr.write('\r%s: %3d%% (%d%s/%d%s)%s' % ( |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 59 | self._title, |
| 60 | p, |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 61 | self._done, self._units, |
Tim Schumacher | 913327f | 2017-06-05 15:01:41 +0200 | [diff] [blame] | 62 | self._total, self._units, |
| 63 | "\n" if self._print_newline else "")) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 64 | sys.stderr.flush() |
| 65 | |
| 66 | def end(self): |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 67 | if _NOT_TTY or IsTrace() or not self._show: |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 68 | return |
| 69 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 70 | if self._total <= 0: |
| 71 | sys.stderr.write('\r%s: %d, done. \n' % ( |
| 72 | self._title, |
| 73 | self._done)) |
| 74 | sys.stderr.flush() |
| 75 | else: |
| 76 | p = (100 * self._done) / self._total |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 77 | sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done. \n' % ( |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 78 | self._title, |
| 79 | p, |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 80 | self._done, self._units, |
| 81 | self._total, self._units)) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 82 | sys.stderr.flush() |