Skip to content

Commit

Permalink
Merge pull request #2628 from cdce8p/long-desc
Browse files Browse the repository at this point in the history
Write long description in message payload
  • Loading branch information
jaraco committed May 22, 2021
2 parents c6a761f + 34c31eb commit 18d751d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
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'):
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(''):]
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

0 comments on commit 18d751d

Please sign in to comment.