From fb5a60ef01226225331e07cda961abbf6f7e2728 Mon Sep 17 00:00:00 2001 From: Nathan Jessurun Date: Mon, 21 Nov 2022 13:21:30 -0500 Subject: [PATCH] Introduce `--add-package/-A` pip-compile flag --- piptools/scripts/compile.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 8438e7f55..569794bbf 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -182,6 +182,15 @@ def _determine_linesep( multiple=True, help="Specify a particular package to upgrade; may be used more than once", ) +@click.option( + "-A", + "--add-package", + "add_packages", + nargs=1, + multiple=True, + help="Like --upgrade-package, but only for packages that are not already " + "in the input file; may be used more than once", +) @click.option( "-o", "--output-file", @@ -312,6 +321,7 @@ def cli( annotation_style: str, upgrade: bool, upgrade_packages: tuple[str, ...], + add_packages: tuple[str, ...], output_file: LazyFile | IO[Any] | None, newline: str, allow_unsafe: bool, @@ -416,6 +426,9 @@ def cli( upgrade_install_reqs = { key_from_ireq(install_req): install_req for install_req in upgrade_reqs_gen } + # Similar procedure for --add-package/-E but the key is not needed since + # the package doesn't need to be checked against the internal list + add_reqs_list = [install_req_from_line(pkg) for pkg in add_packages] existing_pins_to_upgrade = set() @@ -517,11 +530,13 @@ def cli( key_from_ireq(ireq) for ireq in constraints if not ireq.constraint } - allowed_upgrades = primary_packages | existing_pins_to_upgrade - constraints.extend( - ireq for key, ireq in upgrade_install_reqs.items() if key in allowed_upgrades + allowed_upgrades = (primary_packages | existing_pins_to_upgrade).intersection( + upgrade_install_reqs ) + constraints.extend(upgrade_install_reqs[key] for key in allowed_upgrades) + constraints.extend(add_reqs_list) + constraints = [req for req in constraints if req.match_markers(extras)] for req in constraints: drop_extras(req)