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

Fix environment markers handling #1081

Merged
merged 1 commit into from
Jul 14, 2017
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
34 changes: 33 additions & 1 deletion setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import distutils.core
import distutils.cmd
import distutils.dist
from collections import defaultdict
from distutils.errors import (DistutilsOptionError, DistutilsPlatformError,
DistutilsSetupError)
from distutils.util import rfc822_escape
Expand Down Expand Up @@ -134,7 +135,13 @@ def check_extras(dist, attr, value):
k, m = k.split(':', 1)
if pkg_resources.invalid_marker(m):
raise DistutilsSetupError("Invalid environment marker: " + m)
list(pkg_resources.parse_requirements(v))
for r in pkg_resources.parse_requirements(v):
if r.marker:
tmpl = (
"'extras_require' requirements cannot include "
"environment markers, in {section!r}: '{req!s}'"
)
raise DistutilsSetupError(tmpl.format(section=k, req=r))
except (TypeError, ValueError, AttributeError):
raise DistutilsSetupError(
"'extras_require' must be a dictionary whose values are "
Expand Down Expand Up @@ -346,6 +353,31 @@ def __init__(self, attrs=None):
)
if getattr(self, 'python_requires', None):
self.metadata.python_requires = self.python_requires
self._finalize_requires()

def _finalize_requires(self):
"""Move requirements in `install_requires` that
are using environment markers to `extras_require`.
"""
if not self.install_requires:
return
extras_require = defaultdict(list, (
(k, list(pkg_resources.parse_requirements(v)))
for k, v in (self.extras_require or {}).items()
))
install_requires = []
for r in pkg_resources.parse_requirements(self.install_requires):
marker = r.marker
if not marker:
install_requires.append(r)
continue
r.marker = None
extras_require[':'+str(marker)].append(r)
self.extras_require = dict(
(k, [str(r) for r in v])
for k, v in extras_require.items()
)
self.install_requires = [str(r) for r in install_requires]

def parse_config_files(self, filenames=None):
"""Parses configuration files from various levels
Expand Down
15 changes: 10 additions & 5 deletions setuptools/tests/test_egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ def test_install_requires_with_markers(self, tmpdir_cwd, env):
self._run_install_command(tmpdir_cwd, env)
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
requires_txt = os.path.join(egg_info_dir, 'requires.txt')
assert "barbazquux;python_version<'2'" in open(
requires_txt).read().split('\n')
with open(requires_txt) as fp:
install_requires = fp.read()
assert install_requires.lstrip() == DALS('''
[:python_version < "2"]
barbazquux
''')
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []

def test_setup_requires_with_markers(self, tmpdir_cwd, env):
Expand All @@ -202,10 +206,11 @@ def test_tests_require_with_markers(self, tmpdir_cwd, env):
tmpdir_cwd, env, cmd=['test'], output="Ran 0 tests in")
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []

def test_extra_requires_with_markers(self, tmpdir_cwd, env):
def test_extras_require_with_markers(self, tmpdir_cwd, env):
self._setup_script_with_requires(
"""extra_requires={":python_version<'2'": ["barbazquux"]},""")
self._run_install_command(tmpdir_cwd, env)
"""extras_require={"extra": ["barbazquux; python_version<'2'"]},""")
with pytest.raises(AssertionError):
self._run_install_command(tmpdir_cwd, env)
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []

def test_python_requires_egg_info(self, tmpdir_cwd, env):
Expand Down