Skip to content

Commit 61bc807

Browse files
authored
Merge pull request #116 from abravalheri/fix-check-warning
Prevent warn on false positive for author/maintainer's email
2 parents 267dbd2 + 05c961b commit 61bc807

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

distutils/command/check.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
Implements the Distutils 'check' command.
44
"""
5+
from email.utils import getaddresses
6+
57
from distutils.core import Command
68
from distutils.errors import DistutilsSetupError
79

@@ -96,19 +98,39 @@ def check_metadata(self):
9698

9799
if missing:
98100
self.warn("missing required meta-data: %s" % ', '.join(missing))
99-
if metadata.author:
100-
if not metadata.author_email:
101-
self.warn("missing meta-data: if 'author' supplied, " +
102-
"'author_email' should be supplied too")
103-
elif metadata.maintainer:
104-
if not metadata.maintainer_email:
105-
self.warn("missing meta-data: if 'maintainer' supplied, " +
106-
"'maintainer_email' should be supplied too")
107-
else:
101+
if not (
102+
self._check_contact("author", metadata) or
103+
self._check_contact("maintainer", metadata)
104+
):
108105
self.warn("missing meta-data: either (author and author_email) " +
109106
"or (maintainer and maintainer_email) " +
110107
"should be supplied")
111108

109+
def _check_contact(self, kind, metadata):
110+
"""
111+
Returns True if the contact's name is specified and False otherwise.
112+
This function will warn if the contact's email is not specified.
113+
"""
114+
name = getattr(metadata, kind) or ''
115+
email = getattr(metadata, kind + '_email') or ''
116+
117+
msg = ("missing meta-data: if '{}' supplied, " +
118+
"'{}' should be supplied too")
119+
120+
if name and email:
121+
return True
122+
123+
if name:
124+
self.warn(msg.format(kind, kind + '_email'))
125+
return True
126+
127+
addresses = [(alias, addr) for alias, addr in getaddresses([email])]
128+
if any(alias and addr for alias, addr in addresses):
129+
# The contact's name can be encoded in the email: `Name <email>`
130+
return True
131+
132+
return False
133+
112134
def check_restructuredtext(self):
113135
"""Checks if the long string fields are reST-compliant."""
114136
data = self.distribution.get_long_description()

distutils/tests/test_check.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ def test_check_metadata(self):
7171
cmd = self._run(metadata)
7272
self.assertEqual(cmd._warnings, 0)
7373

74+
def test_check_author_maintainer(self):
75+
for kind in ("author", "maintainer"):
76+
# ensure no warning when author_email or maintainer_email is given
77+
# (the spec allows these fields to take the form "Name <email>")
78+
metadata = {'url': 'xxx',
79+
kind + '_email': 'Name <[email protected]>',
80+
'name': 'xxx', 'version': 'xxx'}
81+
cmd = self._run(metadata)
82+
self.assertEqual(cmd._warnings, 0)
83+
84+
# the check should warn if only email is given and it does not
85+
# contain the name
86+
metadata[kind + '_email'] = '[email protected]'
87+
cmd = self._run(metadata)
88+
self.assertEqual(cmd._warnings, 1)
89+
90+
# the check should warn if only the name is given
91+
metadata[kind] = "Name"
92+
del metadata[kind + '_email']
93+
cmd = self._run(metadata)
94+
self.assertEqual(cmd._warnings, 1)
95+
7496
@unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
7597
def test_check_document(self):
7698
pkg_info, dist = self.create_dist()

0 commit comments

Comments
 (0)