Skip to content

Commit

Permalink
Merge pull request #2641 from cdce8p/mv-version
Browse files Browse the repository at this point in the history
Always use latest metadata version for PKG-INFO
  • Loading branch information
jaraco committed May 9, 2021
2 parents a41b6d9 + 417b02b commit fb37758
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
2 changes: 2 additions & 0 deletions changelog.d/2641.change.rst
@@ -0,0 +1,2 @@
Setuptools will now always try to use the latest supported
metadata version for ``PKG-INFO``. - by :user:`cdce8p`
48 changes: 13 additions & 35 deletions setuptools/dist.py
Expand Up @@ -52,23 +52,9 @@ def _get_unpatched(cls):

def get_metadata_version(self):
mv = getattr(self, 'metadata_version', None)

if mv is None:
if self.long_description_content_type or self.provides_extras:
mv = StrictVersion('2.1')
elif (self.maintainer is not None or
self.maintainer_email is not None or
getattr(self, 'python_requires', None) is not None or
self.project_urls):
mv = StrictVersion('1.2')
elif (self.provides or self.requires or self.obsoletes or
self.classifiers or self.download_url):
mv = StrictVersion('1.1')
else:
mv = StrictVersion('1.0')

mv = StrictVersion('2.1')
self.metadata_version = mv

return mv


Expand Down Expand Up @@ -171,22 +157,17 @@ def write_field(key, value):
write_field('Summary', single_line(self.get_description()))
write_field('Home-page', self.get_url())

if version < StrictVersion('1.2'):
write_field('Author', self.get_contact())
write_field('Author-email', self.get_contact_email())
else:
optional_fields = (
('Author', 'author'),
('Author-email', 'author_email'),
('Maintainer', 'maintainer'),
('Maintainer-email', 'maintainer_email'),
)
optional_fields = (
('Author', 'author'),
('Author-email', 'author_email'),
('Maintainer', 'maintainer'),
('Maintainer-email', 'maintainer_email'),
)

for field, attr in optional_fields:
attr_val = getattr(self, attr)

if attr_val is not None:
write_field(field, attr_val)
for field, attr in optional_fields:
attr_val = getattr(self, attr, None)
if attr_val is not None:
write_field(field, attr_val)

license = rfc822_escape(self.get_license())
write_field('License', license)
Expand All @@ -202,11 +183,8 @@ def write_field(key, value):
if keywords:
write_field('Keywords', keywords)

if version >= StrictVersion('1.2'):
for platform in self.get_platforms():
write_field('Platform', platform)
else:
self._write_list(file, 'Platform', self.get_platforms())
for platform in self.get_platforms():
write_field('Platform', platform)

self._write_list(file, 'Classifier', self.get_classifiers())

Expand Down
6 changes: 0 additions & 6 deletions setuptools/tests/test_dist.py
Expand Up @@ -84,15 +84,9 @@ def __read_test_cases():

test_cases = [
('Metadata version 1.0', params()),
('Metadata version 1.1: Provides', params(
provides=['package'],
)),
('Metadata Version 1.0: Short long description', params(
long_description='Short long description',
)),
('Metadata version 1.1: Obsoletes', params(
obsoletes=['foo'],
)),
('Metadata version 1.1: Classifiers', params(
classifiers=[
'Programming Language :: Python :: 3',
Expand Down
24 changes: 22 additions & 2 deletions setuptools/tests/test_egg_info.py
Expand Up @@ -5,6 +5,7 @@
import re
import stat
import time
from typing import List, Tuple

import pytest
from jaraco import path
Expand Down Expand Up @@ -45,6 +46,11 @@ def run():
""")
})

@staticmethod
def _extract_mv_version(pkg_info_lines: List[str]) -> Tuple[int, int]:
version_str = pkg_info_lines[0].split(' ')[1]
return tuple(map(int, version_str.split('.')[:2]))

@pytest.fixture
def env(self):
with contexts.tempdir(prefix='setuptools-test.') as env_dir:
Expand Down Expand Up @@ -829,6 +835,20 @@ def test_setup_cfg_license_file_license_files(
for lf in excl_licenses:
assert sources_lines.count(lf) == 0

def test_metadata_version(self, tmpdir_cwd, env):
"""Make sure latest metadata version is used by default."""
self._setup_script_with_requires("")
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')
# Update metadata version if changed
assert self._extract_mv_version(pkg_info_lines) == (2, 1)

def test_long_description_content_type(self, tmpdir_cwd, env):
# Test that specifying a `long_description_content_type` keyword arg to
# the `setup` function results in writing a `Description-Content-Type`
Expand Down Expand Up @@ -884,7 +904,7 @@ def test_project_urls(self, tmpdir_cwd, env):
assert expected_line in pkg_info_lines
expected_line = 'Project-URL: Link Two, https://example.com/two/'
assert expected_line in pkg_info_lines
assert 'Metadata-Version: 1.2' in pkg_info_lines
assert self._extract_mv_version(pkg_info_lines) >= (1, 2)

def test_license(self, tmpdir_cwd, env):
"""Test single line license."""
Expand Down Expand Up @@ -933,7 +953,7 @@ def test_python_requires_egg_info(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 'Requires-Python: >=2.7.12' in pkg_info_lines
assert 'Metadata-Version: 1.2' in pkg_info_lines
assert self._extract_mv_version(pkg_info_lines) >= (1, 2)

def test_manifest_maker_warning_suppression(self):
fixtures = [
Expand Down

0 comments on commit fb37758

Please sign in to comment.