Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2009 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 17 | import os |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 18 | import sys |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 19 | from time import time |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 20 | from repo_trace import IsTrace |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 21 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 22 | _NOT_TTY = not os.isatty(2) |
| 23 | |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 24 | # This will erase all content in the current line (wherever the cursor is). |
| 25 | # It does not move the cursor, so this is usually followed by \r to move to |
| 26 | # column 0. |
| 27 | CSI_ERASE_LINE = '\x1b[2K' |
| 28 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 29 | |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 30 | class Progress(object): |
Tim Schumacher | 7be072e | 2017-06-28 18:29:23 +0200 | [diff] [blame] | 31 | def __init__(self, title, total=0, units='', print_newline=False, |
| 32 | always_print_percentage=False): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 33 | self._title = title |
| 34 | self._total = total |
| 35 | self._done = 0 |
| 36 | self._lastp = -1 |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 37 | self._start = time() |
| 38 | self._show = False |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 39 | self._units = units |
Tim Schumacher | 913327f | 2017-06-05 15:01:41 +0200 | [diff] [blame] | 40 | self._print_newline = print_newline |
Tim Schumacher | 7be072e | 2017-06-28 18:29:23 +0200 | [diff] [blame] | 41 | self._always_print_percentage = always_print_percentage |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 42 | |
Mike Frysinger | 3538dd2 | 2019-08-26 15:32:06 -0400 | [diff] [blame] | 43 | def update(self, inc=1, msg=''): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 44 | self._done += inc |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 45 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 46 | if _NOT_TTY or IsTrace(): |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 47 | return |
| 48 | |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 49 | if not self._show: |
| 50 | if 0.5 <= time() - self._start: |
| 51 | self._show = True |
| 52 | else: |
| 53 | return |
| 54 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 55 | if self._total <= 0: |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 56 | sys.stderr.write('%s\r%s: %d,' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 57 | CSI_ERASE_LINE, |
| 58 | self._title, |
| 59 | self._done)) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 60 | sys.stderr.flush() |
| 61 | else: |
| 62 | p = (100 * self._done) / self._total |
| 63 | |
Tim Schumacher | 7be072e | 2017-06-28 18:29:23 +0200 | [diff] [blame] | 64 | if self._lastp != p or self._always_print_percentage: |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 65 | self._lastp = p |
Mike Frysinger | 3538dd2 | 2019-08-26 15:32:06 -0400 | [diff] [blame] | 66 | sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s)%s%s%s' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 67 | CSI_ERASE_LINE, |
| 68 | self._title, |
| 69 | p, |
| 70 | self._done, self._units, |
| 71 | self._total, self._units, |
| 72 | ' ' if msg else '', msg, |
| 73 | "\n" if self._print_newline else "")) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 74 | sys.stderr.flush() |
| 75 | |
| 76 | def end(self): |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 77 | if _NOT_TTY or IsTrace() or not self._show: |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 78 | return |
| 79 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 80 | if self._total <= 0: |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 81 | sys.stderr.write('%s\r%s: %d, done.\n' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 82 | CSI_ERASE_LINE, |
| 83 | self._title, |
| 84 | self._done)) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 85 | sys.stderr.flush() |
| 86 | else: |
| 87 | p = (100 * self._done) / self._total |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 88 | sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 89 | CSI_ERASE_LINE, |
| 90 | self._title, |
| 91 | p, |
| 92 | self._done, self._units, |
| 93 | self._total, self._units)) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 94 | sys.stderr.flush() |