blob: 580ae884faa9fd15899ec9eb267056d6f00501a5 [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
17
18class Progress(object):
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070019 def __init__(self, title, total=0):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070020 self._title = title
21 self._total = total
22 self._done = 0
23 self._lastp = -1
24
25 def update(self, inc=1):
26 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070027
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070028 if self._total <= 0:
29 sys.stderr.write('\r%s: %d, ' % (
30 self._title,
31 self._done))
32 sys.stderr.flush()
33 else:
34 p = (100 * self._done) / self._total
35
36 if self._lastp != p:
37 self._lastp = p
38 sys.stderr.write('\r%s: %3d%% (%d/%d) ' % (
39 self._title,
40 p,
41 self._done,
42 self._total))
43 sys.stderr.flush()
44
45 def end(self):
46 if self._total <= 0:
47 sys.stderr.write('\r%s: %d, done. \n' % (
48 self._title,
49 self._done))
50 sys.stderr.flush()
51 else:
52 p = (100 * self._done) / self._total
53 sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % (
Shawn O. Pearce68194f42009-04-10 16:48:52 -070054 self._title,
55 p,
56 self._done,
57 self._total))
58 sys.stderr.flush()