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

Prevent warn on false positive for author/maintainer's email #116

Merged
merged 1 commit into from
Mar 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions distutils/command/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Implements the Distutils 'check' command.
"""
from email.utils import getaddresses

from distutils.core import Command
from distutils.errors import DistutilsSetupError

Expand Down Expand Up @@ -96,19 +98,39 @@ def check_metadata(self):

if missing:
self.warn("missing required meta-data: %s" % ', '.join(missing))
if metadata.author:
if not metadata.author_email:
self.warn("missing meta-data: if 'author' supplied, " +
"'author_email' should be supplied too")
elif metadata.maintainer:
if not metadata.maintainer_email:
self.warn("missing meta-data: if 'maintainer' supplied, " +
"'maintainer_email' should be supplied too")
else:
if not (
self._check_contact("author", metadata) or
self._check_contact("maintainer", metadata)
):
self.warn("missing meta-data: either (author and author_email) " +
"or (maintainer and maintainer_email) " +
"should be supplied")

def _check_contact(self, kind, metadata):
"""
Returns True if the contact's name is specified and False otherwise.
This function will warn if the contact's email is not specified.
"""
name = getattr(metadata, kind) or ''
email = getattr(metadata, kind + '_email') or ''

msg = ("missing meta-data: if '{}' supplied, " +
"'{}' should be supplied too")

if name and email:
return True

if name:
self.warn(msg.format(kind, kind + '_email'))
return True

addresses = [(alias, addr) for alias, addr in getaddresses([email])]
if any(alias and addr for alias, addr in addresses):
# The contact's name can be encoded in the email: `Name <email>`
return True

return False

def check_restructuredtext(self):
"""Checks if the long string fields are reST-compliant."""
data = self.distribution.get_long_description()
Expand Down
22 changes: 22 additions & 0 deletions distutils/tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ def test_check_metadata(self):
cmd = self._run(metadata)
self.assertEqual(cmd._warnings, 0)

def test_check_author_maintainer(self):
for kind in ("author", "maintainer"):
# ensure no warning when author_email or maintainer_email is given
# (the spec allows these fields to take the form "Name <email>")
metadata = {'url': 'xxx',
kind + '_email': 'Name <name@email.com>',
'name': 'xxx', 'version': 'xxx'}
cmd = self._run(metadata)
self.assertEqual(cmd._warnings, 0)

# the check should warn if only email is given and it does not
# contain the name
metadata[kind + '_email'] = 'name@email.com'
cmd = self._run(metadata)
self.assertEqual(cmd._warnings, 1)

# the check should warn if only the name is given
metadata[kind] = "Name"
del metadata[kind + '_email']
cmd = self._run(metadata)
self.assertEqual(cmd._warnings, 1)

@unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
def test_check_document(self):
pkg_info, dist = self.create_dist()
Expand Down