From 4a370ac17a19703e73e168b0d7477dcf2b28aa99 Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Jul 21 2017 06:04:56 +0000 Subject: changelog: Format all sizes for readability Even size change for a package should be formatted with bigger unit than bytes. The formatting function is also updated to not print decimal places when the unit used is actually B (as at that point there can never be anything else than .00). Fixes: https://pagure.io/compose-utils/issue/50 Signed-off-by: Lubomír Sedlář --- diff --git a/compose_utils/changelog.py b/compose_utils/changelog.py index ecc4732..b378183 100644 --- a/compose_utils/changelog.py +++ b/compose_utils/changelog.py @@ -24,13 +24,15 @@ def formatsize(size): units = ('KiB', 'MiB', 'GiB', 'TiB') size = float(size) chosen = 'B' + prec = 0 for unit in units: if size > 1024: + prec = 2 size = size / 1024 chosen = unit else: break - return '%.2f %s' % (size, chosen) + return '{0:.{1}f} {2}'.format(size, prec, chosen) def to_utf8(text): @@ -455,7 +457,7 @@ class ComposeChangelog(object): result.append("Summary: %s" % i["summary"]) if not shorten: result.append("RPMs: %s" % " ".join(sorted(i["rpms"]))) - result.append("Size: %s bytes" % i["size"]) + result.append("Size: %s" % formatsize(i["size"])) result.append("") result.append("") @@ -465,7 +467,7 @@ class ComposeChangelog(object): if not shorten: result.append("Summary: %s" % i["summary"]) result.append("RPMs: %s" % " ".join(sorted(i["rpms"]))) - result.append("Size: %s bytes" % i["size"]) + result.append("Size: %s" % formatsize(i["size"])) result.append("") result.append("") @@ -481,8 +483,8 @@ class ComposeChangelog(object): if i["dropped_rpms"]: result.append("Dropped RPMs: %s" % " ".join(i["dropped_rpms"])) if not shorten: - result.append("Size: %s bytes" % i["size"]) - result.append("Size change: %s bytes" % i["size_change"]) + result.append("Size: %s" % formatsize(i["size"])) + result.append("Size change: %s" % formatsize(i["size_change"])) if i["changelog"]: result.append("Changelog:") for entry in i["changelog"]: @@ -504,8 +506,8 @@ class ComposeChangelog(object): if i["dropped_rpms"]: result.append("Dropped RPMs: %s" % " ".join(i["dropped_rpms"])) if not shorten: - result.append("Size: %s bytes" % i["size"]) - result.append("Size change: %s bytes" % i["size_change"]) + result.append("Size: %s" % formatsize(i["size"])) + result.append("Size change: %s" % formatsize(i["size_change"])) if i["changelog"]: result.append("Changelog:") for entry in i["changelog"]: diff --git a/tests/fixtures/summary-full.txt b/tests/fixtures/summary-full.txt index b4236b4..a3e02e3 100644 --- a/tests/fixtures/summary-full.txt +++ b/tests/fixtures/summary-full.txt @@ -6,10 +6,10 @@ Dropped packages: 1 Upgraded packages: 1 Downgraded packages: 1 -Size of added packages: 10.00 B -Size of dropped packages: 15.00 B -Size of upgraded packages: 20.00 B -Size of downgraded packages: 25.00 B +Size of added packages: 10 B +Size of dropped packages: 15 B +Size of upgraded packages: 20 B +Size of downgraded packages: 25 B -Size change of upgraded packages: 4.00 B -Size change of downgraded packages: 6.00 B +Size change of upgraded packages: 4 B +Size change of downgraded packages: 6 B diff --git a/tests/fixtures/verbose-full.txt b/tests/fixtures/verbose-full.txt index 36479ad..ab6e1c7 100644 --- a/tests/fixtures/verbose-full.txt +++ b/tests/fixtures/verbose-full.txt @@ -13,14 +13,14 @@ Path: images/MATE-dvd.iso Package: compose-utils-0.1.9-1.fc25 Summary: compose-utils summary RPMs: compose-utils -Size: 11 bytes +Size: 11 B ===== DROPPED PACKAGES ===== Package: fedpkg-1.22-1.fc25 Summary: fedpkg summary RPMs: fedpkg -Size: 12 bytes +Size: 12 B ===== UPGRADED PACKAGES ===== @@ -30,8 +30,8 @@ Summary: pungi summary RPMs: pungi pungi-utils Added RPMs: pungi-utils Dropped RPMs: pungi-ostree -Size: 13 bytes -Size change: 5 bytes +Size: 13 B +Size change: 5 B Changelog: * Pungi changed. * A lot. @@ -45,8 +45,8 @@ Summary: rpkg summary RPMs: pyrpkg Added RPMs: pyrpkg Dropped RPMs: rpkg -Size: 14 bytes -Size change: 6 bytes +Size: 14 B +Size change: 6 B Changelog: Rpkg changed as well. diff --git a/tests/fixtures/verbose-short.txt b/tests/fixtures/verbose-short.txt index ff2b20f..5f20589 100644 --- a/tests/fixtures/verbose-short.txt +++ b/tests/fixtures/verbose-short.txt @@ -12,12 +12,12 @@ Path: images/MATE-dvd.iso ===== ADDED PACKAGES ===== Package: compose-utils-0.1.9-1.fc25 Summary: compose-utils summary -Size: 11 bytes +Size: 11 B ===== DROPPED PACKAGES ===== Package: fedpkg-1.22-1.fc25 -Size: 12 bytes +Size: 12 B ===== UPGRADED PACKAGES ===== @@ -25,7 +25,7 @@ Package: pungi-4.1.10-1.fc25 Old package: pungi-4.1.9-1.fc25 Added RPMs: pungi-utils Dropped RPMs: pungi-ostree -Size change: 5 bytes +Size change: 5 B Changelog: * Pungi changed. * A lot. @@ -37,7 +37,7 @@ Package: rpkg-1.41-1.fc25 Old package: rpkg-1.41-2.fc25 Added RPMs: pyrpkg Dropped RPMs: rpkg -Size change: 6 bytes +Size change: 6 B Changelog: Rpkg changed as well.