|
| 1 | +# |
| 2 | +# Sonar Scanner Python |
| 3 | +# Copyright (C) 2011-2024 SonarSource SA. |
| 4 | +# mailto:info AT sonarsource DOT com |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either |
| 9 | +# version 3 of the License, or (at your option) any later version. |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with this program; if not, write to the Free Software Foundation, |
| 18 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +# |
| 20 | +import pathlib |
| 21 | +import shutil |
| 22 | +import tarfile |
| 23 | +import zipfile |
| 24 | +from dataclasses import dataclass |
| 25 | +from typing import Optional |
| 26 | + |
| 27 | +from pysonar_scanner import utils |
| 28 | +from pysonar_scanner.api import JRE, SonarQubeApi |
| 29 | +from pysonar_scanner.cache import Cache |
| 30 | +from pysonar_scanner.configuration import Configuration |
| 31 | +from pysonar_scanner.exceptions import ( |
| 32 | + ChecksumException, |
| 33 | + NoJreAvailableException, |
| 34 | + UnsupportedArchiveFormat, |
| 35 | +) |
| 36 | +from pysonar_scanner.exceptions import JreProvisioningException |
| 37 | + |
| 38 | + |
| 39 | +@dataclass(frozen=True) |
| 40 | +class JREResolvedPath: |
| 41 | + path: pathlib.Path |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def from_string(path: str) -> "JREResolvedPath": |
| 45 | + if not path: |
| 46 | + raise ValueError("JRE path cannot be empty") |
| 47 | + return JREResolvedPath(pathlib.Path(path)) |
| 48 | + |
| 49 | + |
| 50 | +class JREProvisioner: |
| 51 | + def __init__(self, api: SonarQubeApi, cache: Cache): |
| 52 | + self.api = api |
| 53 | + self.cache = cache |
| 54 | + |
| 55 | + def provision(self) -> JREResolvedPath: |
| 56 | + jre, resolved_path = self.__attempt_provisioning_jre_with_retry() |
| 57 | + return self.__unpack_jre(jre, resolved_path) |
| 58 | + |
| 59 | + def __attempt_provisioning_jre_with_retry(self) -> tuple[JRE, pathlib.Path]: |
| 60 | + jre_and_resolved_path = self.__attempt_provisioning_jre() |
| 61 | + if jre_and_resolved_path is None: |
| 62 | + jre_and_resolved_path = self.__attempt_provisioning_jre() |
| 63 | + if jre_and_resolved_path is None: |
| 64 | + raise ChecksumException( |
| 65 | + f"Failed to download and verify JRE for {utils.get_os().value} and {utils.get_arch().value}" |
| 66 | + ) |
| 67 | + |
| 68 | + return jre_and_resolved_path |
| 69 | + |
| 70 | + def __attempt_provisioning_jre(self) -> Optional[tuple[JRE, pathlib.Path]]: |
| 71 | + jre = self.__get_available_jre() |
| 72 | + |
| 73 | + jre_path = self.__get_jre_from_cache(jre) |
| 74 | + if jre_path is not None: |
| 75 | + return (jre, jre_path) |
| 76 | + |
| 77 | + jre_path = self.__download_jre(jre) |
| 78 | + return (jre, jre_path) if jre_path is not None else None |
| 79 | + |
| 80 | + def __get_available_jre(self) -> JRE: |
| 81 | + jres = self.api.get_analysis_jres(os=utils.get_os(), arch=utils.get_arch()) |
| 82 | + if len(jres) == 0: |
| 83 | + raise NoJreAvailableException( |
| 84 | + f"No JREs are available for {utils.get_os().value} and {utils.get_arch().value}" |
| 85 | + ) |
| 86 | + return jres[0] |
| 87 | + |
| 88 | + def __get_jre_from_cache(self, jre: JRE) -> Optional[pathlib.Path]: |
| 89 | + cache_file = self.cache.get_file(jre.filename, jre.sha256) |
| 90 | + return cache_file.filepath if cache_file.is_valid() else None |
| 91 | + |
| 92 | + def __download_jre(self, jre: JRE) -> Optional[pathlib.Path]: |
| 93 | + cache_file = self.cache.get_file(jre.filename, jre.sha256) |
| 94 | + cache_file.filepath.unlink(missing_ok=True) |
| 95 | + |
| 96 | + with cache_file.open(mode="wb") as f: |
| 97 | + self.api.download_analysis_jre(jre.id, f) |
| 98 | + |
| 99 | + return cache_file.filepath if cache_file.is_valid() else None |
| 100 | + |
| 101 | + def __unpack_jre(self, jre: JRE, file_path: pathlib.Path) -> JREResolvedPath: |
| 102 | + unzip_dir = self.__prepare_unzip_dir(file_path) |
| 103 | + self.__extract_jre(file_path, unzip_dir) |
| 104 | + return JREResolvedPath(unzip_dir / jre.java_path) |
| 105 | + |
| 106 | + def __prepare_unzip_dir(self, file_path: pathlib.Path) -> pathlib.Path: |
| 107 | + unzip_dir = self.cache.get_file_path(f"{file_path}_unzip") |
| 108 | + try: |
| 109 | + if unzip_dir.exists(): |
| 110 | + shutil.rmtree(unzip_dir) |
| 111 | + unzip_dir.mkdir(parents=True) |
| 112 | + return unzip_dir |
| 113 | + except OSError as e: |
| 114 | + raise JreProvisioningException(f"Failed to prepare unzip directory: {unzip_dir}") from e |
| 115 | + |
| 116 | + def __extract_jre(self, file_path: pathlib.Path, unzip_dir: pathlib.Path): |
| 117 | + if file_path.suffix == ".zip": |
| 118 | + with zipfile.ZipFile(file_path, "r") as zip_ref: |
| 119 | + zip_ref.extractall(unzip_dir) |
| 120 | + elif file_path.suffix in [".gz", ".tgz"]: |
| 121 | + with tarfile.open(file_path, "r:gz") as tar_ref: |
| 122 | + tar_ref.extractall(unzip_dir, filter="data") |
| 123 | + else: |
| 124 | + raise UnsupportedArchiveFormat(f"Unsupported archive format: {file_path.suffix}") |
| 125 | + |
| 126 | + |
| 127 | +class JREResolver: |
| 128 | + def __init__(self, configuration: Configuration, jre_provisioner: JREProvisioner): |
| 129 | + self.configuration = configuration |
| 130 | + self.jre_provisioner = jre_provisioner |
| 131 | + |
| 132 | + def resolve_jre(self) -> JREResolvedPath: |
| 133 | + exe_suffix = ".exe" if self.configuration.sonar.scanner.os == "windows" else "" |
| 134 | + if self.configuration.sonar.scanner.java_exe_path: |
| 135 | + return JREResolvedPath(pathlib.Path(self.configuration.sonar.scanner.java_exe_path)) |
| 136 | + if not self.configuration.sonar.scanner.skip_jre_provisioning: |
| 137 | + return self.__provision_jre() |
| 138 | + java_path = pathlib.Path(f"java{exe_suffix}") |
| 139 | + return JREResolvedPath(java_path) |
| 140 | + |
| 141 | + def __provision_jre(self) -> JREResolvedPath: |
| 142 | + return self.jre_provisioner.provision() |
0 commit comments