blob: f9ac53a1d849707db1eaf521d0041306f7ee8dc8 [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
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040018from repo_trace import IsTrace
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.
25CSI_ERASE_LINE = '\x1b[2K'
26
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.
30CSI_ERASE_LINE_AFTER = '\x1b[K'
31
David Pursehouse819827a2020-02-12 15:20:19 +090032
Mike Frysinger8d2a6df2021-02-26 03:55:44 -050033def duration_str(total):
34 """A less noisy timedelta.__str__.
35
36 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
47
48
Shawn O. Pearce68194f42009-04-10 16:48:52 -070049class Progress(object):
Mike Frysinger151701e2021-04-13 15:07:21 -040050 def __init__(self, title, total=0, units='', print_newline=False, delay=True,
51 quiet=False):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070052 self._title = title
53 self._total = total
54 self._done = 0
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070055 self._start = time()
Mike Frysingerb2fa30a2021-02-24 00:15:32 -050056 self._show = not delay
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070057 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020058 self._print_newline = print_newline
Mike Frysingerfbb95a42021-02-23 17:34:35 -050059 # Only show the active jobs section if we run more than one in parallel.
60 self._show_jobs = False
61 self._active = 0
62
Mike Frysinger151701e2021-04-13 15:07:21 -040063 # When quiet, never show any output. It's a bit hacky, but reusing the
64 # existing logic that delays initial output keeps the rest of the class
65 # clean. Basically we set the start time to years in the future.
66 if quiet:
67 self._show = False
68 self._start += 2**32
69
Mike Frysingerfbb95a42021-02-23 17:34:35 -050070 def start(self, name):
71 self._active += 1
72 if not self._show_jobs:
73 self._show_jobs = self._active > 1
74 self.update(inc=0, msg='started ' + name)
75
76 def finish(self, name):
77 self.update(msg='finished ' + name)
78 self._active -= 1
Shawn O. Pearce68194f42009-04-10 16:48:52 -070079
Mike Frysinger3538dd22019-08-26 15:32:06 -040080 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070081 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070082
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070083 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070084 return
85
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070086 if not self._show:
87 if 0.5 <= time() - self._start:
88 self._show = True
89 else:
90 return
91
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070092 if self._total <= 0:
Mike Frysinger4c11aeb2022-04-19 02:30:09 -040093 sys.stderr.write('\r%s: %d,%s' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090094 self._title,
Mike Frysinger4c11aeb2022-04-19 02:30:09 -040095 self._done,
96 CSI_ERASE_LINE_AFTER))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070097 sys.stderr.flush()
98 else:
99 p = (100 * self._done) / self._total
Mike Frysingerfbb95a42021-02-23 17:34:35 -0500100 if self._show_jobs:
101 jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '')
102 else:
103 jobs = ''
Mike Frysinger4c11aeb2022-04-19 02:30:09 -0400104 sys.stderr.write('\r%s: %2d%% %s(%d%s/%d%s)%s%s%s%s' % (
Mike Frysinger4e05f652021-02-23 16:57:56 -0500105 self._title,
106 p,
Mike Frysingerfbb95a42021-02-23 17:34:35 -0500107 jobs,
Mike Frysinger4e05f652021-02-23 16:57:56 -0500108 self._done, self._units,
109 self._total, self._units,
110 ' ' if msg else '', msg,
Mike Frysinger4c11aeb2022-04-19 02:30:09 -0400111 CSI_ERASE_LINE_AFTER,
Mike Frysinger4e05f652021-02-23 16:57:56 -0500112 '\n' if self._print_newline else ''))
113 sys.stderr.flush()
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700114
115 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -0700116 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -0700117 return
118
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500119 duration = duration_str(time() - self._start)
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700120 if self._total <= 0:
Mike Frysinger4c11aeb2022-04-19 02:30:09 -0400121 sys.stderr.write('\r%s: %d, done in %s%s\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900122 self._title,
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500123 self._done,
Mike Frysinger4c11aeb2022-04-19 02:30:09 -0400124 duration,
125 CSI_ERASE_LINE_AFTER))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700126 sys.stderr.flush()
127 else:
128 p = (100 * self._done) / self._total
Mike Frysinger4c11aeb2022-04-19 02:30:09 -0400129 sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done in %s%s\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900130 self._title,
131 p,
132 self._done, self._units,
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500133 self._total, self._units,
Mike Frysinger4c11aeb2022-04-19 02:30:09 -0400134 duration,
135 CSI_ERASE_LINE_AFTER))
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700136 sys.stderr.flush()