Drop progressbar2 dependency and use our own

This is to reduce the dependency set of rpmdevtools.

Resolves: #1927784
This commit is contained in:
Michal Domonkos 2021-02-11 15:32:54 +01:00
parent c5b93c8494
commit 987b185581
2 changed files with 150 additions and 3 deletions

133
progressbar.py Normal file
View File

@ -0,0 +1,133 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# A simple text-based progress bar, compatible with the basic API of:
# https://github.com/WoLpH/python-progressbar
#
# Copyright (C) 2021 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import shutil
import sys
import time
class ProgressBar:
FORMAT = '{value:>10} / {max_value:<10} [{bars}]'
BARS = '= '
SPINLEN = 5
def __init__(self, stream=sys.stderr, max_width=80, fps=10):
self._stream = stream
self._max_width = max_width
self._min_delay = 1 / fps
@staticmethod
def _format_value(value):
raise NotImplementedError()
def start(self, max_value):
self._value = 0
self._max_value = max_value or 0
self._status = dict()
self._spinner = 0
self._timestamp = 0
self.update(0)
def update(self, value):
self._value = value
if value > self._max_value:
self._max_value = 0
ts = time.time()
if (ts - self._timestamp) < self._min_delay:
return
self._timestamp = ts
status = {'value': self._format_value(value),
'max_value': self._format_value(self._max_value) \
if self._max_value else '???',
'bars': ''}
termw = min(shutil.get_terminal_size()[0], self._max_width)
nbars = max(termw - len(self.FORMAT.format(**status)), 0)
nfill = nskip = 0
if self._max_value:
nfill = round(nbars * value / self._max_value)
elif nbars > self.SPINLEN:
nfill = self.SPINLEN
nskip = self._spinner % (nbars - self.SPINLEN)
self._spinner = nskip + 1
status['bars'] = self.BARS[1] * nskip + \
self.BARS[0] * nfill + \
self.BARS[1] * (nbars - nfill - nskip)
if status == self._status:
return
self._status = status
self._stream.write('\r')
self._stream.write(self.FORMAT.format(**self._status))
self._stream.flush()
def finish(self):
self._max_value = self._value
self._timestamp = 0 # Force an update
self.update(self._value)
self._stream.write('\n')
self._stream.flush()
class DataTransferBar(ProgressBar):
@staticmethod
def _format_value(value):
symbols = ' KMGTPEZY'
depth = 0
max_depth = len(symbols) - 1
unit = 1024.0
# 1023.95 should be formatted as 1.0 (not 1024.0)
# More info: https://stackoverflow.com/a/63839503
thres = unit - 0.05
while value >= thres and depth < max_depth:
depth += 1
value /= unit
symbol = ' %siB' % symbols[depth] if depth > 0 else ''
return '%.1f%s' % (value, symbol)
if __name__ == '__main__':
# Show a dummy bar for debugging purposes
bar = DataTransferBar()
size = 50*1024*1024
chunk = 1024*1234
recvd = 0
bar.start(size)
while recvd < (size - chunk):
recvd += chunk
bar.update(recvd)
time.sleep(0.1)
bar.update(size)
bar.finish()

View File

@ -1,12 +1,13 @@
Name: rpmdevtools
Version: 9.3
Release: 4%{?dist}
Release: 5%{?dist}
Summary: RPM Development Tools
# rpmdev-setuptree is GPLv2, everything else GPLv2+
License: GPLv2+ and GPLv2
URL: https://pagure.io/rpmdevtools
Source0: https://releases.pagure.org/rpmdevtools/%{name}-%{version}.tar.xz
Source1: progressbar.py
# Use Accept-Encoding: identity together with decode_content=False
# This is to avoid text files downloaded gzipped
@ -25,7 +26,6 @@ BuildRequires: perl-generators
# python dependencies for spectool
# spectool is executed for creating man page
BuildRequires: python3-devel
BuildRequires: python3dist(progressbar2)
BuildRequires: python3dist(requests)
BuildRequires: python3dist(rpm)
# emacs-common >= 1:22.3-3 for macros.emacs
@ -44,7 +44,6 @@ Requires: gawk
Requires: grep
Requires: rpm-build >= 4.4.2.3
Requires: python%{python3_version}dist(argcomplete)
Requires: python%{python3_version}dist(progressbar2)
Requires: python%{python3_version}dist(requests)
Requires: python%{python3_version}dist(rpm)
Requires: sed
@ -76,6 +75,15 @@ rpmdev-bumpspec Bump revision in specfile
grep -lF "%{_bindir}/python " * \
| xargs sed -i -e "s|%{_bindir}/python |%{_bindir}/python3 |"
# Let spectool find the bundled progressbar2 implementation
cp %{SOURCE1} .
sed -i \
's|^\(import progressbar\)$|'\
'import sys\n'\
'sys.path.insert(1, "%{_datadir}/rpmdevtools")\n'\
'\1\nsys.path.pop(1)|' \
rpmdev-spectool
%build
%configure --libdir=%{_prefix}/lib
@ -103,6 +111,9 @@ done
ln -sr %{buildroot}%{_bindir}/rpmdev-spectool %{buildroot}%{_bindir}/spectool
echo ".so man1/rpmdev-spectool.1" > %{buildroot}%{_mandir}/man1/spectool.1
cp %{SOURCE1} %{buildroot}%{_datadir}/rpmdevtools/
%py_byte_compile %{python3} %{buildroot}%{_datadir}/rpmdevtools/
%files -f %{name}.files
%license COPYING
@ -120,6 +131,9 @@ echo ".so man1/rpmdev-spectool.1" > %{buildroot}%{_mandir}/man1/spectool.1
%changelog
* Thu Feb 11 2021 Michal Domonkos <mdomonko@redhat.com> - 9.3-5
- Drop progressbar2 dependency and bundle a tiny replacement (#1927784)
* Wed Jan 27 2021 Fedora Release Engineering <releng@fedoraproject.org> - 9.3-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_34_Mass_Rebuild