]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Add Remote._format_size()
authorZack Cerza <zack@redhat.com>
Wed, 18 May 2016 15:27:36 +0000 (09:27 -0600)
committerZack Cerza <zack@redhat.com>
Wed, 18 May 2016 17:45:37 +0000 (11:45 -0600)
Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/orchestra/remote.py
teuthology/orchestra/test/test_remote.py

index 39f2d05a4ced31348f8f003e9adaceda4abcb8c1..2d7b6fe8d7463e6814b648c12ac732e9bdbdc478 100644 (file)
@@ -283,6 +283,17 @@ class Remote(object):
         with self._sftp_open_file(remote_path) as f:
             return f.stat().st_size
 
+    @staticmethod
+    def _format_size(file_size):
+        """
+        Given a file_size in bytes, returns a human-readable representation.
+        """
+        for unit in ('B', 'KB', 'MB', 'GB', 'TB'):
+            if abs(file_size) < 1024.0:
+                break
+            file_size = file_size / 1024.0
+        return "{:3.0f}{}".format(file_size, unit)
+
     def remove(self, path):
         self.run(args=['rm', '-fr', path])
 
index fd693ec059713c084e0c4be334d534070272d755..e17f42400c6663f3575f7eb038ea9664e065931c 100644 (file)
@@ -176,3 +176,10 @@ class TestRemote(object):
             rem = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh)
             assert rem._sftp_get_size('/fake/file') == 42
 
+    def test_format_size(self):
+        assert remote.Remote._format_size(1023).strip() == '1023B'
+        assert remote.Remote._format_size(1024).strip() == '1KB'
+        assert remote.Remote._format_size(1024**2).strip() == '1MB'
+        assert remote.Remote._format_size(1024**5).strip() == '1TB'
+        assert remote.Remote._format_size(1021112).strip() == '997KB'
+        assert remote.Remote._format_size(1021112**2).strip() == '971GB'