From badfe739c61dd6f3609b4e3854519cfbe663c5a2 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 30 Mar 2021 16:51:52 +0200 Subject: [PATCH 1/7] Write long description in message payload --- changelog.d/2828.change.rst | 2 ++ setuptools/dist.py | 8 ++++++-- setuptools/tests/test_egg_info.py | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 changelog.d/2828.change.rst diff --git a/changelog.d/2828.change.rst b/changelog.d/2828.change.rst new file mode 100644 index 0000000000..9d607cbd39 --- /dev/null +++ b/changelog.d/2828.change.rst @@ -0,0 +1,2 @@ +Write long description in message payload of PKG-INFO file. +Changed in metadata version 2.1 diff --git a/setuptools/dist.py b/setuptools/dist.py index a1b7e832cc..71b31873e8 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -176,8 +176,9 @@ def write_field(key, value): for project_url in self.project_urls.items(): write_field('Project-URL', '%s, %s' % project_url) - long_desc = rfc822_escape(self.get_long_description()) - write_field('Description', long_desc) + if version < StrictVersion('2.1'): + long_desc = rfc822_escape(self.get_long_description()) + write_field('Description', long_desc) keywords = ','.join(self.get_keywords()) if keywords: @@ -207,6 +208,9 @@ def write_field(key, value): for extra in self.provides_extras: write_field('Provides-Extra', extra) + if version >= StrictVersion('2.1'): + file.write("\n%s\n\n" % self.get_long_description()) + sequence = tuple, list diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index 0d595ad078..ba683d088d 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py @@ -875,6 +875,27 @@ def test_long_description_content_type(self, tmpdir_cwd, env): assert expected_line in pkg_info_lines assert 'Metadata-Version: 2.1' in pkg_info_lines + def test_description(self, tmpdir_cwd, env): + self._setup_script_with_requires( + "long_description='This is a long description\\nover multiple lines',") + environ = os.environ.copy().update( + HOME=env.paths['home'], + ) + code, data = environment.run_setup_py( + cmd=['egg_info'], + pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]), + data_stream=1, + env=environ, + ) + egg_info_dir = os.path.join('.', 'foo.egg-info') + with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: + pkg_info_lines = pkginfo_file.read().split('\n') + assert 'Metadata-Version: 2.1' in pkg_info_lines + assert '' == pkg_info_lines[-1] + long_desc_lines = pkg_info_lines[pkg_info_lines.index(''):] + assert 'This is a long description' in long_desc_lines + assert 'over multiple lines' in long_desc_lines + def test_project_urls(self, tmpdir_cwd, env): # Test that specifying a `project_urls` dict to the `setup` # function results in writing multiple `Project-URL` lines to From 56bd73b26d379f1423ae8816941018c6a48c0ef7 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 30 Mar 2021 19:11:21 +0200 Subject: [PATCH 2/7] Fix tests --- setuptools/dist.py | 20 ++++++++++++++++++++ setuptools/tests/test_egg_info.py | 10 ++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/setuptools/dist.py b/setuptools/dist.py index 71b31873e8..1eb51ba415 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -96,6 +96,24 @@ def read_pkg_file(self, file): """Reads the metadata values from a file object.""" msg = message_from_file(file) + def _read_long_description(): + value = msg['description'] + if value in ('UNKNOWN', None): + return None + description_lines = value.splitlines() + if len(description_lines) == 1: + return description_lines[0].lstrip() + description_dedent = '\n'.join( + (description_lines[0].lstrip(), + textwrap.dedent('\n'.join(description_lines[1:])))) + return description_dedent + + def _read_payload(): + value = msg.get_payload().strip() + if value == 'UNKNOWN': + return None + return value + self.metadata_version = StrictVersion(msg['metadata-version']) self.name = _read_field_from_msg(msg, 'name') self.version = _read_field_from_msg(msg, 'version') @@ -114,6 +132,8 @@ def read_pkg_file(self, file): self.download_url = None self.long_description = _read_field_unescaped_from_msg(msg, 'description') + if self.long_description is None and self.metadata_version >= StrictVersion('2.1'): + self.long_description = _read_payload() self.description = _read_field_from_msg(msg, 'summary') if 'keywords' in msg: diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index ba683d088d..d0183eef01 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py @@ -875,9 +875,15 @@ def test_long_description_content_type(self, tmpdir_cwd, env): assert expected_line in pkg_info_lines assert 'Metadata-Version: 2.1' in pkg_info_lines - def test_description(self, tmpdir_cwd, env): + def test_long_description(self, tmpdir_cwd, env): + # Test that specifying `long_description` and `long_description_content_type` + # keyword args to the `setup` function results in writing + # the description in the message payload of the `PKG-INFO` file + # in the `.egg-info` directory. self._setup_script_with_requires( - "long_description='This is a long description\\nover multiple lines',") + "long_description='This is a long description\\nover multiple lines'," + "long_description_content_type='text/markdown'," + ) environ = os.environ.copy().update( HOME=env.paths['home'], ) From 2a6fc561148f3cf91af0849183f2094e3d0fdacc Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 9 Apr 2021 22:17:31 +0200 Subject: [PATCH 3/7] Update changlog entry --- changelog.d/2828.change.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/2828.change.rst b/changelog.d/2828.change.rst index 9d607cbd39..a7af513bcf 100644 --- a/changelog.d/2828.change.rst +++ b/changelog.d/2828.change.rst @@ -1,2 +1,2 @@ Write long description in message payload of PKG-INFO file. -Changed in metadata version 2.1 +Changed in metadata version 2.1 - by :user:`cdce8p` From d1a491ec8144ba856b74467b551eab359e86b659 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 15 Apr 2021 02:01:17 +0200 Subject: [PATCH 4/7] Changes after rebase --- setuptools/dist.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/setuptools/dist.py b/setuptools/dist.py index 1eb51ba415..961b3cbde0 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -92,28 +92,17 @@ def _read_list_from_msg(msg: "Message", field: str) -> Optional[List[str]]: return values +def _read_payload_from_msg(msg: "Message") -> Optional[str]: + value = msg.get_payload().strip() + if value == 'UNKNOWN': + return None + return value + + def read_pkg_file(self, file): """Reads the metadata values from a file object.""" msg = message_from_file(file) - def _read_long_description(): - value = msg['description'] - if value in ('UNKNOWN', None): - return None - description_lines = value.splitlines() - if len(description_lines) == 1: - return description_lines[0].lstrip() - description_dedent = '\n'.join( - (description_lines[0].lstrip(), - textwrap.dedent('\n'.join(description_lines[1:])))) - return description_dedent - - def _read_payload(): - value = msg.get_payload().strip() - if value == 'UNKNOWN': - return None - return value - self.metadata_version = StrictVersion(msg['metadata-version']) self.name = _read_field_from_msg(msg, 'name') self.version = _read_field_from_msg(msg, 'version') @@ -133,7 +122,7 @@ def _read_payload(): self.long_description = _read_field_unescaped_from_msg(msg, 'description') if self.long_description is None and self.metadata_version >= StrictVersion('2.1'): - self.long_description = _read_payload() + self.long_description = _read_payload_from_msg(msg) self.description = _read_field_from_msg(msg, 'summary') if 'keywords' in msg: From 1069ae4e3554a8828075a83dedc77e0e212637c7 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 15 Apr 2021 23:10:40 +0200 Subject: [PATCH 5/7] Small changes after review --- setuptools/tests/test_egg_info.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index d0183eef01..22e970a4bd 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py @@ -884,21 +884,17 @@ def test_long_description(self, tmpdir_cwd, env): "long_description='This is a long description\\nover multiple lines'," "long_description_content_type='text/markdown'," ) - environ = os.environ.copy().update( - HOME=env.paths['home'], - ) code, data = environment.run_setup_py( cmd=['egg_info'], pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]), data_stream=1, - env=environ, ) egg_info_dir = os.path.join('.', 'foo.egg-info') with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'Metadata-Version: 2.1' in pkg_info_lines assert '' == pkg_info_lines[-1] - long_desc_lines = pkg_info_lines[pkg_info_lines.index(''):] + long_desc_lines = pkg_info_lines[8:] assert 'This is a long description' in long_desc_lines assert 'over multiple lines' in long_desc_lines From 273bc17bbe8d58d1e87523120da965dbfdabf53b Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 15 Apr 2021 23:39:53 +0200 Subject: [PATCH 6/7] Fix changelog filename --- changelog.d/{2828.change.rst => 2628.change.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog.d/{2828.change.rst => 2628.change.rst} (100%) diff --git a/changelog.d/2828.change.rst b/changelog.d/2628.change.rst similarity index 100% rename from changelog.d/2828.change.rst rename to changelog.d/2628.change.rst From 34c31ebe437edf9d6d4ee5010461668156793569 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 9 May 2021 20:16:37 +0200 Subject: [PATCH 7/7] Changes after rebase --- changelog.d/2628.change.rst | 3 +-- setuptools/dist.py | 7 +------ setuptools/tests/test_dist.py | 4 ++-- setuptools/tests/test_egg_info.py | 4 ++-- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/changelog.d/2628.change.rst b/changelog.d/2628.change.rst index a7af513bcf..53619f89c8 100644 --- a/changelog.d/2628.change.rst +++ b/changelog.d/2628.change.rst @@ -1,2 +1 @@ -Write long description in message payload of PKG-INFO file. -Changed in metadata version 2.1 - by :user:`cdce8p` +Write long description in message payload of PKG-INFO file. - by :user:`cdce8p` diff --git a/setuptools/dist.py b/setuptools/dist.py index 961b3cbde0..bf3b9461b8 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -185,10 +185,6 @@ def write_field(key, value): for project_url in self.project_urls.items(): write_field('Project-URL', '%s, %s' % project_url) - if version < StrictVersion('2.1'): - long_desc = rfc822_escape(self.get_long_description()) - write_field('Description', long_desc) - keywords = ','.join(self.get_keywords()) if keywords: write_field('Keywords', keywords) @@ -217,8 +213,7 @@ def write_field(key, value): for extra in self.provides_extras: write_field('Provides-Extra', extra) - if version >= StrictVersion('2.1'): - file.write("\n%s\n\n" % self.get_long_description()) + file.write("\n%s\n\n" % self.get_long_description()) sequence = tuple, list diff --git a/setuptools/tests/test_dist.py b/setuptools/tests/test_dist.py index 6378caeff1..c4279f0bc4 100644 --- a/setuptools/tests/test_dist.py +++ b/setuptools/tests/test_dist.py @@ -251,8 +251,8 @@ def test_maintainer_author(name, attrs, tmpdir): with io.open(str(fn.join('PKG-INFO')), 'r', encoding='utf-8') as f: raw_pkg_lines = f.readlines() - # Drop blank lines - pkg_lines = list(filter(None, raw_pkg_lines)) + # Drop blank lines and strip lines from default description + pkg_lines = list(filter(None, raw_pkg_lines[:-2])) pkg_lines_set = set(pkg_lines) diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py index 22e970a4bd..d7657a47ff 100644 --- a/setuptools/tests/test_egg_info.py +++ b/setuptools/tests/test_egg_info.py @@ -893,8 +893,8 @@ def test_long_description(self, tmpdir_cwd, env): with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file: pkg_info_lines = pkginfo_file.read().split('\n') assert 'Metadata-Version: 2.1' in pkg_info_lines - assert '' == pkg_info_lines[-1] - long_desc_lines = pkg_info_lines[8:] + assert '' == pkg_info_lines[-1] # last line should be empty + long_desc_lines = pkg_info_lines[pkg_info_lines.index(''):] assert 'This is a long description' in long_desc_lines assert 'over multiple lines' in long_desc_lines