Skip to content

Commit

Permalink
Use Rich for print() output (#878)
Browse files Browse the repository at this point in the history
* Use rich for print

* Log warnings for retries

* Log warning for gpg

* Log warning for skipped packages
  • Loading branch information
bhrutledge committed Feb 27, 2022
1 parent 4e978af commit f69d4b7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
22 changes: 8 additions & 14 deletions tests/test_repository.py
Expand Up @@ -230,7 +230,7 @@ def dictfunc():
default_repo.upload(package)


def test_upload_retry(tmpdir, default_repo, capsys):
def test_upload_retry(tmpdir, default_repo, caplog):
"""Print retry messages when the upload response indicates a server error."""
default_repo.disable_progress_bar = True

Expand All @@ -254,33 +254,27 @@ def test_upload_retry(tmpdir, default_repo, capsys):
# Upload with default max_redirects of 5
default_repo.upload(package)

msg = [
assert caplog.messages == [
(
"Uploading fake.whl\n"
'Received "500: Internal server error" '
f"Package upload appears to have failed. Retry {i} of 5"
'Received "500: Internal server error"\n'
f"Package upload appears to have failed. Retry {i} of 5."
)
for i in range(1, 6)
]

captured = capsys.readouterr()
assert captured.out == "\n".join(msg) + "\n"
caplog.clear()

# Upload with custom max_redirects of 3
default_repo.upload(package, 3)

msg = [
assert caplog.messages == [
(
"Uploading fake.whl\n"
'Received "500: Internal server error" '
f"Package upload appears to have failed. Retry {i} of 3"
'Received "500: Internal server error"\n'
f"Package upload appears to have failed. Retry {i} of 3."
)
for i in range(1, 4)
]

captured = capsys.readouterr()
assert captured.out == "\n".join(msg) + "\n"


@pytest.mark.parametrize(
"package_meta,repository_url,release_urls",
Expand Down
2 changes: 2 additions & 0 deletions twine/commands/register.py
Expand Up @@ -16,6 +16,8 @@
import os.path
from typing import List, cast

from rich import print

from twine import exceptions
from twine import package as package_file
from twine import settings
Expand Down
11 changes: 6 additions & 5 deletions twine/commands/upload.py
Expand Up @@ -18,6 +18,7 @@
from typing import Dict, List, cast

import requests
from rich import print

from twine import commands
from twine import exceptions
Expand Down Expand Up @@ -127,15 +128,15 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
uploaded_packages = []

for package in packages_to_upload:
skip_message = " Skipping {} because it appears to already exist".format(
package.basefilename
skip_message = (
f"Skipping {package.basefilename} because it appears to already exist"
)

# Note: The skip_existing check *needs* to be first, because otherwise
# we're going to generate extra HTTP requests against a hardcoded
# URL for no reason.
if upload_settings.skip_existing and repository.package_is_uploaded(package):
print(skip_message)
logger.warning(skip_message)
continue

resp = repository.upload(package)
Expand All @@ -154,7 +155,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
)

if skip_upload(resp, upload_settings.skip_existing, package):
print(skip_message)
logger.warning(skip_message)
continue

utils.check_status_code(resp, upload_settings.verbose)
Expand All @@ -163,7 +164,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:

release_urls = repository.release_urls(uploaded_packages)
if release_urls:
print("\nView at:")
print("\n[green]View at:")
for url in release_urls:
print(url)

Expand Down
11 changes: 7 additions & 4 deletions twine/package.py
Expand Up @@ -13,13 +13,15 @@
# limitations under the License.
import hashlib
import io
import logging
import os
import re
import subprocess
from typing import Dict, NamedTuple, Optional, Sequence, Tuple, Union

import importlib_metadata
import pkginfo
from rich import print

from twine import exceptions
from twine import wheel
Expand All @@ -43,6 +45,8 @@

MetadataValue = Union[str, Sequence[str]]

logger = logging.getLogger(__name__)


def _safe_name(name: str) -> str:
"""Convert an arbitrary string to a standard distribution name.
Expand Down Expand Up @@ -222,16 +226,15 @@ def run_gpg(cls, gpg_args: Tuple[str, ...]) -> None:
except FileNotFoundError:
if gpg_args[0] != "gpg":
raise exceptions.InvalidSigningExecutable(
"{} executable not available.".format(gpg_args[0])
f"{gpg_args[0]} executable not available."
)

print("gpg executable not available. Attempting fallback to gpg2.")
logger.warning("gpg executable not available. Attempting fallback to gpg2.")
try:
subprocess.check_call(("gpg2",) + gpg_args[1:])
except FileNotFoundError:
print("gpg2 executable not available.")
raise exceptions.InvalidSigningExecutable(
"'gpg' or 'gpg2' executables not available. "
"'gpg' or 'gpg2' executables not available.\n"
"Try installing one of these or specifying an executable "
"with the --sign-with flag."
)
Expand Down
14 changes: 5 additions & 9 deletions twine/repository.py
Expand Up @@ -21,6 +21,7 @@
import urllib3
from requests import adapters
from requests_toolbelt.utils import user_agent
from rich import print

import twine
from twine import package as package_file
Expand Down Expand Up @@ -192,15 +193,10 @@ def upload(
return resp
if 500 <= resp.status_code < 600:
number_of_redirects += 1
print(
'Received "{status_code}: {reason}" Package upload '
"appears to have failed. Retry {retry} of "
"{max_redirects}".format(
status_code=resp.status_code,
reason=resp.reason,
retry=number_of_redirects,
max_redirects=max_redirects,
)
logger.warning(
f'Received "{resp.status_code}: {resp.reason}"'
"\nPackage upload appears to have failed."
f" Retry {number_of_redirects} of {max_redirects}."
)
else:
return resp
Expand Down

0 comments on commit f69d4b7

Please sign in to comment.