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

Parse environment markers from setup.py install_requires #1042

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 13 additions & 1 deletion piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,19 @@ def cli(
from distutils.core import run_setup

dist = run_setup(src_file)
tmpfile.write("\n".join(dist.install_requires))
deps = dist.install_requires

# parse additional dependencies in extras_require
# which might come from parsing environment markers
extras_deps = []
for extra_name, extra_pkgs in dist.extras_require.items():
if extra_name.startswith(":"):
marker = extra_name[1:]
for pkg in extra_pkgs:
extras_deps.append("{} ; {}".format(pkg, marker))

deps += extras_deps
tmpfile.write("\n".join(deps))
else:
tmpfile.write(sys.stdin.read())
tmpfile.flush()
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ def test_command_line_setuptools_read(pip_conf, runner):
assert os.path.exists("requirements.txt")


def test_command_line_setuptools_with_env_markers_read(pip_conf, runner):
package = open("setup.py", "w")
package.write(
dedent(
"""\
from setuptools import setup
setup(
install_requires=[
'small-fake-a==0.1 ; "linux" in sys_platform',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add some corner cases. Like empty or whitespace-only string after ;, also multiple conditions

'small-fake-a==0.2 ; "win32" in sys_platform',
'small-fake-a==0.3b1 ; "darwin" in sys_platform'
]
)
"""
)
)
package.close()
out = runner.invoke(cli)

# check that pip-compile generated a configuration
assert "This file is autogenerated by pip-compile" in out.stderr
assert os.path.exists("requirements.txt")
if "linux" in sys.platform:
assert 'small-fake-a==0.1 ; "linux" in sys_platform' in out.stderr
elif "win32" in sys.platform:
assert 'small-fake-a==0.2 ; "win32" in sys_platform' in out.stderr
elif "darwin" in sys.platform:
assert 'small-fake-a==0.3b1 ; "darwin" in sys_platform' in out.stderr


@pytest.mark.parametrize(
"options, expected_output_file",
[
Expand Down