-
Notifications
You must be signed in to change notification settings - Fork 0
/
repackage.py
68 lines (56 loc) · 2.59 KB
/
repackage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import re
import requests
from urllib.parse import urljoin
import urllib.request
import tarfile
import os
import tempfile
from shutil import move, copyfile
base_url = 'https://download.eclipse.org/jdtls/milestones/'
def get_latest_version():
milestones_res = requests.get(base_url, allow_redirects=True)
if(milestones_res.status_code != 200):
raise Exception('Failed to get all versions from milestones page')
version_match_regex = r"<a href='(.*?)'> (\d*.\d*.\d*.)</a>"
matches = re.findall(version_match_regex, milestones_res.text)
versions = [match[1] for match in matches]
sorted_versions = sorted(versions, key=lambda x: list(map(int, x.split('.'))))
return sorted_versions[-1]
def get_jdtls_download_url(version):
url = urljoin(base_url, f'{version}/latest.txt')
pkg_name_res = requests.get(url)
file_name = pkg_name_res.text.strip()
return urljoin(base_url, f'{version}/{file_name}')
def get_equinox_launcher_name(version):
plugins_res = requests.get(f"https://download.eclipse.org/jdtls/milestones/{version}/repository/plugins/")
pattern = r"<a .*?>(org.eclipse.equinox.launcher_.*?.jar)</a>"
matches = re.findall(pattern, plugins_res.text)
if len(matches) < 1:
raise Exception('Could not find the equinox launcher plugin')
if len(matches) > 1:
raise Exception('Found more than one equinox launcher plugin')
return matches[0]
def download_file(url):
with tempfile.TemporaryDirectory() as tmp:
res = requests.get(url)
file_path = urljoin(tmp, 'jdtls.tar.gz')
with open(file_path, 'wb') as file:
file.write(res.content)
return file_path
def extract_tar_gz(tar_gz_path, equinox_plugin_path):
with tarfile.open(tar_gz_path, "r:gz") as tar:
with tempfile.TemporaryDirectory() as tmp:
ex_dir = f'{tmp}/jdtls'
tar.extractall(path=ex_dir)
move(f'{ex_dir}/plugins/{equinox_plugin_path}', f'{ex_dir}/plugins/org.eclipse.equinox.launcher.jar')
with tempfile.TemporaryDirectory() as tmpout:
out_path = f'{tmpout}/jdtls.tar.gz'
with tarfile.open(out_path, "w:gz") as tarout:
for file in os.listdir(ex_dir):
tarout.add(f'{ex_dir}/{file}',arcname=file, recursive=True)
copyfile(f'{tmpout}/jdtls.tar.gz', './jdtls.tar.gz')
version = get_latest_version()
download_url = get_jdtls_download_url(version)
equinox_plugin_path = get_equinox_launcher_name(version)
downloaded_path = download_file(download_url)
extract_tar_gz(downloaded_path, equinox_plugin_path)