blob: 43c7ad218dd8990668d907fcd20698c2c35c413b [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 Frysinger151701e2021-04-13 15:07:21 -040045 def __init__(self, title, total=0, units='', print_newline=False, delay=True,
46 quiet=False):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070047 self._title = title
48 self._total = total
49 self._done = 0
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070050 self._start = time()
Mike Frysingerb2fa30a2021-02-24 00:15:32 -050051 self._show = not delay
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070052 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020053 self._print_newline = print_newline
Mike Frysingerfbb95a42021-02-23 17:34:35 -050054 # Only show the active jobs section if we run more than one in parallel.
55 self._show_jobs = False
56 self._active = 0
57
Mike Frysinger151701e2021-04-13 15:07:21 -040058 # When quiet, never show any output. It's a bit hacky, but reusing the
59 # existing logic that delays initial output keeps the rest of the class
60 # clean. Basically we set the start time to years in the future.
61 if quiet:
62 self._show = False
63 self._start += 2**32
64
Mike Frysingerfbb95a42021-02-23 17:34:35 -050065 def start(self, name):
66 self._active += 1
67 if not self._show_jobs:
68 self._show_jobs = self._active > 1
69 self.update(inc=0, msg='started ' + name)
70
71 def finish(self, name):
72 self.update(msg='finished ' + name)
73 self._active -= 1
Shawn O. Pearce68194f42009-04-10 16:48:52 -070074
Mike Frysinger3538dd22019-08-26 15:32:06 -040075 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070076 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070077
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070078 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070079 return
80
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070081 if not self._show:
82 if 0.5 <= time() - self._start:
83 self._show = True
84 else:
85 return
86
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070087 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040088 sys.stderr.write('%s\r%s: %d,' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090089 CSI_ERASE_LINE,
90 self._title,
91 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070092 sys.stderr.flush()
93 else:
94 p = (100 * self._done) / self._total
Mike Frysingerfbb95a42021-02-23 17:34:35 -050095 if self._show_jobs:
96 jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '')
97 else:
98 jobs = ''
99 sys.stderr.write('%s\r%s: %2d%% %s(%d%s/%d%s)%s%s%s' % (
Mike Frysinger4e05f652021-02-23 16:57:56 -0500100 CSI_ERASE_LINE,
101 self._title,
102 p,
Mike Frysingerfbb95a42021-02-23 17:34:35 -0500103 jobs,
Mike Frysinger4e05f652021-02-23 16:57:56 -0500104 self._done, self._units,
105 self._total, self._units,
106 ' ' if msg else '', msg,
107 '\n' if self._print_newline else ''))
108 sys.stderr.flush()
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700109
110 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -0700111 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -0700112 return
113
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500114 duration = duration_str(time() - self._start)
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700115 if self._total <= 0:
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500116 sys.stderr.write('%s\r%s: %d, done in %s\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900117 CSI_ERASE_LINE,
118 self._title,
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500119 self._done,
120 duration))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -0700121 sys.stderr.flush()
122 else:
123 p = (100 * self._done) / self._total
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500124 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900125 CSI_ERASE_LINE,
126 self._title,
127 p,
128 self._done, self._units,
Mike Frysinger8d2a6df2021-02-26 03:55:44 -0500129 self._total, self._units,
130 duration))
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700131 sys.stderr.flush()