|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import shutil |
| 4 | +import tarfile |
| 5 | +import zipfile |
| 6 | +from pathlib import Path |
| 7 | +from tempfile import TemporaryDirectory |
| 8 | +from typing import Literal |
| 9 | + |
| 10 | +import requests |
| 11 | +import tqdm |
| 12 | +from cpuinfo import get_cpu_info |
| 13 | +from hypy_utils import ensure_dir, printc |
| 14 | +from hypy_utils.downloader import download_file |
| 15 | + |
| 16 | +# Windows / Linux / Darwin |
| 17 | +sys = platform.system() |
| 18 | + |
| 19 | +# "X86_32", "X86_64", "ARM_8", "ARM_7", "PPC_32", "PPC_64", "SPARC_32", |
| 20 | +# "SPARC_64", "S390X", "MIPS_32", "MIPS_64", "RISCV_32", "RISCV_64" |
| 21 | +arch = get_cpu_info()['arch'] |
| 22 | + |
| 23 | +print('Detected system:', sys, arch) |
| 24 | + |
| 25 | +JAVA = Literal['19', '18', '17', '16', '15', '14', '13', '12', '11', '10', '9', '1.8', '1.7'] |
| 26 | + |
| 27 | + |
| 28 | +def download_oracle(java_ver: JAVA, path: str | Path): |
| 29 | + path = Path(path) |
| 30 | + |
| 31 | + # Normalize OS and architecture |
| 32 | + s = sys.lower() |
| 33 | + if s == 'darwin': |
| 34 | + s = 'macos' |
| 35 | + assert s in ['linux', 'macos', 'windows'], f'Unsupported OS for Oracle JDK: {s}' |
| 36 | + |
| 37 | + a = arch.lower() |
| 38 | + if a == 'x86_64' or a == 'x86_32': |
| 39 | + a = 'x64' |
| 40 | + if a == 'ARM_8' or a == 'ARM_7': |
| 41 | + a = 'aarch64' |
| 42 | + assert a in ['x64', 'aarch64'], f'Unsupported CPU architecture for Oracle JDK: {a}' |
| 43 | + |
| 44 | + # Create URL |
| 45 | + ext = 'tar.gz' if s in ['linux', 'macos'] else 'zip' |
| 46 | + fname = f'jdk-{java_ver}_{s}-{a}_bin.{ext}' |
| 47 | + url = f'https://download.oracle.com/java/{java_ver}/latest/{fname}' |
| 48 | + |
| 49 | + # Download |
| 50 | + with TemporaryDirectory() as tmp: |
| 51 | + tmp = Path(tmp) |
| 52 | + file = tmp / fname |
| 53 | + extract = ensure_dir(tmp / 'extract') |
| 54 | + |
| 55 | + print('Downloading JDK...') |
| 56 | + download_file(url, file) |
| 57 | + |
| 58 | + print('Extracting JDK...') |
| 59 | + if fname.endswith('tar.gz'): |
| 60 | + with tarfile.open(file) as f: |
| 61 | + f.extractall(extract) |
| 62 | + elif fname.endswith('zip'): |
| 63 | + with zipfile.ZipFile(file, 'r') as f: |
| 64 | + f.extractall(extract) |
| 65 | + |
| 66 | + print('Checking bin') |
| 67 | + if not (extract / 'bin').is_dir(): |
| 68 | + dirs = [extract / f for f in os.listdir(extract) if (extract / f).is_dir()] |
| 69 | + assert len(dirs) == 1, 'Error: Unknown file structure.' |
| 70 | + extract = dirs[0] |
| 71 | + |
| 72 | + print('Installing JDK...') |
| 73 | + ensure_dir(path.parent) |
| 74 | + shutil.move(extract, path) |
| 75 | + |
| 76 | + print(f'Done! Oracle JDK {java_ver} downloaded to {path}') |
| 77 | + |
0 commit comments