{"thread":{"id":"57178","subject":"[PATCH v3 0/2] git-p4: improve formatting of numeric values","startedAt":"2022-01-04T12:43:35Z","lastAt":"2022-01-04T12:43:40Z","messageCount":3,"participants":["Joel Holdsworth"],"isPatch":true,"patchVersion":3,"patchTotal":2},"messages":[{"id":"445398","messageId":"20220104124307.2524-1-jholdsworth@nvidia.com","threadId":"57178","inReplyTo":null,"subject":"[PATCH v3 0/2] git-p4: improve formatting of numeric values","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2022-01-04T12:43:05Z","receivedAt":"2022-01-04T12:43:35Z","isPatch":true,"sender":{"key":"jholdsworth@nvidia.com","avatar":"https://avatars.githubusercontent.com/u/1449493?v=4"},"body":"This patch-set contains patches designed to improve the way in which the git-p4\nscript reports files sizes and percentage values in an effort to improve the\nreadability of the logged messages.\n\nThis third version of the patch-set adds an ack from Luke Diamand to\npatch 2/2.\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 | 27 +++++++++++++++++++++------\n 1 file changed, 21 insertions(+), 6 deletions(-)\n\n-- \n2.34.1\n\n"},{"id":"445399","messageId":"20220104124307.2524-3-jholdsworth@nvidia.com","threadId":"57178","inReplyTo":"20220104124307.2524-1-jholdsworth@nvidia.com","subject":"[PATCH v3 2/2] git-p4: show progress as an integer","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2022-01-04T12:43:07Z","receivedAt":"2022-01-04T12:43:37Z","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>\nAcked-by: Luke Diamand <luke@diamand.org>\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 74834736fa..a625077b83 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -3637,7 +3637,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":"445400","messageId":"20220104124307.2524-2-jholdsworth@nvidia.com","threadId":"57178","inReplyTo":"20220104124307.2524-1-jholdsworth@nvidia.com","subject":"[PATCH v3 1/2] git-p4: print size values in appropriate units","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2022-01-04T12:43:06Z","receivedAt":"2022-01-04T12:43:40Z","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 | 24 +++++++++++++++++++-----\n 1 file changed, 19 insertions(+), 5 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex 2b4500226a..74834736fa 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -56,6 +56,18 @@\n \n p4_access_checked = False\n \n+def format_size_human_readable(num):\n+    \"\"\" Returns a number of units (typically bytes) formatted as a human-readable\n+        string.\n+    \"\"\"\n+    if num < 1024:\n+        return '{:d} B'.format(num)\n+    for unit in [\"Ki\", \"Mi\", \"Gi\", \"Ti\", \"Pi\", \"Ei\", \"Zi\"]:\n+        num /= 1024.0\n+        if num < 1024.0:\n+            return \"{:3.1f} {}B\".format(num, unit)\n+    return \"{:.1f} YiB\".format(num)\n+\n def p4_build_cmd(cmd):\n     \"\"\"Build a suitable p4 command line.\n \n@@ -2966,7 +2978,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)\\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 +3077,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.' % (\n+                    os.getcwd(), format_size_human_readable(required_bytes))\n \n         if err:\n             f = None\n@@ -3110,7 +3122,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)' % (\n+                    self.stream_file['depotFile'], progress,\n+                    format_size_human_readable(size)))\n                 sys.stdout.flush()\n \n         self.stream_have_file_info = True\n-- \n2.34.1\n\n"}]}