diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 7bd6e0822..85e8a2595 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -179,6 +179,12 @@ type=click.Path(file_okay=False, writable=True), ) @click.option("--pip-args", help="Arguments to pass directly to the pip command.") +@click.option( + "--emit-options/--no-emit-options", + is_flag=True, + default=True, + help="Add options to generated file", +) def cli( ctx, verbose, @@ -207,6 +213,7 @@ def cli( emit_find_links, cache_dir, pip_args, + emit_options, ): """Compiles requirements.txt from requirements.in specs.""" log.verbosity = verbose - quiet @@ -421,6 +428,7 @@ def cli( allow_unsafe=allow_unsafe, find_links=repository.finder.find_links, emit_find_links=emit_find_links, + emit_options=emit_options, ) writer.write( results=results, diff --git a/piptools/writer.py b/piptools/writer.py index 694fd3cc1..782eba9ee 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -67,6 +67,7 @@ def __init__( allow_unsafe, find_links, emit_find_links, + emit_options, ): self.src_files = src_files self.dst_file = dst_file @@ -84,6 +85,7 @@ def __init__( self.allow_unsafe = allow_unsafe self.find_links = find_links self.emit_find_links = emit_find_links + self.emit_options = emit_options def _sort_key(self, ireq): return (not ireq.editable, str(ireq.req).lower()) @@ -125,6 +127,9 @@ def write_find_links(self): yield "--find-links {}".format(find_link) def write_flags(self): + if not self.emit_options: + return + emitted = False for line in chain( self.write_index_options(), diff --git a/tests/test_utils.py b/tests/test_utils.py index 5a92dae87..6e2b6ade5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -249,8 +249,10 @@ def test_force_text(value, expected_text): (["--no-index"], "pip-compile --no-index"), (["--no-emit-trusted-host"], "pip-compile --no-emit-trusted-host"), (["--no-annotate"], "pip-compile --no-annotate"), + (["--no-emit-options"], "pip-compile --no-emit-options"), # Check that default values will be removed from the command (["--emit-trusted-host"], "pip-compile"), + (["--emit-options"], "pip-compile"), (["--annotate"], "pip-compile"), (["--index"], "pip-compile"), (["--max-rounds=10"], "pip-compile"), diff --git a/tests/test_writer.py b/tests/test_writer.py index f1fc3e08c..46ab99e32 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -42,6 +42,7 @@ def writer(tmpdir_cwd): allow_unsafe=False, find_links=[], emit_find_links=True, + emit_options=True, ) yield writer @@ -224,6 +225,40 @@ def test_write_header_no_emit_header(writer): next(writer.write_header()) +def test_write_flags_emit_options(writer): + """ + There should be options if emit_options is True + """ + writer.emit_options = True + writer.index_urls = ["https://index-server"] + writer.find_links = ["links"] + writer.trusted_hosts = ["index-server"] + writer.format_control = FormatControl(no_binary=["flask"], only_binary=["django"]) + + assert tuple(writer.write_flags()) == ( + "--index-url https://index-server", + "--find-links links", + "--trusted-host index-server", + "--no-binary flask", + "--only-binary django", + "", + ) + + +def test_write_flags_no_emit_options(writer): + """ + There should not be options if emit_options is False + """ + writer.emit_options = False + writer.index_urls = ["https://index-server"] + writer.find_links = ["links"] + writer.trusted_hosts = ["index-server"] + writer.format_control = FormatControl(no_binary=["flask"], only_binary=["django"]) + + with raises(StopIteration): + next(writer.write_flags()) + + def test_write_format_controls(writer): """ Tests --no-binary/--only-binary options.