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