Skip to content

Commit

Permalink
Provide response body upon HTTP fault with publish
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai-NL committed Nov 13, 2023
1 parent ec76487 commit 52ebaa7
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/pdm/cli/commands/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

from pdm.project import Project

LIMIT_SIZE_RESPONSEDATA = 4096


class Command(BaseCommand):
"""Build and publish the project to PyPI"""
Expand Down Expand Up @@ -71,7 +73,11 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--no-very-ssl", action="store_false", dest="verify_ssl", help="Disable SSL verification", default=None
"--no-very-ssl",
action="store_false",
dest="verify_ssl",
help="Disable SSL verification",
default=None,
)
group.add_argument(
"--ca-certs",
Expand All @@ -81,7 +87,9 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
)

@staticmethod
def _make_package(filename: str, signatures: dict[str, str], options: argparse.Namespace) -> PackageFile:
def _make_package(
filename: str, signatures: dict[str, str], options: argparse.Namespace
) -> PackageFile:
p = PackageFile.from_filename(filename, options.comment)
if p.base_filename in signatures:
p.add_gpg_signature(signatures[p.base_filename], p.base_filename + ".asc")
Expand All @@ -106,7 +114,13 @@ def _check_response(response: Response) -> None:
try:
response.raise_for_status()
except requests.HTTPError as err:
message = str(err)
message = str(err) + "\n"
if len(response.text) <= LIMIT_SIZE_RESPONSEDATA:
message += response.text
else:
message += (
response.text[:LIMIT_SIZE_RESPONSEDATA] + "\n...\n(truncated)\n"
)
if message:
raise PublishError(message)

Expand All @@ -129,7 +143,14 @@ def get_repository(project: Project, options: argparse.Namespace) -> Repository:
config.ca_certs = ca_certs
if options.verify_ssl is False:
config.verify_ssl = options.verify_ssl
return Repository(project, config.url, config.username, config.password, config.ca_certs, config.verify_ssl)
return Repository(
project,
config.url,
config.username,
config.password,
config.ca_certs,
config.verify_ssl,
)

def handle(self, project: Project, options: argparse.Namespace) -> None:
hooks = HookManager(project, options.skip)
Expand All @@ -139,8 +160,16 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
if options.build:
build.Command.do_build(project, hooks=hooks)

package_files = [str(p) for p in project.root.joinpath("dist").iterdir() if not p.name.endswith(".asc")]
signatures = {p.stem: str(p) for p in project.root.joinpath("dist").iterdir() if p.name.endswith(".asc")}
package_files = [
str(p)
for p in project.root.joinpath("dist").iterdir()
if not p.name.endswith(".asc")
]
signatures = {
p.stem: str(p)
for p in project.root.joinpath("dist").iterdir()
if p.name.endswith(".asc")
}

repository = self.get_repository(project, options)
uploaded: list[PackageFile] = []
Expand All @@ -163,7 +192,9 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
)
for package in packages:
resp = repository.upload(package, progress)
logger.debug("Response from %s:\n%s %s", resp.url, resp.status_code, resp.reason)
logger.debug(
"Response from %s:\n%s %s", resp.url, resp.status_code, resp.reason
)
self._check_response(resp)
uploaded.append(package)

Expand Down

0 comments on commit 52ebaa7

Please sign in to comment.