From 3954ad972e062e20f3b132789386ba6d3994c5f8 Mon Sep 17 00:00:00 2001 From: Vikram Jayanthi Date: Fri, 5 Jun 2020 12:49:21 -0700 Subject: [PATCH] Prints a list of dists that are attempting to be uploaded and their file sizes, cleaning commits Prints succesfully and wrote test prints all packages its attempting to upload before upload Removed extra whitespaces from test_upload Linting + formatting tests Added New line after all package names have been printed Linting + formatting tests Added New line after all package names have been printed Prints a list of uploaded packages, cleaning up commits Prints succesfully and wrote test prints all packages its attempting to upload before upload Removed extra whitespaces from test_upload Linting + formatting tests Added New line after all package names have been printed Prints the path of the distribution Added file size Linting, formatting, typing Removed comments Added get_file_size test and formatting refactored and added tests, changed upload and get_file_size --- tests/test_upload.py | 26 ++++++++++++++++++++++++++ tests/test_utils.py | 16 ++++++++++++++++ twine/commands/upload.py | 5 +++++ twine/utils.py | 12 ++++++++++++ 4 files changed, 59 insertions(+) diff --git a/tests/test_upload.py b/tests/test_upload.py index 42939f93..b30fe583 100644 --- a/tests/test_upload.py +++ b/tests/test_upload.py @@ -18,6 +18,7 @@ from twine import cli from twine import exceptions from twine import package as package_file +from twine import utils from twine.commands import upload from . import helpers @@ -72,6 +73,31 @@ def test_successs_prints_release_urls(upload_settings, stub_repository, capsys): assert captured.out.count(NEW_RELEASE_URL) == 1 +def test_successs_prints_uploaded_package_names_if_verbose(upload_settings, capsys): + """Print the path and file size of each distribution attempting to be uploaded.""" + dists_to_upload = [ + helpers.WHEEL_FIXTURE, + helpers.SDIST_FIXTURE, + helpers.NEW_SDIST_FIXTURE, + helpers.NEW_WHEEL_FIXTURE, + ] + + sizes_of_dists_to_upload = [utils.get_file_size(dist) for dist in dists_to_upload] + + upload_settings.verbose = True + + result = upload.upload(upload_settings, dists_to_upload) + + assert result is None + + captured = capsys.readouterr() + + for dist_to_upload in dists_to_upload: + assert captured.out.count(dist_to_upload) == 1 + for size_of_dist_to_upload in sizes_of_dists_to_upload: + assert captured.out.count(size_of_dist_to_upload) == 1 + + def test_success_with_pre_signed_distribution(upload_settings, stub_repository): """Adds GPG signature provided by user to uploaded package.""" # Upload a pre-signed distribution diff --git a/tests/test_utils.py b/tests/test_utils.py index 55d96706..b8ae34ca 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -313,3 +313,19 @@ def test_check_status_code_for_missing_status_code(capsys, repo_url): captured = capsys.readouterr() assert captured.out == "NOTE: Try --verbose to see response content.\n" + + +@pytest.mark.parametrize( + ("filename, expected"), + [ + (helpers.WHEEL_FIXTURE, "15.4 KB"), + (helpers.SDIST_FIXTURE, "20.8 KB"), + (helpers.NEW_SDIST_FIXTURE, "26.1 KB"), + (helpers.NEW_WHEEL_FIXTURE, "21.9 KB"), + ], +) +def test_get_file_size(filename, expected): + """Compare the size of a file to the expected value.""" + file_size = utils.get_file_size(filename) + + assert file_size == expected diff --git a/twine/commands/upload.py b/twine/commands/upload.py index a37e8bd0..ef7ff67f 100644 --- a/twine/commands/upload.py +++ b/twine/commands/upload.py @@ -60,6 +60,11 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None: repository_url = cast(str, upload_settings.repository_config["repository"]) print(f"Uploading distributions to {repository_url}") + if upload_settings.verbose: + for filename in uploads: + file_size = utils.get_file_size(filename) + print(f" {filename} ({file_size})") + print("\n") repository = upload_settings.create_repository() uploaded_packages = [] diff --git a/twine/utils.py b/twine/utils.py index f49360de..d859904e 100644 --- a/twine/utils.py +++ b/twine/utils.py @@ -159,6 +159,18 @@ def normalize_repository_url(url: str) -> str: return urlunparse(parsed) +def get_file_size(filename: str) -> str: + """Return the size of a file, in KB if < .1 MB.""" + file_size = os.path.getsize(filename) / 1024 + size_unit = "KB" + + if file_size > 1024: + file_size = file_size / 1024 + size_unit = "MB" + + return f"{file_size:.1f} {size_unit}" + + def check_status_code(response: requests.Response, verbose: bool) -> None: """Generate a helpful message based on the response from the repository.