From 8e334e8f888f229557b66bb12b2e686ceab3a8bd Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 27 May 2021 17:42:41 +0100 Subject: [PATCH] Add option for producing constraint compatible output Fixes: #1300 --- piptools/scripts/compile.py | 8 ++++++++ piptools/writer.py | 4 ++++ tests/conftest.py | 7 ++++++- tests/test_cli_compile.py | 26 ++++++++++++++++++++++++++ tests/test_writer.py | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index f7a786c17..92b59d8f0 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -162,6 +162,12 @@ def _get_default_option(option_name: str) -> Any: ) ), ) +@click.option( + "--strip-extras", + is_flag=True, + default=False, + help="Assure output file is constraints compatible, avoiding use of extras.", +) @click.option( "--generate-hashes", is_flag=True, @@ -236,6 +242,7 @@ def cli( upgrade_packages: Tuple[str, ...], output_file: Union[LazyFile, IO[Any], None], allow_unsafe: bool, + strip_extras: bool, generate_hashes: bool, reuse_hashes: bool, src_files: Tuple[str, ...], @@ -457,6 +464,7 @@ def cli( emit_index_url=emit_index_url, emit_trusted_host=emit_trusted_host, annotate=annotate, + strip_extras=strip_extras, generate_hashes=generate_hashes, default_index_url=repository.DEFAULT_INDEX_URL, index_urls=repository.finder.index_urls, diff --git a/piptools/writer.py b/piptools/writer.py index d07ee677f..1b3ab8cc5 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -61,6 +61,7 @@ def __init__( emit_index_url: bool, emit_trusted_host: bool, annotate: bool, + strip_extras: bool, generate_hashes: bool, default_index_url: str, index_urls: Iterable[str], @@ -77,6 +78,7 @@ def __init__( self.emit_index_url = emit_index_url self.emit_trusted_host = emit_trusted_host self.annotate = annotate + self.strip_extras = strip_extras self.generate_hashes = generate_hashes self.default_index_url = default_index_url self.index_urls = index_urls @@ -234,6 +236,8 @@ def _format_requirement( ireq_hashes = (hashes if hashes is not None else {}).get(ireq) line = format_requirement(ireq, marker=marker, hashes=ireq_hashes) + if self.strip_extras: + line = re.sub(r"\[.+?\]", "", line) if not self.annotate: return line diff --git a/tests/conftest.py b/tests/conftest.py index 3ea43ff24..be063d121 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -244,10 +244,14 @@ def make_package(tmp_path): Make a package from a given name, version and list of required packages. """ - def _make_package(name, version="0.1", install_requires=None): + def _make_package(name, version="0.1", install_requires=None, extras_require=None): + if install_requires is None: install_requires = [] + if extras_require is None: + extras_require = dict() + install_requires_str = "[{}]".format( ",".join(f"{package!r}" for package in install_requires) ) @@ -267,6 +271,7 @@ def _make_package(name, version="0.1", install_requires=None): author_email="pip-tools@localhost", url="https://github.com/jazzband/pip-tools", install_requires={install_requires_str}, + extras_require={extras_require}, ) """ ) diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 572de0d5d..214e09bcf 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -1837,3 +1837,29 @@ def test_extras_fail_with_requirements_in(runner, tmpdir): assert out.exit_code == 2 exp = "--extra has effect only with setup.py and PEP-517 input formats" assert exp in out.stderr + + +def test_cli_compile_strip_extras(runner, make_package, make_sdist, tmpdir): + """ + Assures that --strip-extras removes mention of extras from output. + """ + test_package_1 = make_package( + "test_package_1", version="0.1", extras_require={"more": "test_package_2"} + ) + test_package_2 = make_package( + "test_package_2", + version="0.1", + ) + dists_dir = tmpdir / "dists" + + for pkg in (test_package_1, test_package_2): + make_sdist(pkg, dists_dir) + + with open("requirements.in", "w") as reqs_out: + reqs_out.write("test_package_1[more]") + + out = runner.invoke(cli, ["--strip-extras", "--find-links", str(dists_dir)]) + + assert out.exit_code == 0, out + assert "test-package-2==0.1" in out.stderr + assert "[more]" not in out.stderr diff --git a/tests/test_writer.py b/tests/test_writer.py index 6b79ed4ff..adbcc4731 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -44,6 +44,7 @@ def writer(tmpdir_cwd): allow_unsafe=False, find_links=[], emit_find_links=True, + strip_extras=False, ) yield writer