blob: 98bb642960cf2c659be1f76ce0a044d8cc80aaec [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. Pearce6ed4e282009-04-18 09:59:18 -070017from trace import IsTrace
Shawn O. Pearce68194f42009-04-10 16:48:52 -070018
19class Progress(object):
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070020 def __init__(self, title, total=0):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070021 self._title = title
22 self._total = total
23 self._done = 0
24 self._lastp = -1
25
26 def update(self, inc=1):
27 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070028
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070029 if IsTrace():
30 return
31
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070032 if self._total <= 0:
33 sys.stderr.write('\r%s: %d, ' % (
34 self._title,
35 self._done))
36 sys.stderr.flush()
37 else:
38 p = (100 * self._done) / self._total
39
40 if self._lastp != p:
41 self._lastp = p
42 sys.stderr.write('\r%s: %3d%% (%d/%d) ' % (
43 self._title,
44 p,
45 self._done,
46 self._total))
47 sys.stderr.flush()
48
49 def end(self):
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070050 if IsTrace():
51 return
52
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070053 if self._total <= 0:
54 sys.stderr.write('\r%s: %d, done. \n' % (
55 self._title,
56 self._done))
57 sys.stderr.flush()
58 else:
59 p = (100 * self._done) / self._total
60 sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % (
Shawn O. Pearce68194f42009-04-10 16:48:52 -070061 self._title,
62 p,
63 self._done,
64 self._total))
65 sys.stderr.flush()