From a797c9be8e5cc015127c4dd82c545ad2fe6655ca Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 21:01:46 -0400 Subject: Use context for closing file --- setuptools/command/upload_docs.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index f887b47e..133fd323 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -102,9 +102,8 @@ class upload_docs(upload): shutil.rmtree(tmp_dir) def upload_file(self, filename): - f = open(filename, 'rb') - content = f.read() - f.close() + with open(filename, 'rb') as f: + content = f.read() meta = self.distribution.metadata data = { ':action': 'doc_upload', -- cgit v1.2.3 From daf695c9c05db4f64d0c7d977054cf0a450d4839 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 21:34:47 -0400 Subject: Use bytes literals and simpler encoding logic when constructing multipart post --- setuptools/command/upload_docs.py | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index 133fd323..638a1f6d 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -21,15 +21,9 @@ from pkg_resources import iter_entry_points from .upload import upload -errors = 'surrogateescape' if six.PY3 else 'strict' - - -# This is not just a replacement for byte literals -# but works as a general purpose encoder -def b(s, encoding='utf-8'): - if isinstance(s, six.text_type): - return s.encode(encoding, errors) - return s +def _encode(s): + errors = 'surrogateescape' if six.PY3 else 'strict' + return s.encode('utf-8', errors) class upload_docs(upload): @@ -111,16 +105,16 @@ class upload_docs(upload): 'content': (os.path.basename(filename), content), } # set up the authentication - credentials = b(self.username + ':' + self.password) + credentials = _encode(self.username + ':' + self.password) credentials = standard_b64encode(credentials) if six.PY3: credentials = credentials.decode('ascii') auth = "Basic " + credentials # Build up the MIME payload for the POST data - boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - sep_boundary = b('\n--') + b(boundary) - end_boundary = sep_boundary + b('--') + boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' + sep_boundary = b'\n--' + boundary + end_boundary = sep_boundary + b'--' body = [] for key, values in six.iteritems(data): title = '\nContent-Disposition: form-data; name="%s"' % key @@ -132,16 +126,16 @@ class upload_docs(upload): title += '; filename="%s"' % value[0] value = value[1] else: - value = b(value) + value = _encode(value) body.append(sep_boundary) - body.append(b(title)) - body.append(b("\n\n")) + body.append(_encode(title)) + body.append(b"\n\n") body.append(value) - if value and value[-1:] == b('\r'): - body.append(b('\n')) # write an extra newline (lurve Macs) + if value and value[-1:] == b'\r': + body.append(b'\n') # write an extra newline (lurve Macs) body.append(end_boundary) - body.append(b("\n")) - body = b('').join(body) + body.append(b"\n") + body = b''.join(body) self.announce("Submitting documentation to %s" % (self.repository), log.INFO) -- cgit v1.2.3 From 81d8a9560f4a14fe5376f4d86643aa6bdf361869 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 21:45:38 -0400 Subject: Extract method for _build_parts. --- setuptools/command/upload_docs.py | 48 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index 638a1f6d..7136fea0 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -13,6 +13,7 @@ import socket import zipfile import tempfile import shutil +import itertools from setuptools.extern import six from setuptools.extern.six.moves import http_client, urllib @@ -95,6 +96,26 @@ class upload_docs(upload): finally: shutil.rmtree(tmp_dir) + @staticmethod + def _build_parts(data, sep_boundary): + for key, values in six.iteritems(data): + title = '\nContent-Disposition: form-data; name="%s"' % key + # handle multiple entries for the same name + if not isinstance(values, list): + values = [values] + for value in values: + if type(value) is tuple: + title += '; filename="%s"' % value[0] + value = value[1] + else: + value = _encode(value) + yield sep_boundary + yield _encode(title) + yield b"\n\n" + yield value + if value and value[-1:] == b'\r': + yield b'\n' # write an extra newline (lurve Macs) + def upload_file(self, filename): with open(filename, 'rb') as f: content = f.read() @@ -115,27 +136,12 @@ class upload_docs(upload): boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' sep_boundary = b'\n--' + boundary end_boundary = sep_boundary + b'--' - body = [] - for key, values in six.iteritems(data): - title = '\nContent-Disposition: form-data; name="%s"' % key - # handle multiple entries for the same name - if not isinstance(values, list): - values = [values] - for value in values: - if type(value) is tuple: - title += '; filename="%s"' % value[0] - value = value[1] - else: - value = _encode(value) - body.append(sep_boundary) - body.append(_encode(title)) - body.append(b"\n\n") - body.append(value) - if value and value[-1:] == b'\r': - body.append(b'\n') # write an extra newline (lurve Macs) - body.append(end_boundary) - body.append(b"\n") - body = b''.join(body) + end_items = end_boundary, b"\n", + body_items = itertools.chain( + self._build_parts(data, sep_boundary), + end_items, + ) + body = b''.join(body_items) self.announce("Submitting documentation to %s" % (self.repository), log.INFO) -- cgit v1.2.3 From 5e9c9d2fc751749e9ced584a548b197a552179bf Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 21:52:19 -0400 Subject: Extract method for _build_multipart --- setuptools/command/upload_docs.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index 7136fea0..de38e8af 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -116,6 +116,21 @@ class upload_docs(upload): if value and value[-1:] == b'\r': yield b'\n' # write an extra newline (lurve Macs) + @classmethod + def _build_multipart(cls, data): + """ + Build up the MIME payload for the POST data + """ + boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' + sep_boundary = b'\n--' + boundary + end_boundary = sep_boundary + b'--' + end_items = end_boundary, b"\n", + body_items = itertools.chain( + cls._build_parts(data, sep_boundary), + end_items, + ) + return b''.join(body_items) + def upload_file(self, filename): with open(filename, 'rb') as f: content = f.read() @@ -132,16 +147,7 @@ class upload_docs(upload): credentials = credentials.decode('ascii') auth = "Basic " + credentials - # Build up the MIME payload for the POST data - boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - sep_boundary = b'\n--' + boundary - end_boundary = sep_boundary + b'--' - end_items = end_boundary, b"\n", - body_items = itertools.chain( - self._build_parts(data, sep_boundary), - end_items, - ) - body = b''.join(body_items) + body = self._build_multipart(data) self.announce("Submitting documentation to %s" % (self.repository), log.INFO) -- cgit v1.2.3 From 0e17685d76ce5b7f28b3396e362e3f960c912a37 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 21:55:31 -0400 Subject: Extract method for _build_part --- setuptools/command/upload_docs.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index de38e8af..86a71468 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -96,9 +96,14 @@ class upload_docs(upload): finally: shutil.rmtree(tmp_dir) - @staticmethod - def _build_parts(data, sep_boundary): + @classmethod + def _build_parts(cls, data, sep_boundary): for key, values in six.iteritems(data): + for part in cls._build_part(key, values): + yield part + + @staticmethod + def _build_part(key, values, sep_boundary): title = '\nContent-Disposition: form-data; name="%s"' % key # handle multiple entries for the same name if not isinstance(values, list): -- cgit v1.2.3 From 4b0aaa921e46afbfa2f98069ea016f80c3ab1c84 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 21:55:48 -0400 Subject: Normalize indent --- setuptools/command/upload_docs.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index 86a71468..4ad66c29 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -104,22 +104,22 @@ class upload_docs(upload): @staticmethod def _build_part(key, values, sep_boundary): - title = '\nContent-Disposition: form-data; name="%s"' % key - # handle multiple entries for the same name - if not isinstance(values, list): - values = [values] - for value in values: - if type(value) is tuple: - title += '; filename="%s"' % value[0] - value = value[1] - else: - value = _encode(value) - yield sep_boundary - yield _encode(title) - yield b"\n\n" - yield value - if value and value[-1:] == b'\r': - yield b'\n' # write an extra newline (lurve Macs) + title = '\nContent-Disposition: form-data; name="%s"' % key + # handle multiple entries for the same name + if not isinstance(values, list): + values = [values] + for value in values: + if type(value) is tuple: + title += '; filename="%s"' % value[0] + value = value[1] + else: + value = _encode(value) + yield sep_boundary + yield _encode(title) + yield b"\n\n" + yield value + if value and value[-1:] == b'\r': + yield b'\n' # write an extra newline (lurve Macs) @classmethod def _build_multipart(cls, data): -- cgit v1.2.3 From f84edd610dbc12497a79b172312e2061979f5523 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 22:05:47 -0400 Subject: Replace _build_parts with map and flatten. --- setuptools/command/upload_docs.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index 4ad66c29..3158c435 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -14,6 +14,7 @@ import zipfile import tempfile import shutil import itertools +import functools from setuptools.extern import six from setuptools.extern.six.moves import http_client, urllib @@ -96,14 +97,9 @@ class upload_docs(upload): finally: shutil.rmtree(tmp_dir) - @classmethod - def _build_parts(cls, data, sep_boundary): - for key, values in six.iteritems(data): - for part in cls._build_part(key, values): - yield part - @staticmethod - def _build_part(key, values, sep_boundary): + def _build_part(item, sep_boundary): + key, values = item title = '\nContent-Disposition: form-data; name="%s"' % key # handle multiple entries for the same name if not isinstance(values, list): @@ -130,10 +126,13 @@ class upload_docs(upload): sep_boundary = b'\n--' + boundary end_boundary = sep_boundary + b'--' end_items = end_boundary, b"\n", - body_items = itertools.chain( - cls._build_parts(data, sep_boundary), - end_items, + builder = functools.partial( + cls._build_part, + sep_boundary=sep_boundary, ) + part_groups = map(builder, data.items()) + parts = itertools.chain.from_iterable(part_groups) + body_items = itertools.chain(parts, end_items) return b''.join(body_items) def upload_file(self, filename): -- cgit v1.2.3 From 1f1601d16b93b12451363b46b4b0725300f9b21b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 31 May 2016 22:26:29 -0400 Subject: Return content type in _build_multipart --- setuptools/command/upload_docs.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'setuptools/command/upload_docs.py') diff --git a/setuptools/command/upload_docs.py b/setuptools/command/upload_docs.py index 3158c435..01b49046 100644 --- a/setuptools/command/upload_docs.py +++ b/setuptools/command/upload_docs.py @@ -133,7 +133,8 @@ class upload_docs(upload): part_groups = map(builder, data.items()) parts = itertools.chain.from_iterable(part_groups) body_items = itertools.chain(parts, end_items) - return b''.join(body_items) + content_type = 'multipart/form-data; boundary=%s' % boundary + return b''.join(body_items), content_type def upload_file(self, filename): with open(filename, 'rb') as f: @@ -151,7 +152,7 @@ class upload_docs(upload): credentials = credentials.decode('ascii') auth = "Basic " + credentials - body = self._build_multipart(data) + body, ct = self._build_multipart(data) self.announce("Submitting documentation to %s" % (self.repository), log.INFO) @@ -173,7 +174,7 @@ class upload_docs(upload): try: conn.connect() conn.putrequest("POST", url) - content_type = 'multipart/form-data; boundary=%s' % boundary + content_type = ct conn.putheader('Content-type', content_type) conn.putheader('Content-length', str(len(body))) conn.putheader('Authorization', auth) -- cgit v1.2.3