2018-02-02 15:47:17 +01:00
|
|
|
# Copyright (C) 2018 SUSE Linux. All rights reserved.
|
|
|
|
# This program is free software; it may be used, copied, modified
|
|
|
|
# and distributed under the terms of the GNU General Public Licence,
|
|
|
|
# either version 2, or (at your option) any later version.
|
2006-07-14 19:39:46 +02:00
|
|
|
|
2022-01-03 11:46:59 +01:00
|
|
|
|
|
|
|
import signal
|
2024-01-24 09:49:25 +01:00
|
|
|
import sys
|
2024-01-24 09:49:24 +01:00
|
|
|
from abc import ABC
|
|
|
|
from abc import abstractmethod
|
|
|
|
from typing import Optional
|
2022-01-03 11:46:59 +01:00
|
|
|
|
2019-01-08 14:23:38 +01:00
|
|
|
try:
|
|
|
|
import progressbar as pb
|
|
|
|
have_pb_module = True
|
|
|
|
except ImportError:
|
|
|
|
have_pb_module = False
|
2006-07-14 19:39:46 +02:00
|
|
|
|
|
|
|
|
2024-01-24 09:49:24 +01:00
|
|
|
class TextMeterBase(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def start(self, basename: str, size: Optional[int] = None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def update(self, amount_read: int):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def end(self):
|
|
|
|
pass
|
2009-10-20 16:30:15 +02:00
|
|
|
|
2024-01-24 09:49:24 +01:00
|
|
|
|
|
|
|
class PBTextMeter(TextMeterBase):
|
|
|
|
def __init__(self):
|
|
|
|
self.bar: pb.ProgressBar
|
|
|
|
|
|
|
|
def start(self, basename: str, size: Optional[int] = None):
|
2018-02-02 15:47:17 +01:00
|
|
|
if size is None:
|
2024-01-06 09:54:57 +01:00
|
|
|
widgets = [f"{basename}: ", pb.AnimatedMarker(), ' ', pb.Timer()]
|
2018-02-02 15:47:17 +01:00
|
|
|
self.bar = pb.ProgressBar(widgets=widgets, maxval=pb.UnknownLength)
|
2006-07-14 19:39:46 +02:00
|
|
|
else:
|
2024-01-06 09:54:57 +01:00
|
|
|
widgets = [f"{basename}: ", pb.Bar(), ' ', pb.ETA()]
|
2019-01-23 15:35:27 +01:00
|
|
|
if size:
|
|
|
|
# if size is 0, using pb.Percentage will result in
|
|
|
|
# a ZeroDivisionException
|
|
|
|
widgets.insert(1, pb.Percentage())
|
2018-02-02 15:47:17 +01:00
|
|
|
self.bar = pb.ProgressBar(widgets=widgets, maxval=size)
|
2022-01-03 11:46:59 +01:00
|
|
|
# When a signal handler is set, it resets SA_RESTART flag
|
|
|
|
# - see signal.siginterrupt() python docs.
|
|
|
|
# ProgressBar's constructor sets signal handler for SIGWINCH.
|
|
|
|
# So let's make sure that it doesn't interrupt syscalls in osc.
|
|
|
|
signal.siginterrupt(signal.SIGWINCH, False)
|
2018-02-02 15:47:17 +01:00
|
|
|
self.bar.start()
|
2006-07-14 19:39:46 +02:00
|
|
|
|
2024-01-24 09:49:24 +01:00
|
|
|
def update(self, amount_read: int):
|
2018-02-02 15:47:17 +01:00
|
|
|
self.bar.update(amount_read)
|
2006-07-14 19:39:46 +02:00
|
|
|
|
2018-02-02 15:47:17 +01:00
|
|
|
def end(self):
|
|
|
|
self.bar.finish()
|
2010-02-25 09:52:47 +01:00
|
|
|
|
2019-01-08 14:23:38 +01:00
|
|
|
|
2022-07-28 19:11:29 +02:00
|
|
|
class NoPBTextMeter:
|
2024-01-24 09:49:24 +01:00
|
|
|
def start(self, basename: str, size: Optional[int] = None):
|
2023-10-13 20:36:55 +02:00
|
|
|
pass
|
2019-01-14 12:03:16 +01:00
|
|
|
|
2024-01-24 09:49:24 +01:00
|
|
|
def update(self, amount_read: int):
|
2019-01-14 12:03:16 +01:00
|
|
|
pass
|
|
|
|
|
2024-01-24 09:49:24 +01:00
|
|
|
def end(self):
|
2019-01-14 12:03:16 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-01-24 09:49:24 +01:00
|
|
|
def create_text_meter(*args, **kwargs) -> TextMeterBase:
|
|
|
|
from .conf import config
|
|
|
|
|
|
|
|
# this option is no longer used
|
|
|
|
kwargs.pop("use_pb_fallback", True)
|
|
|
|
|
|
|
|
meter_class = PBTextMeter
|
2024-01-24 09:49:25 +01:00
|
|
|
if not have_pb_module or config.quiet or not config.show_download_progress or not sys.stdout.isatty():
|
2024-01-24 09:49:24 +01:00
|
|
|
meter_class = NoPBTextMeter
|
|
|
|
|
|
|
|
return meter_class(*args, **kwargs)
|
2019-01-14 12:03:16 +01:00
|
|
|
|
2022-09-12 13:43:10 +02:00
|
|
|
|
2010-02-25 09:52:47 +01:00
|
|
|
# vim: sw=4 et
|