From 4d44ce684f490b2195241db1d3ad8e5754ebba6c Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Wed, 4 Feb 2015 14:46:19 -0800 Subject: [PATCH] Support IEC suffix for sizes To be consistent with what the "aws s3 ls" command outputs. As per review feedback. --- awscli/customizations/s3/utils.py | 13 +++++++++++-- tests/unit/customizations/s3/test_utils.py | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/awscli/customizations/s3/utils.py b/awscli/customizations/s3/utils.py index a191235ce2a3..f54d184786bb 100644 --- a/awscli/customizations/s3/utils.py +++ b/awscli/customizations/s3/utils.py @@ -40,6 +40,10 @@ 'mb': 1024 ** 2, 'gb': 1024 ** 3, 'tb': 1024 ** 4, + 'kib': 1024, + 'mib': 1024 ** 2, + 'gib': 1024 ** 3, + 'tib': 1024 ** 4, } @@ -86,7 +90,12 @@ def human_readable_to_bytes(value): :returns: The converted value in bytes as an integer """ - suffix = value[-2:].lower() + value = value.lower() + if value[-2:] == 'ib': + # Assume IEC suffix. + suffix = value[-3:].lower() + else: + suffix = value[-2:].lower() has_size_identifier = ( len(value) >= 2 and suffix in SIZE_SUFFIX) if not has_size_identifier: @@ -96,7 +105,7 @@ def human_readable_to_bytes(value): raise ValueError("Invalid size value: %s" % value) else: multiplier = SIZE_SUFFIX[suffix] - return int(value[:-2]) * multiplier + return int(value[:-len(suffix)]) * multiplier class AppendFilter(argparse.Action): diff --git a/tests/unit/customizations/s3/test_utils.py b/tests/unit/customizations/s3/test_utils.py index 41c021ff22fc..cecb917568eb 100644 --- a/tests/unit/customizations/s3/test_utils.py +++ b/tests/unit/customizations/s3/test_utils.py @@ -56,6 +56,14 @@ def test_convert_human_readable_to_bytes(): yield _test_convert_human_readable_to_bytes, "1GB", 1024 ** 3 yield _test_convert_human_readable_to_bytes, "1TB", 1024 ** 4 + # Also because of the "ls" output for s3, we support + # the IEC "mebibyte" format (MiB). + yield _test_convert_human_readable_to_bytes, "1KiB", 1024 + yield _test_convert_human_readable_to_bytes, "1kib", 1024 + yield _test_convert_human_readable_to_bytes, "1MiB", 1024 ** 2 + yield _test_convert_human_readable_to_bytes, "1GiB", 1024 ** 3 + yield _test_convert_human_readable_to_bytes, "1TiB", 1024 ** 4 + def _test_convert_human_readable_to_bytes(size_str, expected): assert_equal(human_readable_to_bytes(size_str), expected)