From 7c70353564f09799c155c2308baac00a9605c0a2 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Wed, 18 May 2016 09:27:36 -0600 Subject: [PATCH] Add Remote._format_size() Signed-off-by: Zack Cerza --- teuthology/orchestra/remote.py | 11 +++++++++++ teuthology/orchestra/test/test_remote.py | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index 39f2d05a4c..2d7b6fe8d7 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -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]) diff --git a/teuthology/orchestra/test/test_remote.py b/teuthology/orchestra/test/test_remote.py index fd693ec059..e17f42400c 100644 --- a/teuthology/orchestra/test/test_remote.py +++ b/teuthology/orchestra/test/test_remote.py @@ -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' -- 2.39.5