Repository navigation
Expand file tree
/
Copy path_jdk_fetch.py
More file actions
109 lines (87 loc) · 3.71 KB
/
Copy path_jdk_fetch.py
File metadata and controls
109 lines (87 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Utility functions for fetching JDK/JRE.
"""
from __future__ import annotations
import logging
import os
import subprocess
from pathlib import Path
from jgo.exec import JavaLocator, JavaSource
import scyjava.config
_logger = logging.getLogger(__name__)
def resolve_java(vendor: str | None = None, version: str | None = None) -> None:
"""
Resolve JDK installation location and configure PATH/JAVA_HOME.
Might download Java or use the system Java, depending on the
scyjava.config.fetch_java setting.
Supports cjdk version syntax including "11", "17", "11+", "17+", etc.
See https://pypi.org/project/cjdk for more information.
"""
if vendor is None:
vendor = scyjava.config.get_java_vendor()
if version is None:
version = scyjava.config.get_java_version()
fetch = scyjava.config.get_fetch_java()
# Note: Whether Java gets downloaded is up to jgo's JavaLocator, which
# logs what it actually ends up using.
_logger.debug(f"Resolving Java {vendor}:{version} (fetch mode: {fetch})")
# Map scyjava fetch mode to jgo JavaSource strategy.
# "always" -> DOWNLOAD: always use cjdk-managed Java, ignoring system Java.
# "never" -> SYSTEM: always use system Java, never downloading via cjdk.
# "auto" -> AUTO: prefer system Java, fall back to cjdk if absent/too old.
_FETCH_MODES = {
"always": JavaSource.DOWNLOAD,
"download": JavaSource.DOWNLOAD,
"never": JavaSource.SYSTEM,
"system": JavaSource.SYSTEM,
}
java_source = _FETCH_MODES.get(fetch, JavaSource.AUTO)
locator = JavaLocator(
java_source=java_source,
java_version=version, # Pass string directly (e.g. "11", "17", "11+", "17+")
java_vendor=vendor,
verbose=True,
)
# Locate returns path to java executable (e.g., /path/to/java/bin/java)
java_exe = locator.locate()
java_home = _java_home(java_exe)
_logger.debug(f"java_home -> {java_home}")
_add_to_path(str(java_home / "bin"), front=True)
os.environ["JAVA_HOME"] = str(java_home)
def _java_home(java_exe: Path) -> Path:
"""
Determine the Java installation directory of the given java executable.
The executable is asked for its java.home property, because it is not
necessarily located at JAVA_HOME/bin/java: it might be a symlink (e.g.
/usr/bin/java with Linux alternatives) or a launcher stub (e.g. macOS's
/usr/bin/java, which delegates to the registered JDK).
"""
try:
output = subprocess.run(
[str(java_exe), "-XshowSettings:properties", "-version"],
check=False,
capture_output=True,
text=True,
timeout=30,
).stderr
for line in output.splitlines():
key, sep, value = line.partition("=")
if sep and key.strip() == "java.home":
java_home = Path(value.strip())
if java_home.is_dir():
return java_home
except (OSError, subprocess.SubprocessError) as e:
_logger.debug(f"Failed to query java.home from {java_exe}: {e}")
_logger.debug(f"Could not query java.home from {java_exe}; assuming bin/java")
return java_exe.parent.parent
def _add_to_path(path: Path | str, front: bool = False) -> None:
"""Add a path to the PATH environment variable.
If front is True, the path is added to the front of the PATH.
By default, the path is added to the end of the PATH.
If the path is already in the PATH, it is not added again.
"""
current_path = os.environ.get("PATH", "")
if (path := str(path)) in current_path:
return
new_path = [path, current_path] if front else [current_path, path]
os.environ["PATH"] = os.pathsep.join(new_path)