diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b207946d..52a095e4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -53,7 +53,7 @@ jobs: # The exact Temurin build to bundle, in OpenJDK notation, such as 21.0.8+9 or 21.0.13.1+2. # Its first 3 numbers are the ones of the version in pyproject.toml, which `test_java_version` checks. # setup-java installs this build or fails: it never falls back to another build with the same numbers, such as one preinstalled on the runner. - TEMURIN_VERSION: 21.0.8+9 + TEMURIN_VERSION: 21.0.13.1+2 steps: - uses: actions/checkout@v7.0.1 with: diff --git a/README.md b/README.md index ce07d64e..233880ee 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,15 @@ pip install jdk4py ## Usage ```python ->>> from jdk4py import JAVA, JAVA_HOME, JAVA_VERSION +>>> from jdk4py import JAVA, JAVA_FULL_VERSION, JAVA_HOME, JAVA_VERSION >>> JAVA_HOME PosixPath('/Users/johndoe/dev/jdk4py/jdk4py/java-runtime') >>> JAVA PosixPath('/Users/johndoe/dev/jdk4py/jdk4py/java-runtime/bin/java') >>> JAVA_VERSION -(21, 0, 8) +(21, 0, 13) +>>> JAVA_FULL_VERSION +'21.0.13.1+2' >>> from subprocess import run >>> some_java_options = ["-Xmx16G", "-Xms2G"] >>> run( @@ -35,5 +37,15 @@ PosixPath('/Users/johndoe/dev/jdk4py/jdk4py/java-runtime/bin/java') `jdk4py`'s version contains 4 numbers: -- The first 3 numbers correspond to the JDK version. -- The fourth number is the library API version. +- The first 3 numbers are the version line of the bundled JDK, such as 21.0.8. `JAVA_VERSION` holds them. +- The fourth number counts the releases of jdk4py in that line. A new release can bundle another build of the JDK, such as an OpenJDK patch release, or change the library. + +The fourth number is not part of the JDK version, even when they look alike. +`JAVA_FULL_VERSION` holds the exact version of the bundled JDK with its build number, such as 21.0.13.1+2: the version can have more than 3 numbers. + +| jdk4py | JDK | +| --------- | ----------- | +| 21.0.13.1 | 21.0.13.1+2 | +| 21.0.8.2 | 21.0.8+9 | +| 21.0.8.1 | 21.0.8+9 | +| 21.0.8.0 | 21.0.8+9 | diff --git a/build_java_runtime.py b/build_java_runtime.py index 106bc204..571ed30d 100644 --- a/build_java_runtime.py +++ b/build_java_runtime.py @@ -1,22 +1,35 @@ -from shutil import rmtree +from pathlib import Path +from shutil import rmtree, which from subprocess import check_call -from jdk4py import JAVA_HOME +from jdk4py import JAVA_HOME, _get_release_property from jdk4py._added_modules import ADDED_MODULES from jdk4py._included_locales import INCLUDED_LOCALES def build_java_runtime() -> None: + jlink = which("jlink") + if jlink is None: + message = "jlink not found." + raise RuntimeError(message) + # The `release` file of the JDK that jlink comes from has the version with its + # build number, such as 21.0.13.1+2-LTS. jlink only keeps the version without it, + # so it is added back for `JAVA_FULL_VERSION`. + source_java_home = Path(jlink).resolve().parent.parent + runtime_version = _get_release_property(source_java_home, "JAVA_RUNTIME_VERSION") + rmtree(JAVA_HOME, ignore_errors=True) check_call( # noqa: S603 - [ # noqa: S607 - "jlink", + [ + jlink, "--no-man-pages", "--strip-debug", "--add-modules", ",".join(ADDED_MODULES), f"--include-locales={','.join(INCLUDED_LOCALES)}", + "--release-info", + f'add:JAVA_RUNTIME_VERSION="{runtime_version}"', "--output", JAVA_HOME, ], diff --git a/pyproject.toml b/pyproject.toml index 5f6b04f7..79d7cd5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ keywords = ["jdk", "java", "jvm", "jre"] name = "jdk4py" readme = "README.md" requires-python = ">=3.10" -version = "21.0.8.2" +version = "21.0.13.1" [project.urls] Repository = "https://github.com/activeviam/jdk4py" diff --git a/src/jdk4py/__init__.py b/src/jdk4py/__init__.py index 2584d0e5..46a6076f 100644 --- a/src/jdk4py/__init__.py +++ b/src/jdk4py/__init__.py @@ -1,5 +1,7 @@ """JDK packaged for Python.""" +import re as _re +from functools import cache as _cache from importlib.metadata import version as _version from pathlib import Path as _Path @@ -13,3 +15,27 @@ int(number) for number in _VERSION.split(".") ] JAVA_VERSION = _JAVA_MAJOR, _JAVA_MINOR, _JAVA_PATCH + + +def _get_release_property(java_home: _Path, key: str, /) -> str: + release_file = java_home / "release" + for line in release_file.read_text(encoding="utf-8").splitlines(): + line_key, _, value = line.partition("=") + if line_key == key: + return value.strip('"') + raise RuntimeError(f"No {key} in {release_file}.") + + +@_cache +def _get_java_full_version() -> str: + runtime_version = _get_release_property(JAVA_HOME, "JAVA_RUNTIME_VERSION") + # Drop the optional suffix after the build number, such as `-LTS` in 21.0.8+9-LTS. + return _re.sub(r"(\+\d+)-.*$", r"\1", runtime_version) + + +def __getattr__(name: str) -> str: + # `JAVA_FULL_VERSION` is read from the Java runtime on first access: + # the runtime does not exist yet when `build_java_runtime.py` imports this module. + if name == "JAVA_FULL_VERSION": + return _get_java_full_version() + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/tests/test_jdk4py.py b/tests/test_jdk4py.py index a89f77bf..25642b6f 100644 --- a/tests/test_jdk4py.py +++ b/tests/test_jdk4py.py @@ -2,7 +2,7 @@ from pathlib import Path from subprocess import run -from jdk4py import JAVA, JAVA_VERSION +from jdk4py import JAVA, JAVA_FULL_VERSION, JAVA_VERSION from jdk4py._included_locales import INCLUDED_LOCALES _TEST_RESOURCES_DIRECTORY = Path(__file__).parent / "resources" @@ -19,7 +19,14 @@ def test_java_version() -> None: assert match, f"Unexpected output:\n{completed_process.stdout}" version = match.group("version") assert isinstance(version, str) - assert tuple([int(number) for number in version.split(".")][:3]) == JAVA_VERSION + assert tuple(int(number) for number in version.split(".")[:3]) == JAVA_VERSION + build_match = re.search(r"\(build (?P[^)]+)\)", completed_process.stderr) + assert build_match, f"Unexpected output:\n{completed_process.stderr}" + # The build can end with an optional suffix, such as `-LTS` in 21.0.13.1+2-LTS. + assert re.fullmatch( + rf"{re.escape(JAVA_FULL_VERSION)}(-.+)?", build_match.group("build") + ) + assert JAVA_FULL_VERSION.startswith(f"{version}+") def test_jar_execution() -> None: diff --git a/uv.lock b/uv.lock index 7021cafa..192ad9c2 100644 --- a/uv.lock +++ b/uv.lock @@ -34,7 +34,7 @@ wheels = [ [[package]] name = "jdk4py" -version = "21.0.8.2" +version = "21.0.13.1" source = { editable = "." } [package.dev-dependencies]