Skip to content

Commit aa913dd

Browse files
tlambert03ctrueden
authored andcommitted
reorg
1 parent 28af43b commit aa913dd

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

src/scyjava/_cjdk_fetch.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import logging
44
import os
5+
import shutil
6+
import subprocess
57
from typing import TYPE_CHECKING
68

9+
import jpype
10+
711
if TYPE_CHECKING:
812
from pathlib import Path
913

@@ -14,6 +18,34 @@
1418
_DEFAULT_JAVA_VERSION = "11"
1519

1620

21+
def ensure_jvm_available(raise_on_error: bool = True) -> None:
22+
"""Ensure that the JVM is available, or raise if `raise_on_error` is True."""
23+
if not is_jvm_available():
24+
cjdk_fetch_java(raise_on_error=raise_on_error)
25+
if not shutil.which("mvn"):
26+
cjdk_fetch_maven(raise_on_error=raise_on_error)
27+
28+
29+
def is_jvm_available() -> bool:
30+
"""Return True if the JVM is available, suppressing stderr on macos."""
31+
from unittest.mock import patch
32+
33+
subprocess_check_output = subprocess.check_output
34+
35+
def _silent_check_output(*args, **kwargs):
36+
# also suppress stderr on calls to subprocess.check_output
37+
kwargs.setdefault("stderr", subprocess.DEVNULL)
38+
return subprocess_check_output(*args, **kwargs)
39+
40+
try:
41+
with patch.object(subprocess, "check_output", new=_silent_check_output):
42+
jpype.getDefaultJVMPath()
43+
# on Darwin, may raise a CalledProcessError when invoking `/user/libexec/java_home`
44+
except (jpype.JVMNotFoundException, subprocess.CalledProcessError):
45+
return False
46+
return True
47+
48+
1749
def cjdk_fetch_java(
1850
vendor: str = "", version: str = "", raise_on_error: bool = True
1951
) -> None:
@@ -25,7 +57,7 @@ def cjdk_fetch_java(
2557
raise ImportError(
2658
"No JVM found. Please install `cjdk` to use the fetch_java feature."
2759
) from e
28-
_logger.info("cjdk is not installed. Skipping automatic fetching of java.")
60+
_logger.info("JVM not found. Please install `cjdk` fetch java automatically.")
2961
return
3062

3163
if not vendor:
@@ -47,7 +79,9 @@ def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True)
4779
raise ImportError(
4880
"Please install `cjdk` to use the fetch_java feature."
4981
) from e
50-
_logger.info("cjdk is not installed. Skipping automatic fetching of Maven.")
82+
_logger.info(
83+
"Maven not found. Please install `cjdk` fetch maven automatically."
84+
)
5185
return
5286

5387
# if url was passed as an argument, or env_var, use it with provided sha

src/scyjava/_jvm.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import logging
77
import os
88
import re
9-
import shutil
109
import subprocess
1110
import sys
1211
from functools import lru_cache
@@ -17,7 +16,7 @@
1716
import jpype.config
1817
from jgo import jgo
1918

20-
from scyjava._cjdk_fetch import cjdk_fetch_java, cjdk_fetch_maven
19+
from scyjava._cjdk_fetch import ensure_jvm_available
2120
import scyjava.config
2221
from scyjava.config import Mode, mode
2322

@@ -120,10 +119,12 @@ def start_jvm(options=None, *, fetch_java: bool | None = None) -> None:
120119
List of options to pass to the JVM.
121120
For example: ['-Dfoo=bar', '-XX:+UnlockExperimentalVMOptions']
122121
:param fetch_java:
123-
Whether to automatically fetch a JRE (and/or maven) using
122+
Whether to automatically fetch a JRE (and maven) using
124123
[`cjdk`](https://github.com/cachedjdk/cjdk) if java and maven executables are
125-
not found. Requires `cjdk` to be installed. See README for details.
124+
not found. Requires `cjdk` to be installed, either manually, or via the
125+
`scyjava[cjdk]` extra.
126126
- If `None` (default), then fetching will only occur if `cjdk` is available.
127+
(A log info will be issued if `cjdk` is not available.)
127128
- If `True`, an exception will be raised if `cjdk` is not available.
128129
- If `False`, no attempt to import `cjdk` is be made.
129130
"""
@@ -141,14 +142,11 @@ def start_jvm(options=None, *, fetch_java: bool | None = None) -> None:
141142
# use the logger to notify user that endpoints are being added
142143
_logger.debug("Adding jars from endpoints {0}".format(endpoints))
143144

144-
if fetch_java is not False and not is_jvm_available():
145-
cjdk_fetch_java(raise_on_error=fetch_java is True)
145+
if fetch_java is not False:
146+
ensure_jvm_available(raise_on_error=fetch_java is True)
146147

147148
# get endpoints and add to JPype class path
148149
if len(endpoints) > 0:
149-
if not shutil.which("mvn") and fetch_java is not False:
150-
cjdk_fetch_maven(raise_on_error=fetch_java is True)
151-
152150
endpoints = endpoints[:1] + sorted(endpoints[1:])
153151
_logger.debug("Using endpoints %s", endpoints)
154152
_, workspace = jgo.resolve_dependencies(
@@ -355,28 +353,6 @@ def is_jvm_headless() -> bool:
355353
return bool(GraphicsEnvironment.isHeadless())
356354

357355

358-
def is_jvm_available() -> bool:
359-
"""
360-
Return True if the JVM is available, suppressing stderr on macos.
361-
"""
362-
from unittest.mock import patch
363-
364-
subprocess_check_output = subprocess.check_output
365-
366-
def _silent_check_output(*args, **kwargs):
367-
# also suppress stderr on calls to subprocess.check_output
368-
kwargs.setdefault("stderr", subprocess.DEVNULL)
369-
return subprocess_check_output(*args, **kwargs)
370-
371-
try:
372-
with patch.object(subprocess, "check_output", new=_silent_check_output):
373-
jpype.getDefaultJVMPath()
374-
# on Darwin, may raise a CalledProcessError when invoking `/user/libexec/java_home`
375-
except (jpype.JVMNotFoundException, subprocess.CalledProcessError):
376-
return False
377-
return True
378-
379-
380356
def is_awt_initialized() -> bool:
381357
"""
382358
Return true iff the AWT subsystem has been initialized.

0 commit comments

Comments
 (0)