|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from pathlib import Path |
| 9 | + |
| 10 | +_logger = logging.getLogger(__name__) |
| 11 | +_DEFAULT_MAVEN_URL = "tgz+https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz" # noqa: E501 |
| 12 | +_DEFAULT_MAVEN_SHA = "a555254d6b53d267965a3404ecb14e53c3827c09c3b94b5678835887ab404556bfaf78dcfe03ba76fa2508649dca8531c74bca4d5846513522404d48e8c4ac8b" # noqa: E501 |
| 13 | +_DEFAULT_JAVA_VENDOR = "zulu-jre" |
| 14 | +_DEFAULT_JAVA_VERSION = "11" |
| 15 | + |
| 16 | + |
| 17 | +def cjdk_fetch_java( |
| 18 | + vendor: str = "", version: str = "", raise_on_error: bool = True |
| 19 | +) -> None: |
| 20 | + """Fetch java using cjdk and add it to the PATH.""" |
| 21 | + try: |
| 22 | + import cjdk |
| 23 | + except ImportError as e: |
| 24 | + if raise_on_error is True: |
| 25 | + raise ImportError( |
| 26 | + "No JVM found. Please install `cjdk` to use the fetch_java feature." |
| 27 | + ) from e |
| 28 | + _logger.info("cjdk is not installed. Skipping automatic fetching of java.") |
| 29 | + return |
| 30 | + |
| 31 | + if not vendor: |
| 32 | + vendor = os.getenv("JAVA_VENDOR", _DEFAULT_JAVA_VENDOR) |
| 33 | + version = os.getenv("JAVA_VERSION", _DEFAULT_JAVA_VERSION) |
| 34 | + |
| 35 | + _logger.info(f"No JVM found, fetching {vendor}:{version} using cjdk...") |
| 36 | + home = cjdk.java_home(vendor=vendor, version=version) |
| 37 | + _add_to_path(str(home / "bin")) |
| 38 | + os.environ["JAVA_HOME"] = str(home) |
| 39 | + |
| 40 | + |
| 41 | +def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True) -> None: |
| 42 | + """Fetch Maven using cjdk and add it to the PATH.""" |
| 43 | + try: |
| 44 | + import cjdk |
| 45 | + except ImportError as e: |
| 46 | + if raise_on_error is True: |
| 47 | + raise ImportError( |
| 48 | + "Please install `cjdk` to use the fetch_java feature." |
| 49 | + ) from e |
| 50 | + _logger.info("cjdk is not installed. Skipping automatic fetching of Maven.") |
| 51 | + return |
| 52 | + |
| 53 | + # if url was passed as an argument, or env_var, use it with provided sha |
| 54 | + # otherwise, use default values for both |
| 55 | + if url := url or os.getenv("MAVEN_URL", ""): |
| 56 | + sha = sha or os.getenv("MAVEN_SHA", "") |
| 57 | + else: |
| 58 | + url = _DEFAULT_MAVEN_URL |
| 59 | + sha = _DEFAULT_MAVEN_SHA |
| 60 | + |
| 61 | + # fix urls to have proper prefix for cjdk |
| 62 | + if url.startswith("http"): |
| 63 | + if url.endswith(".tar.gz"): |
| 64 | + url = url.replace("http", "tgz+http") |
| 65 | + elif url.endswith(".zip"): |
| 66 | + url = url.replace("http", "zip+http") |
| 67 | + |
| 68 | + # determine sha type based on length (cjdk requires specifying sha type) |
| 69 | + # assuming hex-encoded SHA, length should be 40, 64, or 128 |
| 70 | + kwargs = {} |
| 71 | + if sha_len := len(sha): # empty sha is fine... we just don't pass it |
| 72 | + sha_lengths = {40: "sha1", 64: "sha256", 128: "sha512"} |
| 73 | + if sha_len not in sha_lengths: |
| 74 | + raise ValueError( |
| 75 | + "MAVEN_SHA be a valid sha1, sha256, or sha512 hash." |
| 76 | + f"Got invalid SHA length: {sha_len}. " |
| 77 | + ) |
| 78 | + kwargs = {sha_lengths[sha_len]: sha} |
| 79 | + |
| 80 | + maven_dir = cjdk.cache_package("Maven", url, **kwargs) |
| 81 | + if maven_bin := next(maven_dir.rglob("apache-maven-*/**/mvn"), None): |
| 82 | + _add_to_path(maven_bin.parent, front=True) |
| 83 | + else: |
| 84 | + raise RuntimeError("Failed to find Maven executable in the downloaded package.") |
| 85 | + |
| 86 | + |
| 87 | +def _add_to_path(path: Path | str, front: bool = False) -> None: |
| 88 | + """Add a path to the PATH environment variable. |
| 89 | +
|
| 90 | + If front is True, the path is added to the front of the PATH. |
| 91 | + By default, the path is added to the end of the PATH. |
| 92 | + If the path is already in the PATH, it is not added again. |
| 93 | + """ |
| 94 | + |
| 95 | + current_path = os.environ.get("PATH", "") |
| 96 | + if (path := str(path)) in current_path: |
| 97 | + return |
| 98 | + new_path = [path, current_path] if front else [current_path, path] |
| 99 | + os.environ["PATH"] = os.pathsep.join(new_path) |
0 commit comments