-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdater.py
More file actions
146 lines (120 loc) · 3.82 KB
/
Updater.py
File metadata and controls
146 lines (120 loc) · 3.82 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
import subprocess
from urllib.request import urlretrieve
import src.CheckDriver as CheckDriver
import json
import shutil
import os
import tempfile
from time import sleep
from zipfile import ZipFile
from win32api import GetLongPathName
LATEST_APP_VERSION = "curl https://leithmerrifield.github.io/Autoscanner/LATESTVERSION"
DOWNLOAD_LIST = "curl https://leithmerrifield.github.io/Autoscanner/downloads.json"
def get_app_download_link(target_version):
downloads = CheckDriver.get_webpage_content(DOWNLOAD_LIST)
downloads_json = json.loads(downloads)
for version in downloads_json:
if version["Version"] == target_version:
return version["Link"]
def delete_contents():
keep = ["Updater.exe", "database.json", "src", "userdata", "app.zip"]
for root, dir, files in os.walk("./"):
for name in files:
if name not in keep:
try:
os.remove(os.path.join(root, name))
# print(os.path.join(root, name))
except:
pass
for name in dir:
if name not in keep and "userdata" not in root:
shutil.rmtree(os.path.join(root, name))
def move_from_temp():
temp_destination = GetLongPathName(tempfile.gettempdir())
os.remove(temp_destination + "/Autoscanner/Updater.exe")
shutil.copytree(
temp_destination + "/Autoscanner",
"./",
dirs_exist_ok=True,
)
def check_scanner_folder():
if not os.path.isdir("./"):
os.mkdir("./")
return
def create_shortcut():
ps = subprocess.check_output(
"powershell.exe" + " [System.Environment]::GetFolderPath('Desktop')",
stderr=subprocess.STDOUT,
shell=True,
)
desktop = ps.decode("UTF-8")[:-2]
items = os.listdir(desktop)
for item in items:
if "Autoscanner" in item:
os.remove(desktop + "/" + item)
current_dir = os.path.abspath(os.getcwd() + "/Autoscanner.exe")
current_dir = current_dir.split("\\")
current_dir = "/".join(current_dir)
# fmt: off
command = '''param(
[string]$path
)
$ScriptObj = New-Object -ComObject ("WScript.shell")
$destination = [System.Environment]::GetFolderPath("Desktop") + "/Autoscanner.lnk"
$shortcut = $ScriptObj.CreateShortcut($destination)
$shortcut.TargetPath = $path
$separator = "/"
$workingdir = $path -split $separator
$workingdir = $workingdir[0..($workingdir.Length - 2)] -join "/"
$shortcut.WorkingDirectory = $workingdir
$shortcut.Save()
'''
# fmt: on
with open("./shortcut.ps1", "w") as openfile:
openfile.write(command)
sleep(2)
subprocess.run(
[
"powershell.exe",
f"./shortcut.ps1 {current_dir}",
],
)
sleep(1)
return
def clean():
os.remove("./shortcut.ps1")
return
def update():
# compare versions
# check_scanner_folder()
local_version = CheckDriver.get_local_version(
"App_Version", filepath="./src/versions.json"
)
remote_version = CheckDriver.get_webpage_content(LATEST_APP_VERSION)
if local_version == remote_version:
return
# get download link of newest version
# get zip
temp_destination = GetLongPathName(tempfile.gettempdir())
CheckDriver.get_remote_zip(
get_app_download_link(remote_version), temp_destination + "/app.zip"
)
delete_contents()
CheckDriver.unpack_zip(
temp_destination + "/app.zip", temp_destination + "/Autoscanner"
)
sleep(2)
move_from_temp()
CheckDriver.check_file(filepath="./src/versions.json")
CheckDriver.update_version(
remote_version, "App_Version", filepath="./src/versions.json"
)
sleep(2)
create_shortcut()
sleep(2)
clean()
try:
update()
except Exception as e:
with open("./log.txt", "w") as openfile:
openfile.write(str(e))