nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 1 | # |
| 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 | |
| 16 | import sys |
| 17 | from time import time |
| 18 | |
| 19 | class 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.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 27 | self._width = 0 |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 28 | |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 29 | def update(self, inc=1, extra=''): |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 30 | 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.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 38 | text = None |
| 39 | |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 40 | if self._total <= 0: |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 41 | text = '%s: %3d' % (self._title, self._done) |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 42 | else: |
| 43 | p = (100 * self._done) / self._total |
| 44 | |
| 45 | if self._lastp != p: |
| 46 | self._lastp = p |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 47 | text = '%s: %3d%% (%2d/%2d)' % (self._title, p, |
| 48 | self._done, self._total) |
| 49 | |
| 50 | if text: |
| 51 | text += ' ' + extra |
Daniel Bratell | e6ddf19 | 2018-07-17 19:10:27 +0000 | [diff] [blame] | 52 | text = text[:self.terminal_width()] # Avoid wrapping |
maruel@chromium.org | 55a2eb8 | 2010-10-06 23:35:18 +0000 | [diff] [blame] | 53 | 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.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 57 | |
| 58 | def end(self): |
| 59 | if not self._show: |
| 60 | return |
| 61 | |
| 62 | if self._total <= 0: |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 63 | text = '%s: %d, done.' % ( |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 64 | self._title, |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 65 | self._done) |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 66 | else: |
| 67 | p = (100 * self._done) / self._total |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 68 | text = '%s: %3d%% (%d/%d), done.' % ( |
nasser@codeaurora.org | 1f7a3d1 | 2010-02-04 15:11:50 +0000 | [diff] [blame] | 69 | self._title, |
| 70 | p, |
| 71 | self._done, |
szager@chromium.org | fe0d190 | 2014-04-08 20:50:44 +0000 | [diff] [blame] | 72 | 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 Bratell | e6ddf19 | 2018-07-17 19:10:27 +0000 | [diff] [blame] | 77 | |
| 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 |