#!/usr/bin/env python3 """ Compile any `requirements.in` files into a list of exact versions in `requirements.txt`. * If you pass the `--upgrade` flag, it will upgrade all the requirements to the latest version. """ from pathlib import Path import shlex import subprocess import sys def compile_requirements_file(in_file: str, *, upgrade: bool, no_cache: bool) -> None: """ Compile a single requirements file. """ assert in_file.endswith(".in") txt_file = in_file.replace(".in", ".txt") assert in_file != txt_file # If this `.in` file doesn't exist. if not Path(in_file).exists(): return # Construct the `uv pip compile` command. cmd = [ "uv", "pip", "compile", in_file, f"--output-file={txt_file}", # # Exclude dependencies which are less than 7 days old; this is # a mitigation against supply chain attacks and installing # recently-published malicious code on my computer. "--exclude-newer=P7D", # # Allow installing new versions of my own packages "--exclude-newer-package", "alexwlchan-chives=false", ] 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 # stdout, so we pipe that to /dev/null -- we're writing it to a file. subprocess.check_call( ["/Users/alexwlchan/repos/scripts/debug/print_info", f"-> {shlex.join(cmd)}"] ) subprocess.check_call(cmd, stdout=subprocess.DEVNULL) if __name__ == "__main__": for f in ["requirements.in", "dev_requirements.in"]: compile_requirements_file( f, upgrade="--upgrade" in sys.argv, no_cache="--no-cache" in sys.argv )