Permalink
Browse files

Format the sizes and dates.

  • Loading branch information...
hmalphettes committed Nov 20, 2017
1 parent d1ab58e commit c4dd2b5ad1fde9912a284d0ae69d1cdd45597190
Showing with 56 additions and 17 deletions.
  1. +2 −0 .coveragerc
  2. +2 −1 .gitignore
  3. +0 −1 .travis.yml
  4. +6 −0 Makefile
  5. +0 −2 codecov.yml
  6. +3 −1 requirements-dev.txt
  7. +19 −11 s3_storage_analyser.py
  8. +24 −1 test_s3_storage_analyzer.py
View
@@ -0,0 +1,2 @@
[run]
omit=setup.py,test_s3_storage_analyzer.py
View
@@ -3,4 +3,5 @@ __pycache__
.cache
.vscode
*.pyc
.env
.env
.coverage*
View
@@ -4,7 +4,6 @@ python:
install:
- pip install -r requirements-dev.txt
- pip install codecov pytest-cov
script: pytest -s --cov=./
after_success: codecov
View
@@ -1,5 +1,11 @@
test:
pytest -s
cov: clean
pytest --cov=./
clean:
@rm .coverage .coverage.*
install:
pip install -r requirements-dev.txt
View

This file was deleted.

Oops, something went wrong.
View
@@ -1,3 +1,5 @@
-r requirements.txt
moto>=1.1.24
pytest>=3.2.5
pytest>=3.2.5
codecov>=2.5.1
pytest-cov>=2.5.1
View
@@ -124,35 +124,43 @@ def _list_objects(**kwargs):
for i in _list_objects(**kwargs):
yield i
def _format_bucket(bucket, args):
def _format_bucket(bucket, unit='MB'):
return [
bucket['Name'],
bucket['CreationDate'],
bucket['LastModified'],
convert_bytes(bucket['TotalSize'], args.unit),
bucket['CreationDate'].isoformat('T', 'seconds'),
bucket['LastModified'].isoformat('T', 'seconds'),
convert_bytes(bucket['TotalSize'], unit),
bucket['TotalFiles']
]
def _format_buckets(buckets, args):
"""Format a list of buckets as dictionary into a list of arrays for tabulate"""
def _format_buckets(buckets, unit='MB'):
"""Format a list of buckets as dictionary into a list of arrays
ready to be tabulated"""
headers = [
'Name',
'Created',
'Last Modified',
f'Total size {args.unit}',
f'Total size {unit}',
'Total files'
]
return {
'headers': headers,
'values': [_format_bucket(b, args) for b in buckets]
'values': [_format_bucket(b, unit=unit) for b in buckets]
}
def report(prefix=None, unit='MB', tablefmt='plain'):
"""Generate the tabulated report"""
buckets = _analyse_buckets(prefix=prefix)
formatted = _format_buckets(buckets, unit=unit)
return tabulate.tabulate(
formatted['values'],
headers=formatted['headers'],
tablefmt=tablefmt)
def main():
"""CLI entry point"""
args = parse_args()
buckets = _analyse_buckets(prefix=args.prefix)
formatted = _format_buckets(buckets, args)
print(tabulate.tabulate(formatted['values'], headers=formatted['headers'], tablefmt='plain'))
print(report(prefix=args.prefix, unit=args.unit))
if __name__ == "__main__":
main()
@@ -3,8 +3,10 @@
"""
from pprint import pprint
from s3_storage_analyser import _get_s3_client, _list_buckets
from datetime import datetime
from s3_storage_analyser import _get_s3_client, _list_buckets, _format_buckets
from s3_storage_analyser import convert_bytes, traverse_bucket, fetch_bucket_info
from s3_storage_analyser import report
from moto import mock_s3
def test_convert_bytes():
@@ -71,3 +73,24 @@ def test_buckets_filter():
bucket_list = _list_buckets(prefix='s3://a')
assert len(bucket_list) == 2
def test_format_buckets():
"""Format and tabulate the buckets"""
buckets = [{
'Name': 'hm.samples',
'CreationDate': datetime.now(),
'LastModified': datetime.now(),
'TotalSize': 1048576,
'TotalFiles': 6
}]
formatted = _format_buckets(buckets)
pprint(formatted)
@mock_s3
def test_report():
"""Test the tabulated report"""
_setup_s3()
_report = report(unit='KB')
lines = _report.splitlines()
assert ' Total size KB ' in lines[0]
assert ' 0.02 ' in lines[1]

0 comments on commit c4dd2b5

Please sign in to comment.