Skip to content

Commit 639a4a8

Browse files
authored
Merge pull request #2 from FrenkyDema/install-update-file
Install update file
2 parents 43c3e70 + 0ae8d71 commit 639a4a8

3 files changed

Lines changed: 53 additions & 18 deletions

File tree

src/gui/pages/update_page.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import shutil
23
from tkinter import messagebox
34
import aiohttp
45
import webbrowser
@@ -10,19 +11,50 @@
1011
from ...libs.lib import CONFIG_FILE
1112

1213

14+
import logging
15+
16+
import logging
17+
1318
async def update_async(latest_release):
14-
print("update_async")
15-
# Scarica l'aggiornamento dal repository su GitHub
16-
async with aiohttp.ClientSession() as session:
17-
async with session.get(latest_release['zipball_url']) as response:
18-
with open('latest_release.zip', 'wb') as f:
19-
f.write(await response.read())
20-
21-
# Aggiorna il file di configurazione locale con la nuova versione
22-
lib.update_key_json(CONFIG_FILE, "version", latest_release['tag_name'])
23-
24-
# Avvia l'applicazione aggiornata
25-
os.startfile('latest_release.zip')
19+
logger = logging.getLogger(__name__)
20+
logger.info("Starting async update")
21+
22+
# Cerca il file exe tra gli asset della release
23+
exe_asset = None
24+
for asset in latest_release['assets']:
25+
if asset['name'].endswith('.exe'):
26+
exe_asset = asset
27+
break
28+
29+
# Controlla se è stato trovato un file exe tra gli asset
30+
if exe_asset is not None:
31+
# Scarica il file exe
32+
async with aiohttp.ClientSession() as session:
33+
async with session.get(exe_asset['browser_download_url']) as response:
34+
with open(exe_asset['name'], 'wb') as f:
35+
f.write(await response.read())
36+
37+
# Sostituisce il file exe dell'applicazione con il nuovo file
38+
app_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
39+
exe_path = os.path.join(app_path, lib.APP_NAME + '.exe')
40+
if os.path.exists(exe_path):
41+
os.remove(exe_path)
42+
shutil.move(exe_asset['name'], exe_path)
43+
44+
45+
# Rimuovere lavecchia cartella dell' applicazione
46+
shutil.rmtree(os.path.dirname(os.path.dirname(lib.resource_path(""))))
47+
48+
# Avvia l'applicazione aggiornata
49+
os.startfile(exe_path)
50+
51+
# Chiude la vecchia applicazione
52+
os._exit(0)
53+
else:
54+
error_message = "Nessun file exe trovato nella release più recente."
55+
logger.error(error_message)
56+
messagebox.showerror("Error", error_message)
57+
2658

2759

2860
class UpdatePage(CTkFrame):

src/libs/lib.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
language_path = 'src\\resources\\languages\\'
1919

2020
APP_NAME = "Test_Application_Update"
21-
21+
VERSION = "v0.0.0"
2222
CONFIG_FILE = "config.json"
2323

2424
# ================== Temp Files functions ==================
@@ -33,15 +33,17 @@ def resource_temp_path(relative_path: str) -> str:
3333

3434
# ================== File functions ==================
3535

36-
def resource_path(relative_path: str):
37-
base_path = user_data_dir(appname=APP_NAME, appauthor=False)
36+
def resource_path(relative_path: str,):
37+
base_path = user_data_dir(
38+
appname=APP_NAME, appauthor=False, version=VERSION)
3839
print("Local - ", os.path.join(base_path, relative_path))
3940
return os.path.join(base_path, relative_path)
4041

4142

4243
def create_app_files():
4344
print("create app files")
44-
copy_dir(resource_temp_path(file_path), resource_path(file_path))
45+
copy_dir(resource_temp_path(file_path), resource_path(
46+
file_path), resource_temp_path(image_path))
4547

4648

4749
def copy_dir(src: str, dst: str, ignore: str = ""):
@@ -110,9 +112,9 @@ def get_dix_json(file_name: str):
110112
def default_config_values():
111113
print("default config values")
112114
dix = {
113-
"version": "v0.0.0",
115+
"version": VERSION,
114116
"repo_owner": 'FrenkyDema',
115-
"repo_name": 'Test_Application_Update'
117+
"repo_name": APP_NAME
116118
}
117119

118120
update_json(CONFIG_FILE, dix)
@@ -123,6 +125,7 @@ def default_config_values():
123125
def get_image_path(image_name: str) -> str:
124126
return resource_temp_path(image_path + image_name)
125127

128+
126129
boold = True
127130
if __name__ == "__main__":
128131
if boold:

0 commit comments

Comments
 (0)