blob: d1a7c543faa5a0563ad822d641729c2877cbd11a [file] [log] [blame]
Shawn O. Pearce68194f42009-04-10 16:48:52 -07001# 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. Pearcef4f04d92010-05-27 16:48:36 -070015import os
Shawn O. Pearce68194f42009-04-10 16:48:52 -070016import sys
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070017from time import time
LaMont Jones47020ba2022-11-10 00:11:51 +000018from repo_trace import IsTraceToStderr
Shawn O. Pearce68194f42009-04-10 16:48:52 -070019
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070020_NOT_TTY = not os.isatty(2)
21
Mike Frysinger70d861f2019-08-26 15:22:36 -040022# 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.
Gavin Makea2e3302023-03-11 06:46:20 +000025CSI_ERASE_LINE = "\x1b[2K"
Mike Frysinger70d861f2019-08-26 15:22:36 -040026
Mike Frysinger4c11aeb2022-04-19 02:30:09 -040027# 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.
Gavin Makea2e3302023-03-11 06:46:20 +000030CSI_ERASE_LINE_AFTER = "\x1b[K"
Mike Frysinger4c11aeb2022-04-19 02:30:09 -040031
David Pursehouse819827a2020-02-12 15:20:19 +090032
Mike Frysinger8d2a6df2021-02-26 03:55:44 -050033def duration_str(total):
Gavin Makea2e3302023-03-11 06:46:20 +000034 """A less noisy timedelta.__str__.
Mike Frysinger8d2a6df2021-02-26 03:55:44 -050035
Gavin Makea2e3302023-03-11 06:46:20 +000036 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
Mike Frysinger8d2a6df2021-02-26 03:55:44 -050047
48
Shawn O. Pearce68194f42009-04-10 16:48:52 -070049class Progress(object):
Gavin Makea2e3302023-03-11 06:46:20 +000050 def __init__(
51 self,
52 title,
53 total=0,
54 units="",
55 print_newline=False,
56 delay=True,
57 quiet=False,
58 ):
59 self._title = title
60 self._total = total
61 self._done = 0
62 self._start = time()
63 self._show = not delay
64 self._units = units
65 self._print_newline = print_newline
66 # Only show the active jobs section if we run more than one in parallel.
67 self._show_jobs = False
68 self._active = 0
Mike Frysingerfbb95a42021-02-23 17:34:35 -050069
Gavin Makea2e3302023-03-11 06:46:20 +000070 # When quiet, never show any output. It's a bit hacky, but reusing the
71 # existing logic that delays initial output keeps the rest of the class
72 # clean. Basically we set the start time to years in the future.
73 if quiet:
74 self._show = False
75 self._start += 2**32
Mike Frysinger151701e2021-04-13 15:07:21 -040076
Gavin Makea2e3302023-03-11 06:46:20 +000077 def start(self, name):
78 self._active += 1
79 if not self._show_jobs:
80 self._show_jobs = self._active > 1
81 self.update(inc=0, msg="started " + name)
Mike Frysingerfbb95a42021-02-23 17:34:35 -050082
Gavin Makea2e3302023-03-11 06:46:20 +000083 def finish(self, name):
84 self.update(msg="finished " + name)
85 self._active -= 1
Shawn O. Pearce68194f42009-04-10 16:48:52 -070086
Gavin Makea2e3302023-03-11 06:46:20 +000087 def update(self, inc=1, msg=""):
88 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070089
Gavin Makea2e3302023-03-11 06:46:20 +000090 if _NOT_TTY or IsTraceToStderr():
91 return
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070092
Gavin Makea2e3302023-03-11 06:46:20 +000093 if not self._show:
94 if 0.5 <= time() - self._start:
95 self._show = True
96 else:
97 return
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070098
Gavin Makea2e3302023-03-11 06:46:20 +000099 if self._total <= 0:
100 sys.stderr.write(
101 "\r%s: %d,%s" % (self._title, self._done, CSI_ERASE_LINE_AFTER)
102 )
103 sys.stderr.flush()
104 else:
105 p = (100 * self._done) / self._total
106 if self._show_jobs:
107 jobs = "[%d job%s] " % (
108 self._active,
109 "s" if self._active > 1 else "",
110 )
111 else:
112 jobs = ""
113 sys.stderr.write(
114 "\r%s: %2d%% %s(%d%s/%d%s)%s%s%s%s"
115 % (
116 self._title,
117 p,
118 jobs,
119 self._done,
120 self._units,
121 self._total,
122 self._units,
123 " " if msg else "",
124 msg,
125 CSI_ERASE_LINE_AFTER,
126 "\n" if self._print_newline else "",
127 )
128 )
129 sys.stderr.flush()
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700130
Gavin Makea2e3302023-03-11 06:46:20 +0000131 def end(self):
132 if _NOT_TTY or IsTraceToStderr() or not self._show:
133 return
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -0700134
Gavin Makea2e3302023-03-11 06:46:20 +0000135 duration = duration_str(time() - self._start)
136 if self._total <= 0:
137 sys.stderr.write(
138 "\r%s: %d, done in %s%s\n"
139 % (self._title, self._done, duration, CSI_ERASE_LINE_AFTER)
140 )
141 sys.stderr.flush()
142 else:
143 p = (100 * self._done) / self._total
144 sys.stderr.write(
145 "\r%s: %3d%% (%d%s/%d%s), done in %s%s\n"
146 % (
147 self._title,
148 p,
149 self._done,
150 self._units,
151 self._total,
152 self._units,
153 duration,
154 CSI_ERASE_LINE_AFTER,
155 )
156 )
157 sys.stderr.flush()