|
| 1 | +""" |
| 2 | +Utility functions for fetching JDK/JRE. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import logging |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +from typing import TYPE_CHECKING, Union |
| 11 | + |
| 12 | +import jpype |
| 13 | + |
| 14 | +from jgo.exec import JavaLocator, JavaSource |
| 15 | + |
| 16 | +import scyjava.config |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from pathlib import Path |
| 20 | + |
| 21 | +_logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def ensure_jvm_available() -> None: |
| 25 | + """ |
| 26 | + Ensure that the JVM is available. |
| 27 | + """ |
| 28 | + fetch = scyjava.config.get_fetch_java() |
| 29 | + if fetch == "never": |
| 30 | + # Not allowed to fetch Java. |
| 31 | + return |
| 32 | + if fetch == "always" or not is_jvm_available(): |
| 33 | + fetch_java() |
| 34 | + |
| 35 | + |
| 36 | +def is_jvm_available() -> bool: |
| 37 | + """Return True if the JVM is available, suppressing stderr on macos.""" |
| 38 | + from unittest.mock import patch |
| 39 | + |
| 40 | + subprocess_check_output = subprocess.check_output |
| 41 | + |
| 42 | + def _silent_check_output(*args, **kwargs): |
| 43 | + # also suppress stderr on calls to subprocess.check_output |
| 44 | + kwargs.setdefault("stderr", subprocess.DEVNULL) |
| 45 | + return subprocess_check_output(*args, **kwargs) |
| 46 | + |
| 47 | + try: |
| 48 | + with patch.object(subprocess, "check_output", new=_silent_check_output): |
| 49 | + jpype.getDefaultJVMPath() |
| 50 | + # on Darwin, may raise a CalledProcessError when invoking `/usr/libexec/java_home` |
| 51 | + except (jpype.JVMNotFoundException, subprocess.CalledProcessError): |
| 52 | + return False |
| 53 | + return True |
| 54 | + |
| 55 | + |
| 56 | +def fetch_java(vendor: str | None = None, version: str | None = None) -> None: |
| 57 | + """ |
| 58 | + Fetch Java and configure PATH/JAVA_HOME. |
| 59 | +
|
| 60 | + Supports cjdk version syntax including "11", "17", "11+", "17+", etc. |
| 61 | + See https://pypi.org/project/cjdk for more information. |
| 62 | + """ |
| 63 | + if vendor is None: |
| 64 | + vendor = scyjava.config.get_java_vendor() |
| 65 | + if version is None: |
| 66 | + version = scyjava.config.get_java_version() |
| 67 | + |
| 68 | + _logger.info(f"Fetching {vendor}:{version}...") |
| 69 | + |
| 70 | + locator = JavaLocator( |
| 71 | + java_source=JavaSource.AUTO, |
| 72 | + java_version=version, # Pass string directly (e.g. "11", "17", "11+", "17+") |
| 73 | + java_vendor=vendor, |
| 74 | + verbose=True, |
| 75 | + ) |
| 76 | + |
| 77 | + # Locate returns path to java executable (e.g., /path/to/java/bin/java) |
| 78 | + java_exe = locator.locate() |
| 79 | + java_home = java_exe.parent.parent # Navigate from bin/java to JAVA_HOME |
| 80 | + |
| 81 | + _logger.debug(f"java_home -> {java_home}") |
| 82 | + _add_to_path(str(java_home / "bin"), front=True) |
| 83 | + os.environ["JAVA_HOME"] = str(java_home) |
| 84 | + |
| 85 | + |
| 86 | +def _add_to_path(path: Union[Path, str], front: bool = False) -> None: |
| 87 | + """Add a path to the PATH environment variable. |
| 88 | +
|
| 89 | + If front is True, the path is added to the front of the PATH. |
| 90 | + By default, the path is added to the end of the PATH. |
| 91 | + If the path is already in the PATH, it is not added again. |
| 92 | + """ |
| 93 | + |
| 94 | + current_path = os.environ.get("PATH", "") |
| 95 | + if (path := str(path)) in current_path: |
| 96 | + return |
| 97 | + new_path = [path, current_path] if front else [current_path, path] |
| 98 | + os.environ["PATH"] = os.pathsep.join(new_path) |
0 commit comments