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 |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 17 | import time |
| 18 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 19 | |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 20 | try: |
| 21 | import threading as _threading |
| 22 | except ImportError: |
| 23 | import dummy_threading as _threading |
| 24 | |
LaMont Jones | 47020ba | 2022-11-10 00:11:51 +0000 | [diff] [blame] | 25 | from repo_trace import IsTraceToStderr |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 26 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 27 | |
Gavin Mak | b2263ba | 2023-06-07 21:59:17 +0000 | [diff] [blame] | 28 | _TTY = sys.stderr.isatty() |
Shawn O. Pearce | f4f04d9 | 2010-05-27 16:48:36 -0700 | [diff] [blame] | 29 | |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 30 | # This will erase all content in the current line (wherever the cursor is). |
| 31 | # It does not move the cursor, so this is usually followed by \r to move to |
| 32 | # column 0. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 33 | CSI_ERASE_LINE = "\x1b[2K" |
Mike Frysinger | 70d861f | 2019-08-26 15:22:36 -0400 | [diff] [blame] | 34 | |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame] | 35 | # This will erase all content in the current line after the cursor. This is |
| 36 | # useful for partial updates & progress messages as the terminal can display |
| 37 | # it better. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 38 | CSI_ERASE_LINE_AFTER = "\x1b[K" |
Mike Frysinger | 4c11aeb | 2022-04-19 02:30:09 -0400 | [diff] [blame] | 39 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 40 | |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 41 | def convert_to_hms(total): |
| 42 | """Converts a period of seconds to hours, minutes, and seconds.""" |
| 43 | hours, rem = divmod(total, 3600) |
| 44 | mins, secs = divmod(rem, 60) |
| 45 | return int(hours), int(mins), secs |
| 46 | |
| 47 | |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 48 | def duration_str(total): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 49 | """A less noisy timedelta.__str__. |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 50 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 51 | The default timedelta stringification contains a lot of leading zeros and |
| 52 | uses microsecond resolution. This makes for noisy output. |
| 53 | """ |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 54 | hours, mins, secs = convert_to_hms(total) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 55 | ret = "%.3fs" % (secs,) |
| 56 | if mins: |
| 57 | ret = "%im%s" % (mins, ret) |
| 58 | if hours: |
| 59 | ret = "%ih%s" % (hours, ret) |
| 60 | return ret |
Mike Frysinger | 8d2a6df | 2021-02-26 03:55:44 -0500 | [diff] [blame] | 61 | |
| 62 | |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 63 | def elapsed_str(total): |
| 64 | """Returns seconds in the format [H:]MM:SS. |
| 65 | |
| 66 | Does not display a leading zero for minutes if under 10 minutes. This should |
| 67 | be used when displaying elapsed time in a progress indicator. |
| 68 | """ |
| 69 | hours, mins, secs = convert_to_hms(total) |
| 70 | ret = f"{int(secs):>02d}" |
| 71 | if total >= 3600: |
| 72 | # Show leading zeroes if over an hour. |
| 73 | ret = f"{mins:>02d}:{ret}" |
| 74 | else: |
| 75 | ret = f"{mins}:{ret}" |
| 76 | if hours: |
| 77 | ret = f"{hours}:{ret}" |
| 78 | return ret |
| 79 | |
| 80 | |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 81 | def jobs_str(total): |
| 82 | return f"{total} job{'s' if total > 1 else ''}" |
| 83 | |
| 84 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame^] | 85 | class Progress: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 86 | def __init__( |
| 87 | self, |
| 88 | title, |
| 89 | total=0, |
| 90 | units="", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 91 | delay=True, |
| 92 | quiet=False, |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 93 | show_elapsed=False, |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 94 | elide=False, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 95 | ): |
| 96 | self._title = title |
| 97 | self._total = total |
| 98 | self._done = 0 |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 99 | self._start = time.time() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 100 | self._show = not delay |
| 101 | self._units = units |
Gavin Mak | b2263ba | 2023-06-07 21:59:17 +0000 | [diff] [blame] | 102 | self._elide = elide and _TTY |
| 103 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 104 | # Only show the active jobs section if we run more than one in parallel. |
| 105 | self._show_jobs = False |
| 106 | self._active = 0 |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 107 | |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 108 | # Save the last message for displaying on refresh. |
| 109 | self._last_msg = None |
| 110 | self._show_elapsed = show_elapsed |
| 111 | self._update_event = _threading.Event() |
| 112 | self._update_thread = _threading.Thread( |
| 113 | target=self._update_loop, |
| 114 | ) |
| 115 | self._update_thread.daemon = True |
| 116 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 117 | # When quiet, never show any output. It's a bit hacky, but reusing the |
| 118 | # existing logic that delays initial output keeps the rest of the class |
| 119 | # clean. Basically we set the start time to years in the future. |
| 120 | if quiet: |
| 121 | self._show = False |
| 122 | self._start += 2**32 |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 123 | elif show_elapsed: |
| 124 | self._update_thread.start() |
| 125 | |
| 126 | def _update_loop(self): |
| 127 | while True: |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 128 | self.update(inc=0) |
| 129 | if self._update_event.wait(timeout=1): |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 130 | return |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 131 | |
| 132 | def _write(self, s): |
| 133 | s = "\r" + s |
| 134 | if self._elide: |
Gavin Mak | b2263ba | 2023-06-07 21:59:17 +0000 | [diff] [blame] | 135 | col = os.get_terminal_size(sys.stderr.fileno()).columns |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 136 | if len(s) > col: |
| 137 | s = s[: col - 1] + ".." |
| 138 | sys.stderr.write(s) |
| 139 | sys.stderr.flush() |
Mike Frysinger | 151701e | 2021-04-13 15:07:21 -0400 | [diff] [blame] | 140 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 141 | def start(self, name): |
| 142 | self._active += 1 |
| 143 | if not self._show_jobs: |
| 144 | self._show_jobs = self._active > 1 |
| 145 | self.update(inc=0, msg="started " + name) |
Mike Frysinger | fbb95a4 | 2021-02-23 17:34:35 -0500 | [diff] [blame] | 146 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 147 | def finish(self, name): |
| 148 | self.update(msg="finished " + name) |
| 149 | self._active -= 1 |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 150 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 151 | def update(self, inc=1, msg=None): |
| 152 | """Updates the progress indicator. |
| 153 | |
| 154 | Args: |
| 155 | inc: The number of items completed. |
| 156 | msg: The message to display. If None, use the last message. |
| 157 | """ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 158 | self._done += inc |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 159 | if msg is None: |
| 160 | msg = self._last_msg |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 161 | self._last_msg = msg |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 162 | |
Gavin Mak | b2263ba | 2023-06-07 21:59:17 +0000 | [diff] [blame] | 163 | if not _TTY or IsTraceToStderr(): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 164 | return |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 165 | |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 166 | elapsed_sec = time.time() - self._start |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 167 | if not self._show: |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 168 | if 0.5 <= elapsed_sec: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 169 | self._show = True |
| 170 | else: |
| 171 | return |
Shawn O. Pearce | 2810cbc | 2009-04-18 10:09:16 -0700 | [diff] [blame] | 172 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 173 | if self._total <= 0: |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 174 | self._write( |
| 175 | "%s: %d,%s" % (self._title, self._done, CSI_ERASE_LINE_AFTER) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 176 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 177 | else: |
| 178 | p = (100 * self._done) / self._total |
| 179 | if self._show_jobs: |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 180 | jobs = f"[{jobs_str(self._active)}] " |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 181 | else: |
| 182 | jobs = "" |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 183 | if self._show_elapsed: |
| 184 | elapsed = f" {elapsed_str(elapsed_sec)} |" |
| 185 | else: |
| 186 | elapsed = "" |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 187 | self._write( |
| 188 | "%s: %2d%% %s(%d%s/%d%s)%s %s%s" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 189 | % ( |
| 190 | self._title, |
| 191 | p, |
| 192 | jobs, |
| 193 | self._done, |
| 194 | self._units, |
| 195 | self._total, |
| 196 | self._units, |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 197 | elapsed, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 198 | msg, |
| 199 | CSI_ERASE_LINE_AFTER, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 200 | ) |
| 201 | ) |
Shawn O. Pearce | b1168ff | 2009-04-16 08:00:42 -0700 | [diff] [blame] | 202 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 203 | def end(self): |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 204 | self._update_event.set() |
Gavin Mak | b2263ba | 2023-06-07 21:59:17 +0000 | [diff] [blame] | 205 | if not _TTY or IsTraceToStderr() or not self._show: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 206 | return |
Shawn O. Pearce | 6ed4e28 | 2009-04-18 09:59:18 -0700 | [diff] [blame] | 207 | |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 208 | duration = duration_str(time.time() - self._start) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 209 | if self._total <= 0: |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 210 | self._write( |
| 211 | "%s: %d, done in %s%s\n" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 212 | % (self._title, self._done, duration, CSI_ERASE_LINE_AFTER) |
| 213 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 214 | else: |
| 215 | p = (100 * self._done) / self._total |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 216 | self._write( |
| 217 | "%s: %3d%% (%d%s/%d%s), done in %s%s\n" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 218 | % ( |
| 219 | self._title, |
| 220 | p, |
| 221 | self._done, |
| 222 | self._units, |
| 223 | self._total, |
| 224 | self._units, |
| 225 | duration, |
| 226 | CSI_ERASE_LINE_AFTER, |
| 227 | ) |
| 228 | ) |