Skip to content

Commit

Permalink
publish: print on failure, simplify logic
Browse files Browse the repository at this point in the history
Should help with cases like python-poetry#3397

The existing tests in tests/publishing/test_uploader.py should verify
that this doesn't cause regressions.
  • Loading branch information
hauntsaninja authored and 1nF0rmed committed Nov 15, 2021
1 parent e0a228f commit bd5eecc
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions poetry/publishing/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,42 +207,22 @@ def post_data(self, file: Path) -> Dict[str, Any]:

def _upload(
self, session: requests.Session, url: str, dry_run: Optional[bool] = False
) -> None:
try:
self._do_upload(session, url, dry_run)
except HTTPError as e:
if (
e.response.status_code == 400
and "was ever registered" in e.response.text
):
try:
self._register(session, url)
except HTTPError as e:
raise UploadError(e)

raise UploadError(e)

def _do_upload(
self, session: requests.Session, url: str, dry_run: Optional[bool] = False
) -> None:
for file in self.files:
if not file.is_file():
raise UploadError(
"Wheel or Tar files associated with the Project Package do not exist"
)

resp = self._upload_file(session, url, file, dry_run)

if not dry_run:
resp.raise_for_status()
self._upload_file(session, url, file, dry_run)

def _upload_file(
self,
session: requests.Session,
url: str,
file: Path,
dry_run: Optional[bool] = False,
) -> requests.Response:
) -> None:
from cleo.ui.progress_bar import ProgressBar

data = self.post_data(file)
Expand Down Expand Up @@ -293,6 +273,11 @@ def _upload_file(
"Redirects are not supported. "
"Is the URL missing a trailing slash?"
)
elif resp.status_code == 400 and "was ever registered" in resp.text:
self._register(session, url)
resp.raise_for_status()
else:
resp.raise_for_status()
except (requests.ConnectionError, requests.HTTPError) as e:
if self._io.output.is_decorated():
self._io.overwrite(
Expand All @@ -302,8 +287,6 @@ def _upload_file(
finally:
self._io.write_line("")

return resp

def _register(self, session: requests.Session, url: str) -> requests.Response:
"""
Register a package to a repository.
Expand Down

0 comments on commit bd5eecc

Please sign in to comment.