mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-06 21:44:16 +02:00
[commons.util] improvement: returning a float for 'size_to_byte' parsing
This commit is contained in:
@@ -58,7 +58,7 @@ class SearchResult:
|
||||
|
||||
class UpgradeRequirement:
|
||||
|
||||
def __init__(self, pkg: SoftwarePackage, reason: str = None, required_size: int = None, extra_size: int = None, sorting_priority: int = 0):
|
||||
def __init__(self, pkg: SoftwarePackage, reason: str = None, required_size: float = None, extra_size: float = None, sorting_priority: int = 0):
|
||||
"""
|
||||
|
||||
:param pkg:
|
||||
|
||||
@@ -28,7 +28,7 @@ def deep_update(source: dict, overrides: dict):
|
||||
return source
|
||||
|
||||
|
||||
def size_to_byte(size: float, unit: str) -> int:
|
||||
def size_to_byte(size: float, unit: str) -> float:
|
||||
lower_unit = unit.lower()
|
||||
final_size = size
|
||||
|
||||
@@ -36,19 +36,17 @@ def size_to_byte(size: float, unit: str) -> int:
|
||||
final_size /= 8
|
||||
|
||||
if lower_unit[0] == 'b':
|
||||
pass
|
||||
return final_size
|
||||
elif lower_unit[0] == 'k':
|
||||
final_size = final_size * 1024
|
||||
return final_size * 1024
|
||||
elif lower_unit[0] == 'm':
|
||||
final_size = final_size * (1024 ** 2)
|
||||
return final_size * (1024 ** 2)
|
||||
elif lower_unit[0] == 'g':
|
||||
final_size = final_size * (1024 ** 3)
|
||||
return final_size * (1024 ** 3)
|
||||
elif lower_unit[0] == 't':
|
||||
final_size = final_size * (1024 ** 4)
|
||||
return final_size * (1024 ** 4)
|
||||
else:
|
||||
final_size = final_size * (1024 ** 5)
|
||||
|
||||
return round(final_size)
|
||||
return final_size * (1024 ** 5)
|
||||
|
||||
|
||||
def datetime_as_milis(date: datetime = datetime.utcnow()) -> int:
|
||||
|
||||
@@ -474,7 +474,7 @@ def is_mirrors_available() -> bool:
|
||||
return bool(shutil.which('pacman-mirrors'))
|
||||
|
||||
|
||||
def map_update_sizes(pkgs: List[str]) -> Dict[str, int]: # bytes:
|
||||
def map_update_sizes(pkgs: List[str]) -> Dict[str, float]: # bytes:
|
||||
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
||||
|
||||
if output:
|
||||
@@ -483,7 +483,7 @@ def map_update_sizes(pkgs: List[str]) -> Dict[str, int]: # bytes:
|
||||
return {}
|
||||
|
||||
|
||||
def map_download_sizes(pkgs: List[str]) -> Dict[str, int]: # bytes:
|
||||
def map_download_sizes(pkgs: List[str]) -> Dict[str, float]: # bytes:
|
||||
output = run_cmd('pacman -Si {}'.format(' '.join(pkgs)))
|
||||
|
||||
if output:
|
||||
@@ -492,7 +492,7 @@ def map_download_sizes(pkgs: List[str]) -> Dict[str, int]: # bytes:
|
||||
return {}
|
||||
|
||||
|
||||
def get_installed_size(pkgs: List[str]) -> Dict[str, int]: # bytes
|
||||
def get_installed_size(pkgs: List[str]) -> Dict[str, float]: # bytes
|
||||
output = run_cmd('pacman -Qi {}'.format(' '.join(pkgs)))
|
||||
|
||||
if output:
|
||||
|
||||
@@ -58,7 +58,7 @@ class DebianPackage(SoftwarePackage):
|
||||
description: Optional[str] = None, maintainer: Optional[str] = None, installed: bool = False,
|
||||
update: bool = False, app: Optional[DebianApplication] = None, compressed_size: Optional[int] = None,
|
||||
uncompressed_size: Optional[int] = None, categories: Tuple[str] = None,
|
||||
updates_ignored: Optional[bool] = None, transaction_size: Optional[int] = None):
|
||||
updates_ignored: Optional[bool] = None, transaction_size: Optional[float] = None):
|
||||
super(DebianPackage, self).__init__(id=name, name=name, version=version, installed=installed,
|
||||
description=description, update=update,
|
||||
latest_version=latest_version if latest_version is not None else version)
|
||||
|
||||
@@ -410,7 +410,7 @@ def run(app_id: str):
|
||||
subprocess.Popen((f'flatpak run {app_id}',), shell=True, env={**os.environ})
|
||||
|
||||
|
||||
def map_update_download_size(app_ids: Iterable[str], installation: str, version: Version) -> Dict[str, int]:
|
||||
def map_update_download_size(app_ids: Iterable[str], installation: str, version: Version) -> Dict[str, float]:
|
||||
success, output = ProcessHandler().handle_simple(SimpleProcess(('flatpak', 'update', f'--{installation}')))
|
||||
if version >= VERSION_1_2:
|
||||
res = {}
|
||||
|
||||
@@ -270,7 +270,7 @@ class UpgradeSelected(AsyncAction):
|
||||
read_only=True,
|
||||
icon_path=icon_path)
|
||||
|
||||
def _sum_pkgs_size(self, reqs: List[UpgradeRequirement]) -> Tuple[int, int]:
|
||||
def _sum_pkgs_size(self, reqs: List[UpgradeRequirement]) -> Tuple[float, float]:
|
||||
required, extra = 0, 0
|
||||
for r in reqs:
|
||||
if r.required_size is not None:
|
||||
|
||||
@@ -8,26 +8,26 @@ class SizeToByteTest(TestCase):
|
||||
def test_must_return_right_number_of_bytes(self):
|
||||
self.assertEqual(1.0, size_to_byte(1, 'B'))
|
||||
self.assertEqual(1024, size_to_byte(1, 'K'))
|
||||
self.assertEqual(round(float('58675.2')), size_to_byte(57.3, 'K'))
|
||||
self.assertEqual(58675.2, size_to_byte(57.3, 'K'))
|
||||
self.assertEqual(1048576, size_to_byte(1, 'M'))
|
||||
self.assertEqual(round(float('60083404.8')), size_to_byte(57.3, 'M'))
|
||||
self.assertEqual(60083404.8, size_to_byte(57.3, 'M'))
|
||||
self.assertEqual(1073741824, size_to_byte(1, 'G'))
|
||||
self.assertEqual(round(float('61525406515.2')), size_to_byte(57.3, 'G'))
|
||||
self.assertEqual(61525406515.2, size_to_byte(57.3, 'G'))
|
||||
self.assertEqual(1099511627776, size_to_byte(1, 'T'))
|
||||
self.assertEqual(round(float('63002016271564.8')), size_to_byte(57.3, 'T'))
|
||||
self.assertEqual(63002016271564.8, size_to_byte(57.3, 'T'))
|
||||
self.assertEqual(1125899906842624, size_to_byte(1, 'P'))
|
||||
self.assertEqual(round(float('64514064662082350')), size_to_byte(57.3, 'P'))
|
||||
self.assertEqual(6383852471797678, size_to_byte(5.67, 'P'))
|
||||
|
||||
def test_must_return_right_number_of_bytes_for_bit_units(self):
|
||||
self.assertEqual(1, size_to_byte(8, 'b'))
|
||||
self.assertEqual(2, size_to_byte(16, 'b'))
|
||||
self.assertEqual(128, size_to_byte(1, 'KiB'))
|
||||
self.assertEqual(197460, size_to_byte(1542.66, 'KiB'))
|
||||
self.assertEqual(197460.48, size_to_byte(1542.66, 'KiB'))
|
||||
self.assertEqual(131072, size_to_byte(1, 'MiB'))
|
||||
self.assertEqual(546570, size_to_byte(4.17, 'MiB'))
|
||||
self.assertEqual(546570.24, size_to_byte(4.17, 'MiB'))
|
||||
self.assertEqual(134217728, size_to_byte(1, 'GiB'))
|
||||
self.assertEqual(2691065446, size_to_byte(20.05, 'GiB'))
|
||||
self.assertEqual(2691065446.4, size_to_byte(20.05, 'GiB'))
|
||||
self.assertEqual(137438953472, size_to_byte(1, 'TiB'))
|
||||
self.assertEqual(6926923254989, size_to_byte(50.4, 'TiB'))
|
||||
self.assertEqual(6926923254988.8, size_to_byte(50.4, 'TiB'))
|
||||
self.assertEqual(140737488355328, size_to_byte(1, 'PiB'))
|
||||
self.assertEqual(330733097635021, size_to_byte(2.35, 'PiB'))
|
||||
self.assertEqual(330733097635020.8, size_to_byte(2.35, 'PiB'))
|
||||
|
||||
@@ -109,7 +109,7 @@ Description: GNU C compiler
|
||||
'section': 'web',
|
||||
'maintainer': 'Distro Dev <root@distro.com>',
|
||||
'architecture': 'amd64',
|
||||
'uncompressed size': 247463936,
|
||||
'uncompressed size': 247463936.0,
|
||||
'predepends': ('distro-system-adjustments (>= 2021.12.16)', ),
|
||||
'breaks': ('firefox-dbg (< 95.0.1+distro1+una)', 'firefox-dev (< 95.0.1+distro1+una)',
|
||||
'firefox-geckodriver (< 95.0.1+distro1+una)', 'firefox-mozsymbols (< 95.0.1+distro1+una)'),
|
||||
@@ -126,7 +126,7 @@ Description: GNU C compiler
|
||||
'section': 'devel',
|
||||
'maintainer': 'Distro Developers <distro-devel-discuss@lists.distro.com>',
|
||||
'architecture': 'amd64',
|
||||
'uncompressed size': 52429,
|
||||
'uncompressed size': 52428.8,
|
||||
'depends': ('cpp (= 4:9.3.0-1distro2)', 'gcc-9 (>= 9.3.0-3~)'),
|
||||
'recommends': ('libc6-dev | libc-dev', ),
|
||||
'suggests': ('gcc-multilib', 'make', 'manpages-dev', 'autoconf', 'automake',
|
||||
|
||||
@@ -20,4 +20,4 @@ class FlatpakTest(TestCase):
|
||||
SimpleProcess.assert_called_once()
|
||||
handle_simple.assert_called_once()
|
||||
|
||||
self.assertEqual({'org.xpto.Xnote': 4508877}, download_size)
|
||||
self.assertEqual({'org.xpto.Xnote': 4508876.8}, download_size)
|
||||
|
||||
Reference in New Issue
Block a user