Skip to content

Commit ccb2271

Browse files
committed
include cjdk by default
1 parent 64ffbee commit ccb2271

File tree

3 files changed

+29
-46
lines changed

3 files changed

+29
-46
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ requires-python = ">=3.8"
3535
dependencies = [
3636
"jpype1 >= 1.3.0",
3737
"jgo",
38+
"cjdk",
3839
]
3940

4041
[project.optional-dependencies]
4142
# NB: Keep this in sync with dev-environment.yml!
42-
cjdk = ["cjdk"]
4343
dev = [
44-
"scyjava[cjdk]",
4544
"assertpy",
4645
"build",
4746
"jep",

src/scyjava/_cjdk_fetch.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
from typing import TYPE_CHECKING, Union
88

9+
import cjdk
910
import jpype
1011

1112
if TYPE_CHECKING:
@@ -18,12 +19,12 @@
1819
_DEFAULT_JAVA_VERSION = "11"
1920

2021

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."""
22+
def ensure_jvm_available() -> None:
23+
"""Ensure that the JVM is available and Maven is installed."""
2324
if not is_jvm_available():
24-
cjdk_fetch_java(raise_on_error=raise_on_error)
25+
cjdk_fetch_java()
2526
if not shutil.which("mvn"):
26-
cjdk_fetch_maven(raise_on_error=raise_on_error)
27+
cjdk_fetch_maven()
2728

2829

2930
def is_jvm_available() -> bool:
@@ -46,20 +47,8 @@ def _silent_check_output(*args, **kwargs):
4647
return True
4748

4849

49-
def cjdk_fetch_java(
50-
vendor: str = "", version: str = "", raise_on_error: bool = True
51-
) -> None:
50+
def cjdk_fetch_java(vendor: str = "", version: str = "") -> None:
5251
"""Fetch java using cjdk and add it to the PATH."""
53-
try:
54-
import cjdk
55-
except ImportError as e:
56-
if raise_on_error is True:
57-
raise ImportError(
58-
"No JVM found. Please install `cjdk` to use the fetch_java feature."
59-
) from e
60-
_logger.info("JVM not found. Please install `cjdk` fetch java automatically.")
61-
return
62-
6352
if not vendor:
6453
vendor = os.getenv("JAVA_VENDOR", _DEFAULT_JAVA_VENDOR)
6554
version = os.getenv("JAVA_VERSION", _DEFAULT_JAVA_VERSION)
@@ -70,20 +59,8 @@ def cjdk_fetch_java(
7059
os.environ["JAVA_HOME"] = str(home)
7160

7261

73-
def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True) -> None:
62+
def cjdk_fetch_maven(url: str = "", sha: str = "") -> None:
7463
"""Fetch Maven using cjdk and add it to the PATH."""
75-
try:
76-
import cjdk
77-
except ImportError as e:
78-
if raise_on_error is True:
79-
raise ImportError(
80-
"Please install `cjdk` to use the fetch_java feature."
81-
) from e
82-
_logger.info(
83-
"Maven not found. Please install `cjdk` fetch maven automatically."
84-
)
85-
return
86-
8764
# if url was passed as an argument, or env_var, use it with provided sha
8865
# otherwise, use default values for both
8966
if url := url or os.getenv("MAVEN_URL", ""):
@@ -104,7 +81,7 @@ def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True)
10481
kwargs = {}
10582
if sha_len := len(sha): # empty sha is fine... we just don't pass it
10683
sha_lengths = {40: "sha1", 64: "sha256", 128: "sha512"}
107-
if sha_len not in sha_lengths:
84+
if sha_len not in sha_lengths: # pragma: no cover
10885
raise ValueError(
10986
"MAVEN_SHA be a valid sha1, sha256, or sha512 hash."
11087
f"Got invalid SHA length: {sha_len}. "
@@ -114,8 +91,11 @@ def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True)
11491
maven_dir = cjdk.cache_package("Maven", url, **kwargs)
11592
if maven_bin := next(maven_dir.rglob("apache-maven-*/**/mvn"), None):
11693
_add_to_path(maven_bin.parent, front=True)
117-
else:
118-
raise RuntimeError("Failed to find Maven executable in the downloaded package.")
94+
else: # pragma: no cover
95+
raise RuntimeError(
96+
"Failed to find Maven executable on system "
97+
"PATH, and download via cjdk failed."
98+
)
11999

120100

121101
def _add_to_path(path: Union[Path, str], front: bool = False) -> None:

src/scyjava/_jvm.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def jvm_version() -> str:
107107
return tuple(map(int, m.group(1).split(".")))
108108

109109

110-
def start_jvm(options=None, *, fetch_java: Optional[bool] = None) -> None:
110+
def start_jvm(options=None, *, fetch_java: bool = True) -> None:
111111
"""
112112
Explicitly connect to the Java virtual machine (JVM). Only one JVM can
113113
be active; does nothing if the JVM has already been started. Calling
@@ -119,14 +119,18 @@ def start_jvm(options=None, *, fetch_java: Optional[bool] = None) -> None:
119119
List of options to pass to the JVM.
120120
For example: ['-Dfoo=bar', '-XX:+UnlockExperimentalVMOptions']
121121
:param fetch_java:
122-
Whether to automatically fetch a JRE (and maven) using
123-
[`cjdk`](https://github.com/cachedjdk/cjdk) if java and maven executables are
124-
not found. Requires `cjdk` to be installed, either manually, or via the
125-
`scyjava[cjdk]` extra.
126-
- If `None` (default), then fetching will only occur if `cjdk` is available.
127-
(A log info will be issued if `cjdk` is not available.)
128-
- If `True`, an exception will be raised if `cjdk` is not available.
129-
- If `False`, no attempt to import `cjdk` is be made.
122+
If True (default), when a JVM/or maven cannot be located on the system,
123+
[`cjdk`](https://github.com/cachedjdk/cjdk) will be used to download
124+
a JRE distribution and set up the JVM. The following environment variables
125+
may be used to configure the JRE and Maven distributions to download:
126+
* `JAVA_VENDOR`: The vendor of the JRE distribution to download.
127+
Defaults to "zulu-jre".
128+
* `JAVA_VERSION`: The version of the JRE distribution to download.
129+
Defaults to "11".
130+
* `MAVEN_URL`: The URL of the Maven distribution to download.
131+
Defaults to https://dlcdn.apache.org/maven/maven-3/3.9.9/
132+
* `MAVEN_SHA`: The SHA512 hash of the Maven distribution to download, if
133+
providing a custom MAVEN_URL.
130134
"""
131135
# if JVM is already running -- break
132136
if jvm_started():
@@ -142,10 +146,10 @@ def start_jvm(options=None, *, fetch_java: Optional[bool] = None) -> None:
142146
# use the logger to notify user that endpoints are being added
143147
_logger.debug("Adding jars from endpoints {0}".format(endpoints))
144148

145-
if fetch_java is not False:
149+
if fetch_java:
146150
from scyjava._cjdk_fetch import ensure_jvm_available
147151

148-
ensure_jvm_available(raise_on_error=fetch_java is True)
152+
ensure_jvm_available()
149153

150154
# get endpoints and add to JPype class path
151155
if len(endpoints) > 0:

0 commit comments

Comments
 (0)