blob: b119b3741f93c3c914ec4b5fe7ab08db508c9c86 [file] [log] [blame]
Shawn O. Pearce68194f42009-04-10 16:48:52 -07001#
2# Copyright (C) 2009 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070017from time import time
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070018from trace import IsTrace
Shawn O. Pearce68194f42009-04-10 16:48:52 -070019
20class Progress(object):
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070021 def __init__(self, title, total=0):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070022 self._title = title
23 self._total = total
24 self._done = 0
25 self._lastp = -1
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070026 self._start = time()
27 self._show = False
Shawn O. Pearce68194f42009-04-10 16:48:52 -070028
29 def update(self, inc=1):
30 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070031
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070032 if IsTrace():
33 return
34
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070035 if not self._show:
36 if 0.5 <= time() - self._start:
37 self._show = True
38 else:
39 return
40
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070041 if self._total <= 0:
42 sys.stderr.write('\r%s: %d, ' % (
43 self._title,
44 self._done))
45 sys.stderr.flush()
46 else:
47 p = (100 * self._done) / self._total
48
49 if self._lastp != p:
50 self._lastp = p
51 sys.stderr.write('\r%s: %3d%% (%d/%d) ' % (
52 self._title,
53 p,
54 self._done,
55 self._total))
56 sys.stderr.flush()
57
58 def end(self):
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070059 if IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070060 return
61
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070062 if self._total <= 0:
63 sys.stderr.write('\r%s: %d, done. \n' % (
64 self._title,
65 self._done))
66 sys.stderr.flush()
67 else:
68 p = (100 * self._done) / self._total
69 sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % (
Shawn O. Pearce68194f42009-04-10 16:48:52 -070070 self._title,
71 p,
72 self._done,
73 self._total))
74 sys.stderr.flush()