summaryrefslogtreecommitdiffstats
path: root/progress.py
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2009-04-16 08:00:42 -0700
committerShawn O. Pearce <sop@google.com>2009-04-16 08:05:05 -0700
commitb1168ffadaff387a8b7ab9a9c861073035c505a8 (patch)
tree8a922e173391861b2426c9700d9caa5765666249 /progress.py
parent4c5c7aa74b2dec2cbfb6b6bd7e24d5922e92c112 (diff)
downloadgit-repo-b1168ffadaff387a8b7ab9a9c861073035c505a8.tar.gz
Don't divide by zero in progress meter
If there are no projects to fetch, the progress meter would have divided by zero during `repo sync`, and that throws a ZeroDivisionError. Instead we report the progress with an unknown amount remaining. Signed-off-by: Shawn O. Pearce <sop@google.com>
Diffstat (limited to 'progress.py')
-rw-r--r--progress.py41
1 files changed, 27 insertions, 14 deletions
diff --git a/progress.py b/progress.py
index 89d6c5ba..580ae884 100644
--- a/progress.py
+++ b/progress.py
@@ -16,7 +16,7 @@
16import sys 16import sys
17 17
18class Progress(object): 18class Progress(object):
19 def __init__(self, title, total): 19 def __init__(self, title, total=0):
20 self._title = title 20 self._title = title
21 self._total = total 21 self._total = total
22 self._done = 0 22 self._done = 0
@@ -24,22 +24,35 @@ class Progress(object):
24 24
25 def update(self, inc=1): 25 def update(self, inc=1):
26 self._done += inc 26 self._done += inc
27 p = (100 * self._done) / self._total
28 27
29 if self._lastp != p: 28 if self._total <= 0:
30 self._lastp = p 29 sys.stderr.write('\r%s: %d, ' % (
31 sys.stderr.write('\r%s: %3d%% (%d/%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' % (
32 self._title, 54 self._title,
33 p, 55 p,
34 self._done, 56 self._done,
35 self._total)) 57 self._total))
36 sys.stderr.flush() 58 sys.stderr.flush()
37
38 def end(self):
39 p = (100 * self._done) / self._total
40 sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % (
41 self._title,
42 p,
43 self._done,
44 self._total))
45 sys.stderr.flush()