Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write long description in message payload #2628

Merged
merged 7 commits into from May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/2628.change.rst
@@ -0,0 +1 @@
Write long description in message payload of PKG-INFO file. - by :user:`cdce8p`
14 changes: 11 additions & 3 deletions setuptools/dist.py
Expand Up @@ -92,6 +92,13 @@ 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)
Expand All @@ -114,6 +121,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'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a solution that doesn't select on metadata version? Note that long description in Metadata 2.1 could still use the Description field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will try to read the long description from the header field first. Only if the result is None and the mv >= '2.1' we try to read it from the payload as well. It will work that way, since in 2.1 an UNKNOWN value for the long description is added either in the header field or the payload.

self.long_description = _read_payload_from_msg(msg)
self.description = _read_field_from_msg(msg, 'summary')

if 'keywords' in msg:
Expand Down Expand Up @@ -176,9 +185,6 @@ 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)

keywords = ','.join(self.get_keywords())
if keywords:
write_field('Keywords', keywords)
Expand Down Expand Up @@ -207,6 +213,8 @@ def write_field(key, value):
for extra in self.provides_extras:
write_field('Provides-Extra', extra)

file.write("\n%s\n\n" % self.get_long_description())


sequence = tuple, list

Expand Down
4 changes: 2 additions & 2 deletions setuptools/tests/test_dist.py
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions setuptools/tests/test_egg_info.py
Expand Up @@ -875,6 +875,29 @@ 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_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 `<distribution>.egg-info` directory.
self._setup_script_with_requires(
"long_description='This is a long description\\nover multiple lines',"
"long_description_content_type='text/markdown',"
)
code, data = environment.run_setup_py(
cmd=['egg_info'],
pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]),
data_stream=1,
)
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] # last line should be empty
long_desc_lines = pkg_info_lines[pkg_info_lines.index(''):]
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down