Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 1 | # Copyright (C) 2009 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 15 | import os |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 16 | import sys |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 17 | from time import time |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 18 | from repo_trace import IsTrace |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 19 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 20 | _NOT_TTY = not os.isatty(2) |
| 21 | |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 22 | # This will erase all content in the current line (wherever the cursor is). |
| 23 | # It does not move the cursor, so this is usually followed by \r to move to |
| 24 | # column 0. |
| 25 | CSI_ERASE_LINE = '\x1b[2K' |
| 26 | |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 27 | # This will erase all content in the current line after the cursor. This is |
| 28 | # useful for partial updates & progress messages as the terminal can display |
| 29 | # it better. |
| 30 | CSI_ERASE_LINE_AFTER = '\x1b[K' |
| 31 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 32 | |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 33 | def duration_str(total): |
| 34 | """A less noisy timedelta.__str__. |
| 35 | |
| 36 | The default timedelta stringification contains a lot of leading zeros and |
| 37 | uses microsecond resolution. This makes for noisy output. |
| 38 | """ |
| 39 | hours, rem = divmod(total, 3600) |
| 40 | mins, secs = divmod(rem, 60) |
| 41 | ret = '%.3fs' % (secs,) |
| 42 | if mins: |
| 43 | ret = '%im%s' % (mins, ret) |
| 44 | if hours: |
| 45 | ret = '%ih%s' % (hours, ret) |
| 46 | return ret |
| 47 | |
| 48 | |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 49 | class Progress(object): |
Mike Frysinger | 151701e | 2021-04-13 15:07:21 -0400 | [diff] [blame] | 50 | def __init__(self, title, total=0, units='', print_newline=False, delay=True, |
| 51 | quiet=False): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 52 | self._title = title |
| 53 | self._total = total |
| 54 | self._done = 0 |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 55 | self._start = time() |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 56 | self._show = not delay |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 57 | self._units = units |
Tim Schumacher | 913327f | 2017-06-05 15:01:41 +0200 | [diff] [blame] | 58 | self._print_newline = print_newline |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 59 | # Only show the active jobs section if we run more than one in parallel. |
| 60 | self._show_jobs = False |
| 61 | self._active = 0 |
| 62 | |
Mike Frysinger | 151701e | 2021-04-13 15:07:21 -0400 | [diff] [blame] | 63 | # When quiet, never show any output. It's a bit hacky, but reusing the |
| 64 | # existing logic that delays initial output keeps the rest of the class |
| 65 | # clean. Basically we set the start time to years in the future. |
| 66 | if quiet: |
| 67 | self._show = False |
| 68 | self._start += 2**32 |
| 69 | |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 70 | def start(self, name): |
| 71 | self._active += 1 |
| 72 | if not self._show_jobs: |
| 73 | self._show_jobs = self._active > 1 |
| 74 | self.update(inc=0, msg='started ' + name) |
| 75 | |
| 76 | def finish(self, name): |
| 77 | self.update(msg='finished ' + name) |
| 78 | self._active -= 1 |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 79 | |
Mike Frysinger | 3538dd2 | 2019-08-26 15:32:06 -0400 | [diff] [blame] | 80 | def update(self, inc=1, msg=''): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 81 | self._done += inc |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 82 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 83 | if _NOT_TTY or IsTrace(): |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 84 | return |
| 85 | |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 86 | if not self._show: |
| 87 | if 0.5 <= time() - self._start: |
| 88 | self._show = True |
| 89 | else: |
| 90 | return |
| 91 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 92 | if self._total <= 0: |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 93 | sys.stderr.write('\r%s: %d,%s' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 94 | self._title, |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 95 | self._done, |
| 96 | CSI_ERASE_LINE_AFTER)) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 97 | sys.stderr.flush() |
| 98 | else: |
| 99 | p = (100 * self._done) / self._total |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 100 | if self._show_jobs: |
| 101 | jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '') |
| 102 | else: |
| 103 | jobs = '' |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 104 | sys.stderr.write('\r%s: %2d%% %s(%d%s/%d%s)%s%s%s%s' % ( |
Mike Frysinger | 4e05f65 | 2021-02-23 16:57:56 -0500 | [diff] [blame] | 105 | self._title, |
| 106 | p, |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 107 | jobs, |
Mike Frysinger | 4e05f65 | 2021-02-23 16:57:56 -0500 | [diff] [blame] | 108 | self._done, self._units, |
| 109 | self._total, self._units, |
| 110 | ' ' if msg else '', msg, |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 111 | CSI_ERASE_LINE_AFTER, |
Mike Frysinger | 4e05f65 | 2021-02-23 16:57:56 -0500 | [diff] [blame] | 112 | '\n' if self._print_newline else '')) |
| 113 | sys.stderr.flush() |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 114 | |
| 115 | def end(self): |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 116 | if _NOT_TTY or IsTrace() or not self._show: |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 117 | return |
| 118 | |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 119 | duration = duration_str(time() - self._start) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 120 | if self._total <= 0: |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 121 | sys.stderr.write('\r%s: %d, done in %s%s\n' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 122 | self._title, |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 123 | self._done, |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 124 | duration, |
| 125 | CSI_ERASE_LINE_AFTER)) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 126 | sys.stderr.flush() |
| 127 | else: |
| 128 | p = (100 * self._done) / self._total |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 129 | sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done in %s%s\n' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 130 | self._title, |
| 131 | p, |
| 132 | self._done, self._units, |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 133 | self._total, self._units, |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame^] | 134 | duration, |
| 135 | CSI_ERASE_LINE_AFTER)) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 136 | sys.stderr.flush() |