Skip to content

Commit

Permalink
Add option for producing constraint compatible output (#1404)
Browse files Browse the repository at this point in the history
Fixes: #1300
  • Loading branch information
ssbarnea committed Jun 11, 2021
1 parent c7e6d84 commit f031f69
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
8 changes: 8 additions & 0 deletions piptools/scripts/compile.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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, ...],
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions piptools/writer.py
Expand Up @@ -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],
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Expand Up @@ -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)
)
Expand All @@ -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},
)
"""
)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_cli_compile.py
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/test_writer.py
Expand Up @@ -44,6 +44,7 @@ def writer(tmpdir_cwd):
allow_unsafe=False,
find_links=[],
emit_find_links=True,
strip_extras=False,
)
yield writer

Expand Down

0 comments on commit f031f69

Please sign in to comment.