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 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 27 | |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 28 | def duration_str(total): |
| 29 | """A less noisy timedelta.__str__. |
| 30 | |
| 31 | The default timedelta stringification contains a lot of leading zeros and |
| 32 | uses microsecond resolution. This makes for noisy output. |
| 33 | """ |
| 34 | hours, rem = divmod(total, 3600) |
| 35 | mins, secs = divmod(rem, 60) |
| 36 | ret = '%.3fs' % (secs,) |
| 37 | if mins: |
| 38 | ret = '%im%s' % (mins, ret) |
| 39 | if hours: |
| 40 | ret = '%ih%s' % (hours, ret) |
| 41 | return ret |
| 42 | |
| 43 | |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 44 | class Progress(object): |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 45 | def __init__(self, title, total=0, units='', print_newline=False, delay=True): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 46 | self._title = title |
| 47 | self._total = total |
| 48 | self._done = 0 |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 49 | self._start = time() |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 50 | self._show = not delay |
Shawn O. Pearce | 490d09a | 2011-09-19 08:56:47 -0700 | [diff] [blame] | 51 | self._units = units |
Tim Schumacher | 913327f | 2017-06-05 15:01:41 +0200 | [diff] [blame] | 52 | self._print_newline = print_newline |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 53 | # Only show the active jobs section if we run more than one in parallel. |
| 54 | self._show_jobs = False |
| 55 | self._active = 0 |
| 56 | |
| 57 | def start(self, name): |
| 58 | self._active += 1 |
| 59 | if not self._show_jobs: |
| 60 | self._show_jobs = self._active > 1 |
| 61 | self.update(inc=0, msg='started ' + name) |
| 62 | |
| 63 | def finish(self, name): |
| 64 | self.update(msg='finished ' + name) |
| 65 | self._active -= 1 |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 66 | |
Mike Frysinger | 3538dd2 | 2019-08-26 15:32:06 -0400 | [diff] [blame] | 67 | def update(self, inc=1, msg=''): |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 68 | self._done += inc |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 69 | |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 70 | if _NOT_TTY or IsTrace(): |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 71 | return |
| 72 | |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 73 | if not self._show: |
| 74 | if 0.5 <= time() - self._start: |
| 75 | self._show = True |
| 76 | else: |
| 77 | return |
| 78 | |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 79 | if self._total <= 0: |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 80 | sys.stderr.write('%s\r%s: %d,' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 81 | CSI_ERASE_LINE, |
| 82 | self._title, |
| 83 | self._done)) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 84 | sys.stderr.flush() |
| 85 | else: |
| 86 | p = (100 * self._done) / self._total |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 87 | if self._show_jobs: |
| 88 | jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '') |
| 89 | else: |
| 90 | jobs = '' |
| 91 | sys.stderr.write('%s\r%s: %2d%% %s(%d%s/%d%s)%s%s%s' % ( |
Mike Frysinger | 4e05f65 | 2021-02-23 16:57:56 -0500 | [diff] [blame] | 92 | CSI_ERASE_LINE, |
| 93 | self._title, |
| 94 | p, |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 95 | jobs, |
Mike Frysinger | 4e05f65 | 2021-02-23 16:57:56 -0500 | [diff] [blame] | 96 | self._done, self._units, |
| 97 | self._total, self._units, |
| 98 | ' ' if msg else '', msg, |
| 99 | '\n' if self._print_newline else '')) |
| 100 | sys.stderr.flush() |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 101 | |
| 102 | def end(self): |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 103 | if _NOT_TTY or IsTrace() or not self._show: |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 104 | return |
| 105 | |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 106 | duration = duration_str(time() - self._start) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 107 | if self._total <= 0: |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 108 | sys.stderr.write('%s\r%s: %d, done in %s\n' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 109 | CSI_ERASE_LINE, |
| 110 | self._title, |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 111 | self._done, |
| 112 | duration)) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 113 | sys.stderr.flush() |
| 114 | else: |
| 115 | p = (100 * self._done) / self._total |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 116 | sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 117 | CSI_ERASE_LINE, |
| 118 | self._title, |
| 119 | p, |
| 120 | self._done, self._units, |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 121 | self._total, self._units, |
| 122 | duration)) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 123 | sys.stderr.flush() |