Repository navigation
Expand file tree
/
Copy pathUpdate.py
More file actions
219 lines (171 loc) · 8.1 KB
/
Copy pathUpdate.py
File metadata and controls
219 lines (171 loc) · 8.1 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import os
import requests
import subprocess
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import threading
import io
import hashlib
from backend.paths import APP_DATA_DIR, IS_LINUX, exe_name, release_asset_name
APP_NAME = "Text2Pen"
# Installation paths (local filenames — no CPU-architecture tag; that only
# matters for picking the right download, below)
INSTALL_DIR = APP_DATA_DIR
TEXT2PEN_NAME = exe_name("Text2Pen")
UPDATE_NAME = exe_name("Update")
TEXT2PEN_PATH = os.path.join(INSTALL_DIR, TEXT2PEN_NAME)
UPDATE_PATH = os.path.join(INSTALL_DIR, UPDATE_NAME)
#Temp paths
UPDATE_TEMP = UPDATE_PATH + "-newest"
# Release asset names (architecture-specific on Linux — a release holds an
# x86_64 and an arm64 binary side by side) and the URLs/hash keys built from them
TEXT2PEN_ASSET = release_asset_name("Text2Pen")
UPDATE_ASSET = release_asset_name("Update")
TEXT2PEN_URL = f"https://github.com/pythonIsFast/Text2Pen/releases/latest/download/{TEXT2PEN_ASSET}"
UPDATE_URL = f"https://github.com/pythonIsFast/Text2Pen/releases/latest/download/{UPDATE_ASSET}"
class UpdateApp():
def __init__(self, root):
self.root = root
self.root.title("Text2Pen Updater")
self.root.geometry("400x150")
self.root.resizable(False, False)
tk.Label(root, text="Updating Text2Pen...", font=("Arial", 14)).pack(pady=10)
self.progress_var = tk.IntVar()
self.progress_bar = ttk.Progressbar(root, orient="horizontal", length=350,
mode="determinate", variable=self.progress_var)
self.progress_bar.pack(pady=20)
self.status_label = tk.Label(root, text="Starting...", font=("Arial", 10))
self.status_label.pack(pady=5)
threading.Thread(target=self.main, daemon=True).start()
def main(self):
try:
print("Checking GitHub release...")
hashes = self.get_release_hashes()
github_hash = hashes[TEXT2PEN_ASSET]
if os.path.exists(TEXT2PEN_PATH):
local_hash = self.sha256_file(TEXT2PEN_PATH)
if local_hash == github_hash:
print("Text2Pen already up to date")
new_hash = local_hash
new_data = None
else:
self.status_label.config(text="Downloading Text2Pen...")
new_data = self.download_file_ram(TEXT2PEN_URL)
new_hash = self.sha256_bytes(new_data)
if new_hash != github_hash:
raise ValueError(f"Downloaded {TEXT2PEN_ASSET} failed SHA256 verification")
else:
self.status_label.config(text="Downloading Text2Pen...")
new_data = self.download_file_ram(TEXT2PEN_URL)
new_hash = self.sha256_bytes(new_data)
if new_hash != github_hash:
raise ValueError(f"Downloaded {TEXT2PEN_ASSET} failed SHA256 verification")
if os.path.exists(TEXT2PEN_PATH):
if new_data is not None:
temp_path = TEXT2PEN_PATH + ".new"
self.write_file_once(temp_path, new_data)
self.replace_file(temp_path, TEXT2PEN_PATH)
else:
print("Adding file")
temp_path = TEXT2PEN_PATH + ".new"
self.write_file_once(temp_path, new_data)
self.replace_file(temp_path, TEXT2PEN_PATH)
github_updater_hash = hashes[UPDATE_ASSET]
if os.path.exists(UPDATE_PATH):
local_updater_hash = self.sha256_file(UPDATE_PATH)
if local_updater_hash == github_updater_hash:
updater_data = None
else:
self.status_label.config(text="Downloading Updater...")
updater_data = self.download_file_ram(UPDATE_URL)
new_updater_hash = self.sha256_bytes(updater_data)
if new_updater_hash != github_updater_hash:
raise ValueError(f"Downloaded {UPDATE_ASSET} failed SHA256 verification")
else:
self.status_label.config(text="Downloading Updater...")
updater_data = self.download_file_ram(UPDATE_URL)
new_updater_hash = self.sha256_bytes(updater_data)
if new_updater_hash != github_updater_hash:
raise ValueError(f"Downloaded {UPDATE_ASSET} failed SHA256 verification")
if os.path.exists(UPDATE_PATH):
if updater_data is not None:
self.write_file_once(UPDATE_TEMP, updater_data)
# Windows locks a running .exe, so the swap has to wait
# for Text2Pen.py's handoff logic on next launch. Linux
# has no such lock — a process can replace its own
# file while running — so finish the swap here directly;
# otherwise Update would re-download "the newest" every
# run forever without ever actually becoming current.
if IS_LINUX:
self.replace_file(UPDATE_TEMP, UPDATE_PATH)
else:
self.write_file_once(UPDATE_TEMP, updater_data)
if IS_LINUX:
self.replace_file(UPDATE_TEMP, UPDATE_PATH)
self.status_label.config(text="Update finished!")
self.progress_var.set(100)
self.root.update_idletasks()
print("Starting Text2Pen...")
subprocess.Popen([TEXT2PEN_PATH, "updaterStart"], close_fds=True)
self.root.destroy()
except Exception as e:
messagebox.showerror("Error", f"Update failed:\n{e}")
self.root.destroy()
def download_file_ram(self, url):
r = requests.get(url, stream=True)
r.raise_for_status()
total = int(r.headers.get("content-length", 0))
downloaded = 0
buffer = io.BytesIO() # RAM
for chunk in r.iter_content(8192):
if chunk:
buffer.write(chunk)
downloaded += len(chunk)
if total:
percent = int(downloaded / total * 100)
self.progress_var.set(percent)
self.status_label.config(
text=f"Downloading {os.path.basename(url)}... {percent}%"
)
self.root.update_idletasks()
buffer.seek(0)
return buffer.getvalue()
def sha256_bytes(self, data: bytes):
return hashlib.sha256(data).hexdigest()
def sha256_file(self, path):
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
h.update(chunk)
return h.hexdigest()
def write_file_once(self, path, data: bytes):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "wb") as f:
f.write(data)
if IS_LINUX:
os.chmod(path, 0o755)
def replace_file(self, src, dst):
try:
os.replace(src, dst)
except Exception as e:
print(f"Error while replacing file! {dst}: {e}")
def get_release_hashes(self):
api = "https://api.github.com/repos/pythonIsFast/Text2Pen/releases/latest"
r = requests.get(api, headers={"User-Agent": "Text2Pen-Updater"}, timeout=30)
r.raise_for_status()
release = r.json()
hashes = {}
for asset in release["assets"]:
hashes[asset["name"]] = asset["digest"].replace("sha256:", "")
return hashes
def on_close():
messagebox.showinfo(
"Update running",
"Please wait for Text2Pen to finish the update."
)
if __name__ == "__main__":
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", on_close)
app = UpdateApp(root)
root.mainloop()