65 lines
2.5 KiB
Python
65 lines
2.5 KiB
Python
|
import os, platform, zipfile, tarfile, urllib.request
|
||
|
|
||
|
JAVA_VER = '8'
|
||
|
RUNTIME_PATH = os.path.join(os.getcwd(), 'runtime')
|
||
|
|
||
|
|
||
|
def get_platform_ext() -> tuple:
|
||
|
if platform.system() == "Windows":
|
||
|
if platform.architecture()[0] == "64bit":
|
||
|
return "x64-windows", "zip"
|
||
|
else:
|
||
|
return "x86-windows", "zip"
|
||
|
elif platform.system() == "Linux" and platform.architecture()[0] == "64bit":
|
||
|
return "x64-linux", "tar.gz"
|
||
|
elif platform.system() == "Darwin" and platform.architecture()[0] == "64bit":
|
||
|
return "macos", "tar.gz"
|
||
|
else:
|
||
|
raise ValueError("Unsupported system. Currently only supports windows and 64-bit linux/MacOS systems!")
|
||
|
|
||
|
|
||
|
# def get_download_url(version: str, arch_os: str, file_ext: str, jv_type: str = "jdk") -> str:
|
||
|
# if not jv_type == "jdk" or not jv_type == "jre":
|
||
|
# raise ValueError(f'{jv_type} is not an acceptable type. Accepted types are either "jdk" or "jre"')
|
||
|
# if version == '1.8':
|
||
|
# version = '8'
|
||
|
# return f"https://corretto.aws/downloads/latest/amazon-corretto-{version}-{arch_os}-{jv_type}.{file_ext}"
|
||
|
|
||
|
|
||
|
def install_runtime(jv_ver: str = JAVA_VER) -> str:
|
||
|
"""
|
||
|
:acknowledge: https://stackoverflow.com/a/7244263
|
||
|
"""
|
||
|
os_arch, ext = get_platform_ext()
|
||
|
url = f"https://corretto.aws/downloads/latest/amazon-corretto-{jv_ver}-{os_arch}-jdk.{ext}"
|
||
|
print(url)
|
||
|
try:
|
||
|
urllib.request.urlretrieve(url, f'Runtime.cash')
|
||
|
if ext == "zip":
|
||
|
with zipfile.ZipFile(f'Runtime.cash') as z:
|
||
|
jv_name = z.namelist()[0].replace('/', '')
|
||
|
z.extractall(path=RUNTIME_PATH)
|
||
|
else: # non-windows, which assume is the tar.gz file
|
||
|
with tarfile.open(f'Runtime.cash', 'r:gz') as t:
|
||
|
jv_name = t.getnames()[0].replace('/', '')
|
||
|
t.extractall(path=RUNTIME_PATH)
|
||
|
return jv_name
|
||
|
finally:
|
||
|
os.unlink(f'Runtime.cash')
|
||
|
|
||
|
|
||
|
def get_java_path(jv_ver: str = JAVA_VER, directory: str = RUNTIME_PATH) -> str:
|
||
|
if not os.path.exists(directory):
|
||
|
return os.path.join(directory, install_runtime(jv_ver), "bin", "java.exe")
|
||
|
if jv_ver == '8':
|
||
|
jv_ver = '1.8'
|
||
|
jv_path = [root for root, d, f in os.walk(RUNTIME_PATH, topdown=False) if jv_ver in root and '\\bin' in root]
|
||
|
if jv_path:
|
||
|
return os.path.join(jv_path[0], "java.exe")
|
||
|
else:
|
||
|
return os.path.join(directory, install_runtime(jv_ver), "bin", "java.exe")
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
print(get_java_path())
|