Repository navigation
Expand file tree
/
Copy pathinstaller.py
More file actions
100 lines (84 loc) · 2.85 KB
/
Copy pathinstaller.py
File metadata and controls
100 lines (84 loc) · 2.85 KB
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
import json
import os
import platform
import subprocess
import urllib.request
GITHUB_REPO = "bitswired/blitzkey"
def download_file(url, destination):
with urllib.request.urlopen(url) as response, open(destination, "wb") as out_file:
out_file.write(response.read())
def get_latest_release(repo):
url = f"https://api.github.com/repos/{repo}/releases/latest"
with urllib.request.urlopen(url) as response:
data = json.load(response)
return data
def is_blitzkey_installed():
binary_path = os.path.expanduser("~/blitzkey")
try:
output = subprocess.check_output(
[binary_path, "-V"], stderr=subprocess.STDOUT
).decode()
installed_version = output.split()[-1]
return installed_version
except subprocess.CalledProcessError:
return None
except FileNotFoundError:
return None
def main():
installed_version = is_blitzkey_installed()
release_data = get_latest_release(GITHUB_REPO)
latest_version = release_data["tag_name"].lstrip("v")
name_to_url = {
asset["name"]: asset["browser_download_url"] for asset in release_data["assets"]
}
system = platform.system()
architecture = platform.machine()
if system == "Windows":
file_name = "blitzkey-amd64.exe"
elif system == "Darwin":
if architecture == "x86_64":
file_name = "blitzkey-darwin-amd64"
else:
file_name = "blitzkey-darwin-arm64"
elif system == "Linux":
if architecture == "x86_64":
file_name = "blitzkey-linux-amd64"
else:
file_name = "blitzkey-linux-arm64"
else:
print("Unsupported platform")
return
url = name_to_url.get(file_name)
if not url:
print(f"Could not find a release for {system} {architecture}")
return
destination = os.path.expanduser("~/blitzkey")
if not installed_version:
should_install = (
input("Blitzkey is not installed. Would you like to install it? [Y/n] ")
.strip()
.lower()
or "y"
)
if should_install == "y":
download_file(url, destination)
print(f"Downloaded {file_name} to {destination}")
elif installed_version < latest_version:
should_update = (
input(
f"A newer version of Blitzkey (v{latest_version}) is available. Would you like to update? [Y/n] "
)
.strip()
.lower()
or "y"
)
if should_update == "y":
download_file(url, destination)
print(f"Updated Blitzkey to v{latest_version} at {destination}")
else:
print("Blitzkey is up-to-date.")
# Make the file executable (for macOS and Linux)
if system != "Windows":
os.chmod(destination, 0o755)
if __name__ == "__main__":
main()