| 544 | |
| 545 | |
| 546 | def ensure_uv_python( |
| 547 | python, # type: Union[UvPython, str] |
| 548 | install_if_missing=True, # type: bool |
| 549 | ): |
| 550 | # type: (...) -> str |
| 551 | |
| 552 | python_spec = python.spec() if isinstance(python, UvPython) else python |
| 553 | |
| 554 | env = make_env(UV_PYTHON_INSTALL_DIR=os.path.join(PEX_TEST_DEV_ROOT, "uv-pythons")) |
| 555 | process = subprocess.Popen( |
| 556 | args=["uv", "python", "find", python_spec], env=env, stdout=subprocess.PIPE |
| 557 | ) |
| 558 | stdout, _ = process.communicate() |
| 559 | if process.returncode == 0: |
| 560 | return cast(str, stdout.decode("utf-8").strip()) |
| 561 | |
| 562 | assert install_if_missing, "The {python} interpreter was not found by uv on the system.".format( |
| 563 | python=python |
| 564 | ) |
| 565 | subprocess.check_call( |
| 566 | args=["uv", "python", "install", "--managed-python", "--force", python_spec], env=env |
| 567 | ) |
| 568 | return ensure_uv_python(python_spec, install_if_missing=False) |
| 569 | |
| 570 | |
| 571 | def run_pex_command( |