Skip to content

Commit 127f203

Browse files
committed
Make "always" fetch mode actually *always* fetch
The JavaSource.AUTO to jgo was hardcoded. And the reason was that jgo did not actually have a JavaSource.DOWNLOAD to force cjdk-based resolution. But as of jgo 2.2.0, it now does, so we can use it here. We now fully lean on jgo's JDK/JRE resolution mechanism in all cases, and stop trying to be smarter than jgo downstream here in scyjava. We also support "download" and "system" for fetch_java now, since that is the terminology used in the jgo project, and it would be confusing (to me, at least) if those values mapped silently to AUTO.
1 parent c6fc2b3 commit 127f203

File tree

3 files changed

+22
-41
lines changed

3 files changed

+22
-41
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ classifiers = [
3333
requires-python = ">=3.10"
3434
dependencies = [
3535
"jpype1 >= 1.3.0",
36-
"jgo>=2.1.0",
36+
"jgo>=2.2.0",
3737
]
3838

3939
[dependency-groups]

src/scyjava/_jdk_fetch.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
import logging
88
import os
9-
import subprocess
109
from typing import TYPE_CHECKING, Union
1110

12-
import jpype
13-
1411
from jgo.exec import JavaLocator, JavaSource
1512

1613
import scyjava.config
@@ -21,41 +18,11 @@
2118
_logger = logging.getLogger(__name__)
2219

2320

24-
def ensure_jvm_available() -> None:
25-
"""
26-
Ensure that the JVM is available.
21+
def resolve_java(vendor: str | None = None, version: str | None = None) -> None:
2722
"""
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.
23+
Resolve JDK installation location and configure PATH/JAVA_HOME.
24+
Might download Java or use the system Java, depending on the
25+
scyjava.config.fetch_java setting.
5926
6027
Supports cjdk version syntax including "11", "17", "11+", "17+", etc.
6128
See https://pypi.org/project/cjdk for more information.
@@ -67,8 +34,22 @@ def fetch_java(vendor: str | None = None, version: str | None = None) -> None:
6734

6835
_logger.info(f"Fetching {vendor}:{version}...")
6936

37+
fetch = scyjava.config.get_fetch_java()
38+
39+
# Map scyjava fetch mode to jgo JavaSource strategy.
40+
# "always" -> DOWNLOAD: always use cjdk-managed Java, ignoring system Java.
41+
# "never" -> SYSTEM: always use system Java, never downloading via cjdk.
42+
# "auto" -> AUTO: prefer system Java, fall back to cjdk if absent/too old.
43+
_FETCH_MODES = {
44+
"always": JavaSource.DOWNLOAD,
45+
"download": JavaSource.DOWNLOAD,
46+
"never": JavaSource.SYSTEM,
47+
"system": JavaSource.SYSTEM,
48+
}
49+
java_source = _FETCH_MODES.get(fetch, JavaSource.AUTO)
50+
7051
locator = JavaLocator(
71-
java_source=JavaSource.AUTO,
52+
java_source=java_source,
7253
java_version=version, # Pass string directly (e.g. "11", "17", "11+", "17+")
7354
java_vendor=vendor,
7455
verbose=True,

src/scyjava/_jvm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import scyjava.config
2121
from scyjava.config import Mode, mode
22-
from scyjava._jdk_fetch import ensure_jvm_available
22+
from scyjava._jdk_fetch import resolve_java
2323

2424
_logger = logging.getLogger(__name__)
2525

@@ -175,7 +175,7 @@ def start_jvm(options: Sequence[str] | None = None) -> None:
175175
_logger.debug("Adding jars from endpoints {0}".format(endpoints))
176176

177177
# download Java as appropriate
178-
ensure_jvm_available()
178+
resolve_java()
179179

180180
# Fail fast if Java version is too old. JPype 1.6+ dropped Java 8 support.
181181
try:

0 commit comments

Comments
 (0)