diff -r 039c316bd157 Lib/packaging/command/register.py --- a/Lib/packaging/command/register.py Sat Jun 25 20:30:52 2011 +0300 +++ b/Lib/packaging/command/register.py Sat Jun 25 21:34:40 2011 -0400 @@ -10,7 +10,7 @@ from packaging import logger from packaging.util import (read_pypirc, generate_pypirc, DEFAULT_REPOSITORY, - DEFAULT_REALM, get_pypirc_path) + DEFAULT_REALM, get_pypirc_path, encode_multipart) from packaging.command.cmd import Command class register(Command): @@ -231,32 +231,14 @@ if 'name' in data: logger.info('Registering %s to %s', data['name'], self.repository) # Build up the MIME payload for the urllib2 POST data - boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - sep_boundary = '\n--' + boundary - end_boundary = sep_boundary + '--' - body = io.StringIO() - for key, value in data.items(): - # handle multiple entries for the same name - if not isinstance(value, (tuple, list)): - value = [value] - - for value in value: - body.write(sep_boundary) - body.write('\nContent-Disposition: form-data; name="%s"'%key) - body.write("\n\n") - body.write(value) - if value and value[-1] == '\r': - body.write('\n') # write an extra newline (lurve Macs) - body.write(end_boundary) - body.write("\n") - body = body.getvalue() + content_type, body = encode_multipart(data.items(), []) # build the Request headers = { - 'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'%boundary, + 'Content-type': content_type, 'Content-length': str(len(body)) } - req = urllib.request.Request(self.repository, body, headers) + req = urllib.request.Request(self.repository, data=body, headers=headers) # handle HTTP and include the Basic Auth handler opener = urllib.request.build_opener( diff -r 039c316bd157 Lib/packaging/command/upload.py --- a/Lib/packaging/command/upload.py Sat Jun 25 20:30:52 2011 +0300 +++ b/Lib/packaging/command/upload.py Sat Jun 25 21:34:40 2011 -0400 @@ -14,7 +14,7 @@ from packaging import logger from packaging.errors import PackagingOptionError from packaging.util import (spawn, read_pypirc, DEFAULT_REPOSITORY, - DEFAULT_REALM) + DEFAULT_REALM, encode_multipart) from packaging.command.cmd import Command @@ -131,49 +131,18 @@ auth = b"Basic " + standard_b64encode(user_pass) # Build up the MIME payload for the POST data - boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - sep_boundary = b'\n--' + boundary - end_boundary = sep_boundary + b'--' - body = BytesIO() + files = [] + for key in ('content', 'gpg_signature'): + if key in data: + filename_, value = data.pop(key) + files.append((key, filename_, value)) - file_fields = ('content', 'gpg_signature') - - for key, value in data.items(): - # handle multiple entries for the same name - if not isinstance(value, tuple): - value = [value] - - content_dispo = '\nContent-Disposition: form-data; name="%s"' % key - - if key in file_fields: - filename_, content = value - filename_ = ';filename="%s"' % filename_ - body.write(sep_boundary) - body.write(content_dispo.encode('utf-8')) - body.write(filename_.encode('utf-8')) - body.write(b"\n\n") - body.write(content) - else: - for value in value: - value = str(value).encode('utf-8') - body.write(sep_boundary) - body.write(content_dispo.encode('utf-8')) - body.write(b"\n\n") - body.write(value) - if value and value.endswith(b'\r'): - # write an extra newline (lurve Macs) - body.write(b'\n') - - body.write(end_boundary) - body.write(b"\n") - body = body.getvalue() + content_type, body = encode_multipart(data.items(), files) logger.info("Submitting %s to %s", filename, self.repository) # build the Request - headers = {'Content-type': - 'multipart/form-data; boundary=%s' % - boundary.decode('ascii'), + headers = {'Content-type': content_type, 'Content-length': str(len(body)), 'Authorization': auth} diff -r 039c316bd157 Lib/packaging/command/upload_docs.py --- a/Lib/packaging/command/upload_docs.py Sat Jun 25 20:30:52 2011 +0300 +++ b/Lib/packaging/command/upload_docs.py Sat Jun 25 21:34:40 2011 -0400 @@ -10,7 +10,8 @@ from io import BytesIO from packaging import logger -from packaging.util import read_pypirc, DEFAULT_REPOSITORY, DEFAULT_REALM +from packaging.util import (read_pypirc, DEFAULT_REPOSITORY, DEFAULT_REALM, + encode_multipart) from packaging.errors import PackagingFileError from packaging.command.cmd import Command @@ -28,49 +29,6 @@ return destination -# grabbed from -# http://code.activestate.com/recipes/ -# 146306-http-client-to-post-using-multipartform-data/ -# TODO factor this out for use by install and command/upload - -def encode_multipart(fields, files, boundary=None): - """ - *fields* is a sequence of (name: str, value: str) elements for regular - form fields, *files* is a sequence of (name: str, filename: str, value: - bytes) elements for data to be uploaded as files. - - Returns (content_type: bytes, body: bytes) ready for http.client.HTTP. - """ - if boundary is None: - boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - elif not isinstance(boundary, bytes): - raise TypeError('boundary is not bytes but %r' % type(boundary)) - - l = [] - for key, value in fields: - l.extend(( - b'--' + boundary, - ('Content-Disposition: form-data; name="%s"' % - key).encode('utf-8'), - b'', - value.encode('utf-8'))) - - for key, filename, value in files: - l.extend(( - b'--' + boundary, - ('Content-Disposition: form-data; name="%s"; filename="%s"' % - (key, filename)).encode('utf-8'), - b'', - value)) - l.append(b'--' + boundary + b'--') - l.append(b'') - - body = b'\r\n'.join(l) - - content_type = b'multipart/form-data; boundary=' + boundary - return content_type, body - - class upload_docs(Command): description = "upload HTML documentation to PyPI" diff -r 039c316bd157 Lib/packaging/tests/test_command_register.py --- a/Lib/packaging/tests/test_command_register.py Sat Jun 25 20:30:52 2011 +0300 +++ b/Lib/packaging/tests/test_command_register.py Sat Jun 25 21:34:40 2011 -0400 @@ -152,7 +152,7 @@ req1 = dict(self.conn.reqs[0].headers) req2 = dict(self.conn.reqs[1].headers) self.assertEqual(req2['Content-length'], req1['Content-length']) - self.assertIn('xxx', self.conn.reqs[1].data) + self.assertIn(b'xxx', self.conn.reqs[1].data) def test_password_not_in_file(self): @@ -180,8 +180,8 @@ self.assertEqual(len(self.conn.reqs), 1) req = self.conn.reqs[0] headers = dict(req.headers) - self.assertEqual(headers['Content-length'], '608') - self.assertIn('tarek', req.data) + self.assertEqual(headers['Content-length'], '628') + self.assertIn(b'tarek', req.data) def test_password_reset(self): # this test runs choice 3 @@ -195,8 +195,8 @@ self.assertEqual(len(self.conn.reqs), 1) req = self.conn.reqs[0] headers = dict(req.headers) - self.assertEqual(headers['Content-length'], '290') - self.assertIn('tarek', req.data) + self.assertEqual(headers['Content-length'], '298') + self.assertIn(b'tarek', req.data) @unittest.skipUnless(DOCUTILS_SUPPORT, 'needs docutils') def test_strict(self): diff -r 039c316bd157 Lib/packaging/util.py --- a/Lib/packaging/util.py Sat Jun 25 20:30:52 2011 +0300 +++ b/Lib/packaging/util.py Sat Jun 25 21:34:40 2011 -0400 @@ -1464,3 +1464,49 @@ _path_created.add(abs_head) return created_dirs + + +# grabbed from +# http://code.activestate.com/recipes/ +# 146306-http-client-to-post-using-multipartform-data/ + +def encode_multipart(fields, files, boundary=None): + """ + *fields* is a sequence of (name: str, value: str) elements for regular + form fields, *files* is a sequence of (name: str, filename: str, value: + bytes) elements for data to be uploaded as files. + + Returns (content_type: bytes, body: bytes) ready for http.client.HTTP. + """ + if boundary is None: + boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' + elif not isinstance(boundary, bytes): + raise TypeError('boundary is not bytes but %r' % type(boundary)) + + l = [] + for key, values in fields: + # handle multiple entries for the same name + if not isinstance(values, (tuple, list)): + values=[values] + for value in values: + l.extend(( + b'--' + boundary, + ('Content-Disposition: form-data; name="%s"' % + key).encode('utf-8'), + b'', + value.encode('utf-8'))) + + for key, filename, value in files: + l.extend(( + b'--' + boundary, + ('Content-Disposition: form-data; name="%s"; filename="%s"' % + (key, filename)).encode('utf-8'), + b'', + value)) + l.append(b'--' + boundary + b'--') + l.append(b'') + + body = b'\r\n'.join(l) + + content_type = b'multipart/form-data; boundary=' + boundary + return content_type, body