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