{"thread":{"id":"57108","subject":"[PATCH 0/2] git-p4: improve formatting of numeric values","startedAt":"2021-12-17T15:00:17Z","lastAt":"2021-12-17T20:39:56Z","messageCount":5,"participants":["Joel Holdsworth","Andrew Oakley"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"444364","messageId":"20211217145949.2229886-1-jholdsworth@nvidia.com","threadId":"57108","inReplyTo":null,"subject":"[PATCH 0/2] git-p4: improve formatting of numeric values","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2021-12-17T14:59:47Z","receivedAt":"2021-12-17T15:00:17Z","isPatch":true,"sender":{"key":"jholdsworth@nvidia.com","avatar":"https://avatars.githubusercontent.com/u/1449493?v=4"},"body":"This patch-set contains patches designed to improvement the way in which\nthe git-p4 script reports files sizes and percentage values in an effort\nto improve the readability of the logged messages.\n\nJoel Holdsworth (2):\n  git-p4: print size values in appropriate units\n  git-p4: show progress as an integer\n\n git-p4.py | 25 +++++++++++++++++++------\n 1 file changed, 19 insertions(+), 6 deletions(-)\n\n-- \n2.34.1\n\n"},{"id":"444365","messageId":"20211217145949.2229886-2-jholdsworth@nvidia.com","threadId":"57108","inReplyTo":"20211217145949.2229886-1-jholdsworth@nvidia.com","subject":"[PATCH 1/2] git-p4: print size values in appropriate units","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2021-12-17T14:59:48Z","receivedAt":"2021-12-17T15:00:18Z","isPatch":true,"sender":{"key":"jholdsworth@nvidia.com","avatar":"https://avatars.githubusercontent.com/u/1449493?v=4"},"body":"The git-p4 script reports file sizes in various log messages.\nPreviously, in each case the script would print them as the number of\nbytes divided by 1048576 i.e. the size in mebibytes, rounded down to an\ninteger.  This resulted in small files being described as having a size\nof \"0 MB\".\n\nThis patch replaces the existing behaviour with a new helper function:\nformat_size_human_readable, which takes a number of bytes (or any other\nquantity), and computes the appropriate prefix to use: none, Ki, Mi, Gi,\nTi, Pi, Ei, Zi, Yi.\n\nFor example, a size of 123456 will now be printed as \"120.6 KiB\" greatly\nimproving the readability of the log output.\n\nLarge valued prefixes such as pebi, exbi, zebi and yobi are included for\ncompleteness, though they not expected to appear in any real-world\nPerforce repository!\n\nSigned-off-by: Joel Holdsworth <jholdsworth@nvidia.com>\n---\n git-p4.py | 22 +++++++++++++++++-----\n 1 file changed, 17 insertions(+), 5 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex 2b4500226a..8c1130cb8f 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -56,6 +56,16 @@\n \n p4_access_checked = False\n \n+def format_size_human_readable(num, suffix=\"B\"):\n+    \"\"\" Returns a number of units (typically bytes) formatted as a human-readable\n+        string.\n+    \"\"\"\n+    for unit in [\"\", \"Ki\", \"Mi\", \"Gi\", \"Ti\", \"Pi\", \"Ei\", \"Zi\"]:\n+        if abs(num) < 1024.0:\n+            return \"{:3.1f} {}{}\".format(num, unit, suffix)\n+        num /= 1024.0\n+    return \"{:.1f} Yi{}\".format(num, suffix)\n+\n def p4_build_cmd(cmd):\n     \"\"\"Build a suitable p4 command line.\n \n@@ -2966,7 +2976,8 @@ def streamOneP4File(self, file, contents):\n                 size = int(self.stream_file['fileSize'])\n             else:\n                 size = 0 # deleted files don't get a fileSize apparently\n-            sys.stdout.write('\\r%s --> %s (%i MB)\\n' % (file_path, relPath, size/1024/1024))\n+            sys.stdout.write('\\r%s --> %s (%s MB)\\n' % (\n+                file_path, relPath, format_size_human_readable(size)))\n             sys.stdout.flush()\n \n         (type_base, type_mods) = split_p4_type(file[\"type\"])\n@@ -3064,9 +3075,8 @@ def streamP4FilesCb(self, marshalled):\n         if not err and 'fileSize' in self.stream_file:\n             required_bytes = int((4 * int(self.stream_file[\"fileSize\"])) - calcDiskFree())\n             if required_bytes > 0:\n-                err = 'Not enough space left on %s! Free at least %i MB.' % (\n-                    os.getcwd(), required_bytes/1024/1024\n-                )\n+                err = 'Not enough space left on %s! Free at least %s MB.' % (\n+                    os.getcwd(), format_size_human_readable(required_bytes))\n \n         if err:\n             f = None\n@@ -3110,7 +3120,9 @@ def streamP4FilesCb(self, marshalled):\n             size = int(self.stream_file[\"fileSize\"])\n             if size > 0:\n                 progress = 100*self.stream_file['streamContentSize']/size\n-                sys.stdout.write('\\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))\n+                sys.stdout.write('\\r%s %d%% (%s MB)' % (\n+                    self.stream_file['depotFile'], progress,\n+                    format_size_human_readable(required_bytes)))\n                 sys.stdout.flush()\n \n         self.stream_have_file_info = True\n-- \n2.34.1\n\n"},{"id":"444366","messageId":"20211217145949.2229886-3-jholdsworth@nvidia.com","threadId":"57108","inReplyTo":"20211217145949.2229886-1-jholdsworth@nvidia.com","subject":"[PATCH 2/2] git-p4: show progress as an integer","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2021-12-17T14:59:49Z","receivedAt":"2021-12-17T15:00:18Z","isPatch":true,"sender":{"key":"jholdsworth@nvidia.com","avatar":"https://avatars.githubusercontent.com/u/1449493?v=4"},"body":"When importing files from Perforce, git-p4 periodically logs the\nprogress of file transfers as a percentage. However, the value is\nprinted as a float with an excessive number of decimal places.\n\nFor example a typical update might contain the following message:\n\nImporting revision 12345 (26.199617677553135%)\n\nThis patch simply rounds the value down to the nearest integer\npercentage value, greatly improving readability.\n\nSigned-off-by: Joel Holdsworth <jholdsworth@nvidia.com>\n---\n git-p4.py | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex 8c1130cb8f..d1919c4e12 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -3635,7 +3635,8 @@ def importChanges(self, changes, origin_revision=0):\n             self.updateOptionDict(description)\n \n             if not self.silent:\n-                sys.stdout.write(\"\\rImporting revision %s (%s%%)\" % (change, cnt * 100 / len(changes)))\n+                sys.stdout.write(\"\\rImporting revision %s (%d%%)\" % (\n+                    change, (cnt * 100) // len(changes)))\n                 sys.stdout.flush()\n             cnt = cnt + 1\n \n-- \n2.34.1\n\n"},{"id":"444381","messageId":"20211217165627.49e931d6@ado-tr.dyn.home.arpa","threadId":"57108","inReplyTo":"20211217145949.2229886-2-jholdsworth@nvidia.com","subject":"Re: [PATCH 1/2] git-p4: print size values in appropriate units","fromName":"Andrew Oakley","fromEmail":"andrew@adoakley.name","sentAt":"2021-12-17T16:56:27Z","receivedAt":"2021-12-17T16:56:33Z","isPatch":true,"sender":{"key":"andrew@adoakley.name","avatar":"https://avatars.githubusercontent.com/u/1107440?v=4"},"body":"On Fri, 17 Dec 2021 14:59:48 +0000\nJoel Holdsworth <jholdsworth@nvidia.com> wrote:\n\n> The git-p4 script reports file sizes in various log messages.\n> Previously, in each case the script would print them as the number of\n> bytes divided by 1048576 i.e. the size in mebibytes, rounded down to\n> an integer.  This resulted in small files being described as having a\n> size of \"0 MB\".\n> \n> This patch replaces the existing behaviour with a new helper function:\n> format_size_human_readable, which takes a number of bytes (or any\n> other quantity), and computes the appropriate prefix to use: none,\n> Ki, Mi, Gi, Ti, Pi, Ei, Zi, Yi.\n> \n> For example, a size of 123456 will now be printed as \"120.6 KiB\"\n> greatly improving the readability of the log output.\n> \n> Large valued prefixes such as pebi, exbi, zebi and yobi are included\n> for completeness, though they not expected to appear in any real-world\n> Perforce repository!\n> \n> Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>\n> ---\n>  git-p4.py | 22 +++++++++++++++++-----\n>  1 file changed, 17 insertions(+), 5 deletions(-)\n> \n> diff --git a/git-p4.py b/git-p4.py\n> index 2b4500226a..8c1130cb8f 100755\n> --- a/git-p4.py\n> +++ b/git-p4.py\n> @@ -56,6 +56,16 @@\n>  \n>  p4_access_checked = False\n>  \n> +def format_size_human_readable(num, suffix=\"B\"):\n> +    \"\"\" Returns a number of units (typically bytes) formatted as a\n> human-readable\n> +        string.\n> +    \"\"\"\n> +    for unit in [\"\", \"Ki\", \"Mi\", \"Gi\", \"Ti\", \"Pi\", \"Ei\", \"Zi\"]:\n> +        if abs(num) < 1024.0:\n> +            return \"{:3.1f} {}{}\".format(num, unit, suffix)\n> +        num /= 1024.0\n> +    return \"{:.1f} Yi{}\".format(num, suffix)\n\nIf it's always bytes, why keep the suffix separate?\n\n> +\n>  def p4_build_cmd(cmd):\n>      \"\"\"Build a suitable p4 command line.\n>  \n> @@ -2966,7 +2976,8 @@ def streamOneP4File(self, file, contents):\n>                  size = int(self.stream_file['fileSize'])\n>              else:\n>                  size = 0 # deleted files don't get a fileSize\n> apparently\n> -            sys.stdout.write('\\r%s --> %s (%i MB)\\n' % (file_path,\n> relPath, size/1024/1024))\n> +            sys.stdout.write('\\r%s --> %s (%s MB)\\n' % (\n> +                file_path, relPath,\n> format_size_human_readable(size))) sys.stdout.flush()\n\nYou've left the \"MB\" in.  The same thing happens elsewhere.\n"},{"id":"444390","messageId":"BN8PR12MB3361AD895447CF05A034D68BC8789@BN8PR12MB3361.namprd12.prod.outlook.com","threadId":"57108","inReplyTo":"20211217165627.49e931d6@ado-tr.dyn.home.arpa","subject":"RE: [PATCH 1/2] git-p4: print size values in appropriate units","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2021-12-17T20:39:53Z","receivedAt":"2021-12-17T20:39:56Z","isPatch":true,"sender":{"key":"jholdsworth@nvidia.com","avatar":"https://avatars.githubusercontent.com/u/1449493?v=4"},"body":"> If it's always bytes, why keep the suffix separate?\n\n> You've left the \"MB\" in.  The same thing happens elsewhere.\n\nBoth good points. I've resubmitted the patch set with the suggested amendments.\n\nJoel\n"}]}