blob: de46f538155cc894be1adfb7817c6a7817dd7605 [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
David Pursehouse819827a2020-02-12 15:20:19 +090027
Mike Frysinger8d2a6df2021-02-26 03:55:44 -050028def duration_str(total):
29 """A less noisy timedelta.__str__.
30
31 The default timedelta stringification contains a lot of leading zeros and
32 uses microsecond resolution. This makes for noisy output.
33 """
34 hours, rem = divmod(total, 3600)
35 mins, secs = divmod(rem, 60)
36 ret = '%.3fs' % (secs,)
37 if mins:
38 ret = '%im%s' % (mins, ret)
39 if hours:
40 ret = '%ih%s' % (hours, ret)
41 return ret
42
43
Shawn O. Pearce68194f42009-04-10 16:48:52 -070044class Progress(object):
Mike Frysingerb2fa30a2021-02-24 00:15:32 -050045 def __init__(self, title, total=0, units='', print_newline=False, delay=True):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070046 self._title = title
47 self._total = total
48 self._done = 0
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070049 self._start = time()
Mike Frysingerb2fa30a2021-02-24 00:15:32 -050050 self._show = not delay
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070051 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020052 self._print_newline = print_newline
Mike Frysingerfbb95a42021-02-23 17:34:35 -050053 # Only show the active jobs section if we run more than one in parallel.
54 self._show_jobs = False
55 self._active = 0
56
57 def start(self, name):
58 self._active += 1
59 if not self._show_jobs:
60 self._show_jobs = self._active > 1
61 self.update(inc=0, msg='started ' + name)
62
63 def finish(self, name):
64 self.update(msg='finished ' + name)
65 self._active -= 1
Shawn O. Pearce68194f42009-04-10 16:48:52 -070066
Mike Frysinger3538dd22019-08-26 15:32:06 -040067 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070068 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070069
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070070 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070071 return
72
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070073 if not self._show:
74 if 0.5 <= time() - self._start:
75 self._show = True
76 else:
77 return
78
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070079 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040080 sys.stderr.write('%s\r%s: %d,' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090081 CSI_ERASE_LINE,
82 self._title,
83 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070084 sys.stderr.flush()
85 else:
86 p = (100 * self._done) / self._total
Mike Frysingerfbb95a42021-02-23 17:34:35 -050087 if self._show_jobs:
88 jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '')
89 else:
90 jobs = ''
91 sys.stderr.write('%s\r%s: %2d%% %s(%d%s/%d%s)%s%s%s' % (
Mike Frysinger4e05f652021-02-23 16:57:56 -050092 CSI_ERASE_LINE,
93 self._title,
94 p,
Mike Frysingerfbb95a42021-02-23 17:34:35 -050095 jobs,
Mike Frysinger4e05f652021-02-23 16:57:56 -050096 self._done, self._units,
97 self._total, self._units,
98 ' ' if msg else '', msg,
99 '\n' if self._print_newline else ''))
100 sys.stderr.flush()
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700101
102 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -0700103 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -0700104 return
105
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500106 duration = duration_str(time() - self._start)
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700107 if self._total <= 0:
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500108 sys.stderr.write('%s\r%s: %d, done in %s\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900109 CSI_ERASE_LINE,
110 self._title,
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500111 self._done,
112 duration))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700113 sys.stderr.flush()
114 else:
115 p = (100 * self._done) / self._total
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500116 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900117 CSI_ERASE_LINE,
118 self._title,
119 p,
120 self._done, self._units,
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500121 self._total, self._units,
122 duration))
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700123 sys.stderr.flush()