-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate.py
executable file
·108 lines (79 loc) · 2.85 KB
/
update.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
Upgrade plugin.py server info to the latest release of elixir-ls as seen here:
https://github.com/elixir-lsp/elixir-ls/releases
"""
import hashlib
import json
import os
import re
import urllib.request
def main():
current_version = get_current_release_version()
release = fetch_latest_release()
latest_version = release["tag_name"].lstrip("v")
print(f"Current version: {current_version}")
print(f"Latest version: {latest_version}")
if current_version == latest_version:
print("✓ Using the latest version")
return
print("Update? [Y/n]")
if input().strip().lower() not in ["", "y"]:
print("Aborted.")
return
update_version(latest_version, release)
if "GITHUB_OUTPUT" in os.environ:
with open(os.environ["GITHUB_OUTPUT"], "a") as gh_output:
print(f"version={latest_version}", file=gh_output)
print(f"\n✓ Updated to {latest_version}")
def update_version(version, release):
url = get_download_url(release)
hash = calculate_hash(url)
print(f"SERVER_VERSION = {version}")
print(f"SERVER_URL = {url}")
print(f"SERVER_SHA256 = {hash}")
with open("plugin.py") as f:
content = f.read()
content = re.sub(
r"^SERVER_VERSION.+", f'SERVER_VERSION = "{version}"', content, 0, re.MULTILINE
)
content = re.sub(
r"^SERVER_URL.+", f'SERVER_URL = "{url}"', content, 0, re.MULTILINE
)
content = re.sub(
r"^SERVER_SHA256.+", f'SERVER_SHA256 = "{hash}"', content, 0, re.MULTILINE
)
with open("plugin.py", "w") as f:
f.write(content)
def get_download_url(release):
for asset in release["assets"]:
filename = asset["name"]
if filename.startswith("elixir-ls-v") and filename.endswith(".zip"):
return asset["browser_download_url"]
raise Exception("Asset url not found")
def get_current_release_version():
with open("plugin.py") as f:
content = f.read()
m = re.search(r'SERVER_VERSION = "([0-9.]+)"', content)
if not m:
raise Exception("Cannot find current version in plugin.py")
return m.group(1)
def calculate_hash(url):
response = http_get(url)
hash_object = hashlib.sha256(response)
return hash_object.hexdigest()
def fetch_latest_release():
response = http_get("https://api.github.com/repos/elixir-lsp/elixir-ls/releases")
releases = json.loads(response)
for release in releases:
if not release["draft"] and not release["prerelease"]:
return release
raise Exception("No release found")
def http_get(url):
print(f"<-- GET {url}")
with urllib.request.urlopen(url) as response:
if response.status != 200:
raise Exception(f"Failed loading data, got HTTP {response.status}")
return response.read()
if __name__ == "__main__":
main()