[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -4,37 +4,44 @@ distutils.command.upload
Implements the Distutils 'upload' subcommand (upload package to a package
index).
"""
from __future__ import annotations
import os
import io
import hashlib
import io
import os
from base64 import standard_b64encode
from urllib.request import urlopen, Request, HTTPError
from urllib.parse import urlparse
from distutils.errors import DistutilsError, DistutilsOptionError
from distutils.core import PyPIRCCommand
from distutils.spawn import spawn
from urllib.request import HTTPError
from urllib.request import Request
from urllib.request import urlopen
from distutils import log
from distutils.core import PyPIRCCommand
from distutils.errors import DistutilsError
from distutils.errors import DistutilsOptionError
from distutils.spawn import spawn
# PyPI Warehouse supports MD5, SHA256, and Blake2 (blake2-256)
# https://bugs.python.org/issue40698
_FILE_CONTENT_DIGESTS = {
"md5_digest": getattr(hashlib, "md5", None),
"sha256_digest": getattr(hashlib, "sha256", None),
"blake2_256_digest": getattr(hashlib, "blake2b", None),
'md5_digest': getattr(hashlib, 'md5', None),
'sha256_digest': getattr(hashlib, 'sha256', None),
'blake2_256_digest': getattr(hashlib, 'blake2b', None),
}
class upload(PyPIRCCommand):
description = "upload binary package to PyPI"
description = 'upload binary package to PyPI'
user_options = PyPIRCCommand.user_options + [
('sign', 's',
'sign files to upload using gpg'),
(
'sign', 's',
'sign files to upload using gpg',
),
('identity=', 'i', 'GPG identity used to sign files'),
]
]
boolean_options = PyPIRCCommand.boolean_options + ['sign']
@ -50,7 +57,7 @@ class upload(PyPIRCCommand):
PyPIRCCommand.finalize_options(self)
if self.identity and not self.sign:
raise DistutilsOptionError(
"Must use --sign for --identity to have meaning"
'Must use --sign for --identity to have meaning',
)
config = self._read_pypirc()
if config != {}:
@ -66,8 +73,10 @@ class upload(PyPIRCCommand):
def run(self):
if not self.distribution.dist_files:
msg = ("Must create and upload files in one command "
"(e.g. setup.py sdist upload)")
msg = (
'Must create and upload files in one command '
'(e.g. setup.py sdist upload)'
)
raise DistutilsOptionError(msg)
for command, pyversion, filename in self.distribution.dist_files:
self.upload_file(command, pyversion, filename)
@ -77,22 +86,24 @@ class upload(PyPIRCCommand):
schema, netloc, url, params, query, fragments = \
urlparse(self.repository)
if params or query or fragments:
raise AssertionError("Incompatible url %s" % self.repository)
raise AssertionError('Incompatible url %s' % self.repository)
if schema not in ('http', 'https'):
raise AssertionError("unsupported schema " + schema)
raise AssertionError('unsupported schema ' + schema)
# Sign if requested
if self.sign:
gpg_args = ["gpg", "--detach-sign", "-a", filename]
gpg_args = ['gpg', '--detach-sign', '-a', filename]
if self.identity:
gpg_args[2:2] = ["--local-user", self.identity]
spawn(gpg_args,
dry_run=self.dry_run)
gpg_args[2:2] = ['--local-user', self.identity]
spawn(
gpg_args,
dry_run=self.dry_run,
)
# Fill in the data - send all the meta-data in case we need to
# register a new release
f = open(filename,'rb')
f = open(filename, 'rb')
try:
content = f.read()
finally:
@ -109,7 +120,7 @@ class upload(PyPIRCCommand):
'version': meta.get_version(),
# file content
'content': (os.path.basename(filename),content),
'content': (os.path.basename(filename), content),
'filetype': command,
'pyversion': pyversion,
@ -129,7 +140,7 @@ class upload(PyPIRCCommand):
'provides': meta.get_provides(),
'requires': meta.get_requires(),
'obsoletes': meta.get_obsoletes(),
}
}
data['comment'] = ''
@ -144,15 +155,17 @@ class upload(PyPIRCCommand):
pass
if self.sign:
with open(filename + ".asc", "rb") as f:
data['gpg_signature'] = (os.path.basename(filename) + ".asc",
f.read())
with open(filename + '.asc', 'rb') as f:
data['gpg_signature'] = (
os.path.basename(filename) + '.asc',
f.read(),
)
# set up the authentication
user_pass = (self.username + ":" + self.password).encode('ascii')
user_pass = (self.username + ':' + self.password).encode('ascii')
# The exact encoding of the authentication string is debated.
# Anyway PyPI only accepts ascii for both username or password.
auth = "Basic " + standard_b64encode(user_pass).decode('ascii')
auth = 'Basic ' + standard_b64encode(user_pass).decode('ascii')
# Build up the MIME payload for the POST data
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
@ -172,12 +185,12 @@ class upload(PyPIRCCommand):
value = str(value).encode('utf-8')
body.write(sep_boundary)
body.write(title.encode('utf-8'))
body.write(b"\r\n\r\n")
body.write(b'\r\n\r\n')
body.write(value)
body.write(end_boundary)
body = body.getvalue()
msg = "Submitting %s to %s" % (filename, self.repository)
msg = 'Submitting {} to {}'.format(filename, self.repository)
self.announce(msg, log.INFO)
# build the Request
@ -187,8 +200,10 @@ class upload(PyPIRCCommand):
'Authorization': auth,
}
request = Request(self.repository, data=body,
headers=headers)
request = Request(
self.repository, data=body,
headers=headers,
)
# send the data
try:
result = urlopen(request)
@ -202,13 +217,15 @@ class upload(PyPIRCCommand):
raise
if status == 200:
self.announce('Server response (%s): %s' % (status, reason),
log.INFO)
self.announce(
'Server response ({}): {}'.format(status, reason),
log.INFO,
)
if self.show_response:
text = self._read_pypi_response(result)
msg = '\n'.join(('-' * 75, text, '-' * 75))
self.announce(msg, log.INFO)
else:
msg = 'Upload failed (%s): %s' % (status, reason)
msg = 'Upload failed ({}): {}'.format(status, reason)
self.announce(msg, log.ERROR)
raise DistutilsError(msg)