Skip to content

Commit

Permalink
Prints a list of dists that are attempting to be uploaded and their f…
Browse files Browse the repository at this point in the history
…ile 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
  • Loading branch information
Vikram Jayanthi committed Jun 10, 2020
1 parent 89aab15 commit 3954ad9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_upload.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Expand Up @@ -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
5 changes: 5 additions & 0 deletions twine/commands/upload.py
Expand Up @@ -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 = []
Expand Down
12 changes: 12 additions & 0 deletions twine/utils.py
Expand Up @@ -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.
Expand Down

0 comments on commit 3954ad9

Please sign in to comment.