Skip to main content

python/pip_compile: allow updating specific package versions

ID
c77abe6
date
2026-07-26 19:48:15+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
e5a7eac
message
python/pip_compile: allow updating specific package versions
changed files
1 file, 44 additions, 5 deletions

Changed files

python/pip_compile (1879 → 2960)

diff --git a/python/pip_compile b/python/pip_compile
index aefaefc..7083582 100755
--- a/python/pip_compile
+++ b/python/pip_compile
@@ -5,16 +5,20 @@ in `requirements.txt`.
 
 * If you pass the `--upgrade` flag, it will upgrade all the requirements
   to the latest version.
+* If you pass `--upgrade-package <package>`, it will upgrade only that
+  specific package.
 
 """
 
-from pathlib import Path
+import argparse
 import shlex
 import subprocess
-import sys
+from pathlib import Path
 
 
-def compile_requirements_file(in_file: str, *, upgrade: bool, no_cache: bool) -> None:
+def compile_requirements_file(
+    in_file: str, *, upgrade: bool, upgrade_packages: list[str] | None, no_cache: bool
+) -> None:
     """
     Compile a single requirements file.
     """
@@ -40,12 +44,18 @@ def compile_requirements_file(in_file: str, *, upgrade: bool, no_cache: bool) ->
         "--exclude-newer=P7D",
         #
         # Allow installing new versions of my own packages
-        "--exclude-newer-package", "alexwlchan-chives=false",
+        "--exclude-newer-package",
+        "alexwlchan-chives=false",
     ]
 
     if upgrade:
         cmd.append("--upgrade")
 
+    if upgrade_packages:
+        for pkg in upgrade_packages:
+            cmd.extend(["--upgrade-package", pkg])
+            cmd.extend(["--exclude-newer-package", f"{pkg}=false"])
+
     if no_cache:
         cmd.append("--no-cache")
 
@@ -59,8 +69,37 @@ def compile_requirements_file(in_file: str, *, upgrade: bool, no_cache: bool) ->
     subprocess.check_call(cmd, stdout=subprocess.DEVNULL)
 
 
+def parse_args() -> argparse.Namespace:
+    parser = argparse.ArgumentParser(
+        description="Compile requirements.in files into requirements.txt"
+    )
+    parser.add_argument(
+        "--upgrade",
+        action="store_true",
+        help="Upgrade all packages to their latest versions",
+    )
+    parser.add_argument(
+        "--upgrade-package",
+        action="append",
+        dest="upgrade_packages",
+        metavar="PACKAGE",
+        help="Upgrade a specific package (can be passed multiple times)",
+    )
+    parser.add_argument(
+        "--no-cache",
+        action="store_true",
+        help="Disable caching in uv",
+    )
+    return parser.parse_args()
+
+
 if __name__ == "__main__":
+    args = parse_args()
+
     for f in ["requirements.in", "dev_requirements.in"]:
         compile_requirements_file(
-            f, upgrade="--upgrade" in sys.argv, no_cache="--no-cache" in sys.argv
+            f,
+            upgrade=args.upgrade,
+            upgrade_packages=args.upgrade_packages,
+            no_cache=args.no_cache,
         )