From 73fbcc9cfab198ce05ef3f4a48d239606e35809a Mon Sep 17 00:00:00 2001 From: Tomasz Wrona Date: Fri, 24 Jan 2020 20:49:40 +0100 Subject: [PATCH 1/2] Parse extras_require for additional dependencies coming from environment markers --- piptools/scripts/compile.py | 10 ++++++++++ tests/test_cli_compile.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 590adf89b..5b42fa600 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -323,6 +323,16 @@ def cli( dist = run_setup(src_file) tmpfile.write("\n".join(dist.install_requires)) + + # parse additional dependencies in extras_require + # which might come from parsing environment markers + additional_deps = [] + for extra_name, extra_pkgs in dist.extras_require.items(): + if extra_name.startswith(":"): + marker = extra_name[1:] + for pkg in extra_pkgs: + additional_deps.append("{} ; {}".format(pkg, marker)) + tmpfile.write("\n".join(additional_deps)) else: tmpfile.write(sys.stdin.read()) tmpfile.flush() diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 5014d07bf..7531500b2 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -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', + '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", [ From 0f7d96cc6370b9841bf4fae6d2bd7a29ad80915e Mon Sep 17 00:00:00 2001 From: Tomasz Wrona Date: Wed, 29 Jan 2020 10:08:05 +0100 Subject: [PATCH 2/2] Fixed issue with missing line break when there was only one dependency in install_requires or extras_require --- piptools/scripts/compile.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 5b42fa600..6864e432d 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -322,17 +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 - additional_deps = [] + 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: - additional_deps.append("{} ; {}".format(pkg, marker)) - tmpfile.write("\n".join(additional_deps)) + extras_deps.append("{} ; {}".format(pkg, marker)) + + deps += extras_deps + tmpfile.write("\n".join(deps)) else: tmpfile.write(sys.stdin.read()) tmpfile.flush()