Skip to main content

Add a --no-cache flag to pip_sync

ID
febfafd
date
2025-04-30 10:33:12+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
54b892e
message
Add a `--no-cache` flag to `pip_sync`
changed files
4 files, 58 additions, 24 deletions

Changed files

fish_functions/pip_sync.fish (1520) → fish_functions/pip_sync.fish (1017)

diff --git a/fish_functions/pip_sync.fish b/fish_functions/pip_sync.fish
index cd28e6d..bb11274 100644
--- a/fish_functions/pip_sync.fish
+++ b/fish_functions/pip_sync.fish
@@ -1,11 +1,7 @@
 function pip_sync --description "Make a virtualenv dependencies look like requirements.txt"
 
     # Run the `pip compile` script to get a set of version pins.
-    if contains -- --upgrade $argv
-        pip_compile --upgrade
-    else
-        pip_compile
-    end
+    pip_compile $argv
 
     # If there isn't a virtualenv already, create one
     if test -z "$VIRTUAL_ENV"
@@ -29,14 +25,5 @@ function pip_sync --description "Make a virtualenv dependencies look like requir
     end
 
     # Actually run the `uv pip sync` command.
-    #
-    # If a dev_requirements.txt file is available, use that, otherwise use 
-    # the standard `requirements.txt`.
-    if test \( -e dev_requirements.txt \)
-        ~/repos/scripts/debug/print_info "-> uv pip sync dev_requirements.txt"
-        uv pip sync dev_requirements.txt
-    else if test \( -e requirements.txt \)
-        ~/repos/scripts/debug/print_info "-> uv pip sync requirements.txt"
-        uv pip sync requirements.txt
-    end
+    run_pip_sync $argv
 end

python/README.md (1207) → python/README.md (1629)

diff --git a/python/README.md b/python/README.md
index 091f94e..6193cfa 100644
--- a/python/README.md
+++ b/python/README.md
@@ -22,9 +22,13 @@ scripts = [
         "description": "Format Python files with ruff.",
     },
     {
-        "name": "pip_compile (--upgrade)",
+        "name": "pip_compile (--upgrade) (--no-cache)",
         "description": "Compile any `requirements.in` files into a list of exact versions in `requirements.txt`.",
     },
+    {
+        "name": "run_pip_sync (--no-cache)",
+        "description": "A wrapper around `uv pip sync`. You don't need to call this directly.",
+    },
 ]
 
 cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
@@ -42,11 +46,20 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
 
   <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/python/pip_compile">
-      <code>pip_compile (--upgrade)</code>
+      <code>pip_compile (--upgrade) (--no-cache)</code>
     </a>
   </dt>
   <dd>
     Compile any `requirements.in` files into a list of exact versions in `requirements.txt`.
   </dd>
+
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/python/run_pip_sync">
+      <code>run_pip_sync (--no-cache)</code>
+    </a>
+  </dt>
+  <dd>
+    A wrapper around `uv pip sync`. You don't need to call this directly.
+  </dd>
 </dl>
-<!-- [[[end]]] (checksum: 3e15017c8de1c144defe8f18a08d38b1) -->
+<!-- [[[end]]] (checksum: bc5aac2674e0836cb28c25fa59413628) -->

python/pip_compile (1457) → python/pip_compile (1486)

diff --git a/python/pip_compile b/python/pip_compile
index 319455d..0d06f7c 100755
--- a/python/pip_compile
+++ b/python/pip_compile
@@ -14,7 +14,7 @@ import subprocess
 import sys
 
 
-def compile_requirements_file(in_file: str, *, upgrade: bool) -> None:
+def compile_requirements_file(in_file: str, *, upgrade: bool, no_cache: bool) -> None:
     """
     Compile a single requirements file.
     """
@@ -32,6 +32,9 @@ def compile_requirements_file(in_file: str, *, upgrade: bool) -> None:
     if upgrade:
         cmd.append("--upgrade")
 
+    if no_cache:
+        cmd.append("--no-cache")
+
     # Actually run the command, and print a debug entry for it.
     #
     # `uv pip compile` prints the generated `requirements.txt` file to
@@ -43,8 +46,7 @@ def compile_requirements_file(in_file: str, *, upgrade: bool) -> None:
 
 
 if __name__ == "__main__":
-    upgrade = "--upgrade" in sys.argv
-
-    compile_requirements_file("requirements.in", upgrade=upgrade)
-    compile_requirements_file("script_requirements.in", upgrade=upgrade)
-    compile_requirements_file("dev_requirements.in", upgrade=upgrade)
+    for f in ["requirements.in", "script_requirements.in", "dev_requirements.in"]:
+        compile_requirements_file(
+            f, upgrade="--upgrade" in sys.argv, no_cache="--no-cache" in sys.argv
+        )

python/run_pip_sync (0) → python/run_pip_sync (878)

diff --git a/python/run_pip_sync b/python/run_pip_sync
new file mode 100755
index 0000000..465dc45
--- /dev/null
+++ b/python/run_pip_sync
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+"""
+Run `uv pip sync` with a `requirements.txt` file in the current folder.
+
+This will pick which file to use.
+"""
+
+from pathlib import Path
+import shlex
+import subprocess
+import sys
+
+
+if __name__ == "__main__":
+    # If a dev_requirements.txt file is available, use that, otherwise use
+    # the standard `requirements.txt`.
+    if Path("dev_requirements.txt").exists():
+        chosen_file = "dev_requirements.txt"
+    else:
+        chosen_file = "requirements.txt"
+
+    # Build the command to run.
+    cmd = ["uv", "pip", "sync", chosen_file]
+
+    if "--no-cache" in sys.argv:
+        cmd.append("--no-cache")
+
+    # Actually run the command, and print a debug entry for it.
+    subprocess.check_call(
+        ["/Users/alexwlchan/repos/scripts/debug/print_info", f"-> {shlex.join(cmd)}"]
+    )
+    subprocess.check_call(cmd, stdout=subprocess.DEVNULL)