blob: 672926c1609c98f740be7d1fdf9b9bd2370a3a45 [file] [log] [blame]
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +00001#
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
17from time import time
18
19class Progress(object):
20 def __init__(self, title, total=0):
21 self._title = title
22 self._total = total
23 self._done = 0
24 self._lastp = -1
25 self._start = time()
26 self._show = False
maruel@chromium.org55a2eb82010-10-06 23:35:18 +000027 self._width = 0
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000028
maruel@chromium.org55a2eb82010-10-06 23:35:18 +000029 def update(self, inc=1, extra=''):
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000030 self._done += inc
31
32 if not self._show:
33 if 0.5 <= time() - self._start:
34 self._show = True
35 else:
36 return
37
maruel@chromium.org55a2eb82010-10-06 23:35:18 +000038 text = None
39
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000040 if self._total <= 0:
maruel@chromium.org55a2eb82010-10-06 23:35:18 +000041 text = '%s: %3d' % (self._title, self._done)
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000042 else:
43 p = (100 * self._done) / self._total
44
45 if self._lastp != p:
46 self._lastp = p
maruel@chromium.org55a2eb82010-10-06 23:35:18 +000047 text = '%s: %3d%% (%2d/%2d)' % (self._title, p,
48 self._done, self._total)
49
50 if text:
51 text += ' ' + extra
Daniel Bratelle6ddf192018-07-17 19:10:27 +000052 text = text[:self.terminal_width()] # Avoid wrapping
maruel@chromium.org55a2eb82010-10-06 23:35:18 +000053 spaces = max(self._width - len(text), 0)
54 sys.stdout.write('%s%*s\r' % (text, spaces, ''))
55 sys.stdout.flush()
56 self._width = len(text)
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000057
58 def end(self):
59 if not self._show:
60 return
61
62 if self._total <= 0:
szager@chromium.orgfe0d1902014-04-08 20:50:44 +000063 text = '%s: %d, done.' % (
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000064 self._title,
szager@chromium.orgfe0d1902014-04-08 20:50:44 +000065 self._done)
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000066 else:
67 p = (100 * self._done) / self._total
szager@chromium.orgfe0d1902014-04-08 20:50:44 +000068 text = '%s: %3d%% (%d/%d), done.' % (
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000069 self._title,
70 p,
71 self._done,
szager@chromium.orgfe0d1902014-04-08 20:50:44 +000072 self._total)
73
74 spaces = max(self._width - len(text), 0)
75 sys.stdout.write('%s%*s\n' % (text, spaces, ''))
76 sys.stdout.flush()
Daniel Bratelle6ddf192018-07-17 19:10:27 +000077
78 def terminal_width(self):
79 """Returns sys.maxsize if the width cannot be determined."""
80 try:
81 if not sys.stdout.isatty():
82 return sys.maxsize
83 if sys.platform == 'win32':
84 import ctypes
85 class CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
86 _fields_ = [
87 ('dwSize', ctypes.wintypes._COORD),
88 ('dwCursorPosition', ctypes.wintypes._COORD),
89 ('wAttributes', ctypes.c_ushort),
90 ('srWindow', ctypes.wintypes._SMALL_RECT),
91 ('dwMaximumWindowSize', ctypes.wintypes._COORD)
92 ]
93 # Get our own instance of the kernel lib since python
94 # libraries and python ports are known to manipulate
95 # ctypes.windll.kernel32. See
96 # https://github.com/pallets/click/issues/126
97 kernel32 = ctypes.WinDLL('kernel32')
98 handle = kernel32.GetStdHandle(-12) # -12 == stderr
99 console_screen_buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
100 if kernel32.GetConsoleScreenBufferInfo(
101 ctypes.c_ulong(handle),
102 ctypes.byref(console_screen_buffer_info)):
103 # Note that we return 1 less than the width since writing into
104 # the rightmost column automatically performs a line feed.
105 right = console_screen_buffer_info.srWindow.Right
106 left = console_screen_buffer_info.srWindow.Left
107 return right - left
108 return sys.maxsize
109 else:
110 import fcntl
111 import struct
112 import termios
113 packed = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, '\0' * 8)
114 _, columns, _, _ = struct.unpack('HHHH', packed)
115 return columns
116 except Exception: # pylint: disable=broad-except
117 return sys.maxsize