mirror of https://github.com/python/cpython.git
Merge with 3.3 for Issue #19544 and Issue #6286. Merge is untested. I was unable to test due to bab0cbf86835.
This commit is contained in:
commit
0048ae0cc6
|
@ -10,10 +10,9 @@
|
||||||
import os, io
|
import os, io
|
||||||
import socket
|
import socket
|
||||||
import platform
|
import platform
|
||||||
import configparser
|
|
||||||
import http.client as httpclient
|
|
||||||
from base64 import standard_b64encode
|
from base64 import standard_b64encode
|
||||||
import urllib.parse
|
from urllib.request import urlopen, Request, HTTPError
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
# this keeps compatibility for 2.3 and 2.4
|
# this keeps compatibility for 2.3 and 2.4
|
||||||
if sys.version < "2.5":
|
if sys.version < "2.5":
|
||||||
|
@ -66,6 +65,15 @@ def run(self):
|
||||||
self.upload_file(command, pyversion, filename)
|
self.upload_file(command, pyversion, filename)
|
||||||
|
|
||||||
def upload_file(self, command, pyversion, filename):
|
def upload_file(self, command, pyversion, filename):
|
||||||
|
# Makes sure the repository URL is compliant
|
||||||
|
schema, netloc, url, params, query, fragments = \
|
||||||
|
urlparse(self.repository)
|
||||||
|
if params or query or fragments:
|
||||||
|
raise AssertionError("Incompatible url %s" % self.repository)
|
||||||
|
|
||||||
|
if schema not in ('http', 'https'):
|
||||||
|
raise AssertionError("unsupported schema " + schema)
|
||||||
|
|
||||||
# Sign if requested
|
# Sign if requested
|
||||||
if self.sign:
|
if self.sign:
|
||||||
gpg_args = ["gpg", "--detach-sign", "-a", filename]
|
gpg_args = ["gpg", "--detach-sign", "-a", filename]
|
||||||
|
@ -162,41 +170,31 @@ def upload_file(self, command, pyversion, filename):
|
||||||
self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
|
self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
|
||||||
|
|
||||||
# build the Request
|
# build the Request
|
||||||
# We can't use urllib since we need to send the Basic
|
headers = {'Content-type':
|
||||||
# auth right with the first request
|
'multipart/form-data; boundary=%s' % boundary,
|
||||||
# TODO(jhylton): Can we fix urllib?
|
'Content-length': str(len(body)),
|
||||||
schema, netloc, url, params, query, fragments = \
|
'Authorization': auth}
|
||||||
urllib.parse.urlparse(self.repository)
|
|
||||||
assert not params and not query and not fragments
|
|
||||||
if schema == 'http':
|
|
||||||
http = httpclient.HTTPConnection(netloc)
|
|
||||||
elif schema == 'https':
|
|
||||||
http = httpclient.HTTPSConnection(netloc)
|
|
||||||
else:
|
|
||||||
raise AssertionError("unsupported schema "+schema)
|
|
||||||
|
|
||||||
data = ''
|
request = Request(self.repository, data=body,
|
||||||
loglevel = log.INFO
|
headers=headers)
|
||||||
|
# send the data
|
||||||
try:
|
try:
|
||||||
http.connect()
|
result = urlopen(request)
|
||||||
http.putrequest("POST", url)
|
status = result.getcode()
|
||||||
http.putheader('Content-type',
|
reason = result.msg
|
||||||
'multipart/form-data; boundary=%s'%boundary)
|
|
||||||
http.putheader('Content-length', str(len(body)))
|
|
||||||
http.putheader('Authorization', auth)
|
|
||||||
http.endheaders()
|
|
||||||
http.send(body)
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
self.announce(str(e), log.ERROR)
|
self.announce(str(e), log.ERROR)
|
||||||
return
|
return
|
||||||
|
except HTTPError as e:
|
||||||
|
status = e.code
|
||||||
|
reason = e.msg
|
||||||
|
|
||||||
r = http.getresponse()
|
if status == 200:
|
||||||
if r.status == 200:
|
self.announce('Server response (%s): %s' % (status, reason),
|
||||||
self.announce('Server response (%s): %s' % (r.status, r.reason),
|
|
||||||
log.INFO)
|
log.INFO)
|
||||||
else:
|
else:
|
||||||
self.announce('Upload failed (%s): %s' % (r.status, r.reason),
|
self.announce('Upload failed (%s): %s' % (status, reason),
|
||||||
log.ERROR)
|
log.ERROR)
|
||||||
if self.show_response:
|
if self.show_response:
|
||||||
msg = '\n'.join(('-' * 75, r.read(), '-' * 75))
|
msg = '\n'.join(('-' * 75, result.read(), '-' * 75))
|
||||||
self.announce(msg, log.INFO)
|
self.announce(msg, log.INFO)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""Tests for distutils.command.upload."""
|
"""Tests for distutils.command.upload."""
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
import http.client as httpclient
|
|
||||||
from test.support import run_unittest
|
from test.support import run_unittest
|
||||||
|
|
||||||
|
from distutils.command import upload as upload_mod
|
||||||
from distutils.command.upload import upload
|
from distutils.command.upload import upload
|
||||||
from distutils.core import Distribution
|
from distutils.core import Distribution
|
||||||
|
|
||||||
|
@ -37,47 +37,36 @@
|
||||||
[server1]
|
[server1]
|
||||||
username:me
|
username:me
|
||||||
"""
|
"""
|
||||||
class Response(object):
|
|
||||||
def __init__(self, status=200, reason='OK'):
|
|
||||||
self.status = status
|
|
||||||
self.reason = reason
|
|
||||||
|
|
||||||
class FakeConnection(object):
|
class FakeOpen(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, url):
|
||||||
self.requests = []
|
self.url = url
|
||||||
self.headers = []
|
if not isinstance(url, str):
|
||||||
self.body = ''
|
self.req = url
|
||||||
|
else:
|
||||||
|
self.req = None
|
||||||
|
self.msg = 'OK'
|
||||||
|
|
||||||
def __call__(self, netloc):
|
def getcode(self):
|
||||||
return self
|
return 200
|
||||||
|
|
||||||
def connect(self):
|
|
||||||
pass
|
|
||||||
endheaders = connect
|
|
||||||
|
|
||||||
def putrequest(self, method, url):
|
|
||||||
self.requests.append((method, url))
|
|
||||||
|
|
||||||
def putheader(self, name, value):
|
|
||||||
self.headers.append((name, value))
|
|
||||||
|
|
||||||
def send(self, body):
|
|
||||||
self.body = body
|
|
||||||
|
|
||||||
def getresponse(self):
|
|
||||||
return Response()
|
|
||||||
|
|
||||||
class uploadTestCase(PyPIRCCommandTestCase):
|
class uploadTestCase(PyPIRCCommandTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(uploadTestCase, self).setUp()
|
super(uploadTestCase, self).setUp()
|
||||||
if hasattr(httpclient, 'HTTPSConnection'):
|
self.old_open = upload_mod.urlopen
|
||||||
self.addCleanup(setattr, httpclient, 'HTTPSConnection',
|
upload_mod.urlopen = self._urlopen
|
||||||
httpclient.HTTPSConnection)
|
self.last_open = None
|
||||||
else:
|
|
||||||
self.addCleanup(delattr, httpclient, 'HTTPSConnection')
|
def tearDown(self):
|
||||||
self.conn = httpclient.HTTPSConnection = FakeConnection()
|
upload_mod.urlopen = self.old_open
|
||||||
|
super(uploadTestCase, self).tearDown()
|
||||||
|
|
||||||
|
def _urlopen(self, url):
|
||||||
|
self.last_open = FakeOpen(url)
|
||||||
|
return self.last_open
|
||||||
|
|
||||||
def test_finalize_options(self):
|
def test_finalize_options(self):
|
||||||
|
|
||||||
|
@ -122,14 +111,14 @@ def test_upload(self):
|
||||||
cmd.ensure_finalized()
|
cmd.ensure_finalized()
|
||||||
cmd.run()
|
cmd.run()
|
||||||
|
|
||||||
# what did we send ?
|
# what did we send ?
|
||||||
headers = dict(self.conn.headers)
|
headers = dict(self.last_open.req.headers)
|
||||||
self.assertEqual(headers['Content-length'], '2087')
|
self.assertEqual(headers['Content-length'], '2087')
|
||||||
self.assertTrue(headers['Content-type'].startswith('multipart/form-data'))
|
self.assert_(headers['Content-type'].startswith('multipart/form-data'))
|
||||||
self.assertFalse('\n' in headers['Authorization'])
|
self.assertEquals(self.last_open.req.get_method(), 'POST')
|
||||||
|
self.assertEquals(self.last_open.req.get_full_url(),
|
||||||
self.assertEqual(self.conn.requests, [('POST', '/pypi')])
|
'http://pypi.python.org/pypi')
|
||||||
self.assertTrue((b'xxx') in self.conn.body)
|
self.assert_(b'xxx' in self.last_open.req.data)
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
return unittest.makeSuite(uploadTestCase)
|
return unittest.makeSuite(uploadTestCase)
|
||||||
|
|
|
@ -34,6 +34,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #19544 and Issue #6286: Restore use of urllib over http allowing use
|
||||||
|
of http_proxy for Distutils upload command, a feature accidentally lost
|
||||||
|
in the rollback of distutils2.
|
||||||
|
|
||||||
- Issue #19544 and Issue #7457: Restore the read_pkg_file method to
|
- Issue #19544 and Issue #7457: Restore the read_pkg_file method to
|
||||||
distutils.dist.DistributionMetadata accidentally removed in the undo of
|
distutils.dist.DistributionMetadata accidentally removed in the undo of
|
||||||
distutils2.
|
distutils2.
|
||||||
|
|
Loading…
Reference in New Issue