blob: d948654ff3b83aae36a29d8c18f89e7d7b1ff871 [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
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070016import os
Shawn O. Pearce68194f42009-04-10 16:48:52 -070017import sys
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070018from time import time
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070019from trace import IsTrace
Shawn O. Pearce68194f42009-04-10 16:48:52 -070020
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070021_NOT_TTY = not os.isatty(2)
22
Shawn O. Pearce68194f42009-04-10 16:48:52 -070023class Progress(object):
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070024 def __init__(self, title, total=0, units=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070025 self._title = title
26 self._total = total
27 self._done = 0
28 self._lastp = -1
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070029 self._start = time()
30 self._show = False
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070031 self._units = units
Shawn O. Pearce68194f42009-04-10 16:48:52 -070032
33 def update(self, inc=1):
34 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070035
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070036 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070037 return
38
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070039 if not self._show:
40 if 0.5 <= time() - self._start:
41 self._show = True
42 else:
43 return
44
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070045 if self._total <= 0:
46 sys.stderr.write('\r%s: %d, ' % (
47 self._title,
48 self._done))
49 sys.stderr.flush()
50 else:
51 p = (100 * self._done) / self._total
52
53 if self._lastp != p:
54 self._lastp = p
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070055 sys.stderr.write('\r%s: %3d%% (%d%s/%d%s) ' % (
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070056 self._title,
57 p,
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070058 self._done, self._units,
59 self._total, self._units))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070060 sys.stderr.flush()
61
62 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070063 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070064 return
65
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070066 if self._total <= 0:
67 sys.stderr.write('\r%s: %d, done. \n' % (
68 self._title,
69 self._done))
70 sys.stderr.flush()
71 else:
72 p = (100 * self._done) / self._total
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070073 sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done. \n' % (
Shawn O. Pearce68194f42009-04-10 16:48:52 -070074 self._title,
75 p,
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070076 self._done, self._units,
77 self._total, self._units))
Shawn O. Pearce68194f42009-04-10 16:48:52 -070078 sys.stderr.flush()