|
13 | 13 | from packaging.utils import NormalizedName, canonicalize_name |
14 | 14 | from packaging.version import Version |
15 | 15 |
|
16 | | -from . import constraints, dependency_graph, packagesettings, request_session |
| 16 | +from . import ( |
| 17 | + constraints, |
| 18 | + dependency_graph, |
| 19 | + external_commands, |
| 20 | + packagesettings, |
| 21 | + request_session, |
| 22 | +) |
17 | 23 |
|
18 | 24 | if typing.TYPE_CHECKING: |
19 | 25 | from . import build_environment |
@@ -126,6 +132,26 @@ def pip_constraint_args(self) -> list[str]: |
126 | 132 | path_to_constraints_file = path_to_constraints_file.absolute() |
127 | 133 | return ["--constraint", os.fspath(path_to_constraints_file)] |
128 | 134 |
|
| 135 | + def uv_clean_cache(self, *reqs: Requirement) -> None: |
| 136 | + """Invalidate and clean uv cache for requirements |
| 137 | +
|
| 138 | + uv caches package metadata and unpacked wheels for faster dependency |
| 139 | + resolution and installation. ``uv pip install`` hardlinks files from |
| 140 | + cache location. This function removes a package from all caches, so |
| 141 | + subsequent installations use a new built. |
| 142 | +
|
| 143 | + WARNING: 'uv clean cache' is not concurrency safe with 'uv pip install'. |
| 144 | + """ |
| 145 | + if not reqs: |
| 146 | + raise ValueError("no requirements") |
| 147 | + |
| 148 | + extra_environ: dict[str, str] = {"UV_CACHE_DIR": str(self.uv_cache)} |
| 149 | + cmd = ["uv", "clean", "cache"] |
| 150 | + req_list: list[str] = sorted(set(req.name for req in reqs)) |
| 151 | + logger.debug("invalidate uv cache for %s", req_list) |
| 152 | + cmd.extend(req_list) |
| 153 | + external_commands.run(cmd, extra_environ=extra_environ) |
| 154 | + |
129 | 155 | def write_to_graph_to_file(self): |
130 | 156 | with self.graph_file.open("w", encoding="utf-8") as f: |
131 | 157 | self.dependency_graph.serialize(f) |
|
0 commit comments