blob: d19fc5b171d03cda1727ba38753d23cf9cba100c [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
Shawn O. Pearce68194f42009-04-10 16:48:52 -070028class Progress(object):
Mike Frysinger4e05f652021-02-23 16:57:56 -050029 def __init__(self, title, total=0, units='', print_newline=False):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070030 self._title = title
31 self._total = total
32 self._done = 0
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070033 self._start = time()
34 self._show = False
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070035 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020036 self._print_newline = print_newline
Mike Frysingerfbb95a42021-02-23 17:34:35 -050037 # Only show the active jobs section if we run more than one in parallel.
38 self._show_jobs = False
39 self._active = 0
40
41 def start(self, name):
42 self._active += 1
43 if not self._show_jobs:
44 self._show_jobs = self._active > 1
45 self.update(inc=0, msg='started ' + name)
46
47 def finish(self, name):
48 self.update(msg='finished ' + name)
49 self._active -= 1
Shawn O. Pearce68194f42009-04-10 16:48:52 -070050
Mike Frysinger3538dd22019-08-26 15:32:06 -040051 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070052 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070053
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070054 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070055 return
56
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070057 if not self._show:
58 if 0.5 <= time() - self._start:
59 self._show = True
60 else:
61 return
62
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070063 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040064 sys.stderr.write('%s\r%s: %d,' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090065 CSI_ERASE_LINE,
66 self._title,
67 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070068 sys.stderr.flush()
69 else:
70 p = (100 * self._done) / self._total
Mike Frysingerfbb95a42021-02-23 17:34:35 -050071 if self._show_jobs:
72 jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '')
73 else:
74 jobs = ''
75 sys.stderr.write('%s\r%s: %2d%% %s(%d%s/%d%s)%s%s%s' % (
Mike Frysinger4e05f652021-02-23 16:57:56 -050076 CSI_ERASE_LINE,
77 self._title,
78 p,
Mike Frysingerfbb95a42021-02-23 17:34:35 -050079 jobs,
Mike Frysinger4e05f652021-02-23 16:57:56 -050080 self._done, self._units,
81 self._total, self._units,
82 ' ' if msg else '', msg,
83 '\n' if self._print_newline else ''))
84 sys.stderr.flush()
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070085
86 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070087 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070088 return
89
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070090 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040091 sys.stderr.write('%s\r%s: %d, done.\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090092 CSI_ERASE_LINE,
93 self._title,
94 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070095 sys.stderr.flush()
96 else:
97 p = (100 * self._done) / self._total
Mike Frysinger70d861f2019-08-26 15:22:36 -040098 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090099 CSI_ERASE_LINE,
100 self._title,
101 p,
102 self._done, self._units,
103 self._total, self._units))
Shawn O. Pearce68194f42009-04-10 16:48:52 -0700104 sys.stderr.flush()