Skip to content

Commit 84ccc8f

Browse files
Merge pull request #18 from LeithMerrifield/Updater
Updater
2 parents ba53d54 + 9a6e2e0 commit 84ccc8f

File tree

7 files changed

+252
-22
lines changed

7 files changed

+252
-22
lines changed

Autoscanner.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ a = Analysis(
88
['Main.py'],
99
pathex=[],
1010
binaries=[],
11-
datas=[('src/kv/','./src/kv/'),('src/images/','src/images/'),('src/settings.json','src/')],
11+
datas=[('src/kv/','./src/kv/'),('src/images/','src/images/'),('src/settings.json','src/'),('src/versions.json','src/versions.json')],
1212
hiddenimports=[],
1313
hookspath=[],
1414
hooksconfig={},

Updater.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import subprocess
2+
from urllib.request import urlretrieve
3+
import src.CheckDriver as CheckDriver
4+
import json
5+
import shutil
6+
import os
7+
import tempfile
8+
from time import sleep
9+
from zipfile import ZipFile
10+
11+
LATEST_APP_VERSION = "curl https://leithmerrifield.github.io/Autoscanner/LATESTVERSION"
12+
DOWNLOAD_LIST = "curl https://leithmerrifield.github.io/Autoscanner/downloads.json"
13+
14+
15+
def get_app_download_link(target_version):
16+
downloads = CheckDriver.get_webpage_content(DOWNLOAD_LIST)
17+
18+
downloads_json = json.loads(downloads)
19+
for version in downloads_json:
20+
if version["Version"] == target_version:
21+
return version["Link"]
22+
23+
24+
def delete_contents():
25+
keep = ["Updater.exe", "database.json", "src", "userdata", "app.zip"]
26+
for root, dir, files in os.walk("./Scanner"):
27+
for name in files:
28+
if name not in keep:
29+
try:
30+
os.remove(os.path.join(root, name))
31+
# print(os.path.join(root, name))
32+
except:
33+
pass
34+
for name in dir:
35+
if name not in keep and "userdata" not in root:
36+
shutil.rmtree(os.path.join(root, name))
37+
38+
39+
def move_from_temp():
40+
temp_destination = tempfile.gettempdir()
41+
shutil.copytree(
42+
temp_destination + "/Autoscanner",
43+
"./Scanner",
44+
dirs_exist_ok=True,
45+
)
46+
47+
48+
def check_scanner_folder():
49+
if not os.path.isdir("./Scanner"):
50+
os.mkdir("./Scanner")
51+
return
52+
53+
54+
def create_shortcut():
55+
ps = subprocess.check_output(
56+
"powershell.exe" + " [System.Environment]::GetFolderPath('Desktop')",
57+
stderr=subprocess.STDOUT,
58+
shell=True,
59+
)
60+
desktop = ps.decode("UTF-8")[:-2]
61+
items = os.listdir(desktop)
62+
for item in items:
63+
if "Autoscanner" in item:
64+
os.remove(desktop + "/" + item)
65+
66+
current_dir = os.path.abspath(os.getcwd() + "/Scanner/Autoscanner.exe")
67+
current_dir = current_dir.split("\\")
68+
current_dir = "/".join(current_dir)
69+
# fmt: off
70+
command = '''param(
71+
[string]$path
72+
)
73+
74+
$ScriptObj = New-Object -ComObject ("WScript.shell")
75+
$destination = [System.Environment]::GetFolderPath("Desktop") + "/Autoscanner.lnk"
76+
$shortcut = $ScriptObj.CreateShortcut($destination)
77+
78+
79+
$shortcut.TargetPath = $path
80+
$separator = "/"
81+
$workingdir = $path -split $separator
82+
$workingdir = $workingdir[0..($workingdir.Length - 2)] -join "/"
83+
$shortcut.WorkingDirectory = $workingdir
84+
$shortcut.Save()
85+
86+
'''
87+
# fmt: on
88+
with open("./shortcut.ps1", "w") as openfile:
89+
openfile.write(command)
90+
sleep(2)
91+
subprocess.Popen(
92+
[
93+
"powershell.exe",
94+
"-Command",
95+
"&{Set-ExecutionPolicy Bypass -scope CurrentUser -Force}",
96+
],
97+
)
98+
sleep(1)
99+
subprocess.run(
100+
[
101+
"powershell.exe",
102+
f"./shortcut.ps1 {current_dir}",
103+
],
104+
)
105+
sleep(1)
106+
subprocess.Popen(
107+
[
108+
"powershell.exe",
109+
"-Command",
110+
"&{Set-ExecutionPolicy Restricted -scope CurrentUser -Force}",
111+
],
112+
)
113+
return
114+
115+
116+
def clean():
117+
os.remove("./shortcut.ps1")
118+
return
119+
120+
121+
def update():
122+
# compare versions
123+
check_scanner_folder()
124+
local_version = CheckDriver.get_local_version(
125+
"App_Version", filepath="./Scanner/src/versions.json"
126+
)
127+
remote_version = CheckDriver.get_webpage_content(LATEST_APP_VERSION)
128+
129+
if local_version == remote_version:
130+
return
131+
132+
# get download link of newest version
133+
# get zip
134+
temp_destination = tempfile.gettempdir()
135+
CheckDriver.get_remote_zip(
136+
get_app_download_link(remote_version), temp_destination + "/app.zip"
137+
)
138+
delete_contents()
139+
CheckDriver.unpack_zip(temp_destination + "/app.zip", temp_destination)
140+
sleep(2)
141+
move_from_temp()
142+
CheckDriver.check_file(filepath="./Scanner/src/versions.json")
143+
CheckDriver.update_version(
144+
remote_version, "App_Version", filepath="./Scanner/src/versions.json"
145+
)
146+
147+
sleep(2)
148+
create_shortcut()
149+
sleep(2)
150+
clean()
151+
152+
153+
try:
154+
update()
155+
# delete_contents()
156+
# os.unlink("./python3.dll")
157+
except Exception as e:
158+
with open("./log.txt", "w") as openfile:
159+
openfile.write(str(e))

Updater.spec

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- mode: python ; coding: utf-8 -*-
2+
3+
4+
block_cipher = None
5+
6+
7+
a = Analysis(
8+
['Updater.py'],
9+
pathex=[],
10+
binaries=[],
11+
datas=[],
12+
hiddenimports=[],
13+
hookspath=[],
14+
hooksconfig={},
15+
runtime_hooks=[],
16+
excludes=[],
17+
win_no_prefer_redirects=False,
18+
win_private_assemblies=False,
19+
cipher=block_cipher,
20+
noarchive=False,
21+
)
22+
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
23+
24+
exe = EXE(
25+
pyz,
26+
a.scripts,
27+
a.binaries,
28+
a.zipfiles,
29+
a.datas,
30+
[],
31+
name='Updater',
32+
debug=False,
33+
bootloader_ignore_signals=False,
34+
strip=False,
35+
upx=True,
36+
upx_exclude=[],
37+
runtime_tmpdir=None,
38+
console=True,
39+
disable_windowed_traceback=False,
40+
argv_emulation=False,
41+
target_arch=None,
42+
codesign_identity=None,
43+
entitlements_file=None,
44+
)

build.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
./venv/Scripts/Activate
2+
python.exe -m PyInstaller ./Updater.spec
3+
python.exe -m PyInstaller ./Autoscanner.spec
4+
5+
python.exe ./build.py
6+

build.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import shutil
2+
3+
shutil.copytree("./dist/Autoscanner/", "./dist/release/Scanner")
4+
shutil.make_archive("./dist/Autoscanner", "zip", "./dist/release")

src/CheckDriver.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from zipfile import ZipFile
66
from urllib.request import urlretrieve
77

8+
LATEST_RELEASE_STABLE = (
9+
"curl https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE"
10+
)
811

9-
def get_driver_version():
10-
ps = "curl https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE"
11-
return subprocess.check_output(ps, shell=True).decode("utf-8")
12+
13+
def get_webpage_content(url):
14+
return subprocess.check_output(url, shell=True).decode("utf-8")
1215

1316

1417
def get_download_link(version_number):
@@ -29,34 +32,39 @@ def get_download_link(version_number):
2932
return link
3033

3134

32-
def check_file():
33-
if exists("./src/driver_version.json"):
35+
def check_file(filepath="./src/versions.json"):
36+
if exists("./src/versions.json"):
3437
return
3538
else:
36-
with open("./src/driver_version.json", "w", encoding="utf-8") as openfile:
37-
template = {"Chrome_Driver_Version": ""}
39+
with open(filepath, "w", encoding="utf-8") as openfile:
40+
template = {"Chrome_Driver_Version": "", "App_Version": ""}
3841
json.dump(template, openfile, indent=4)
3942
return
4043

4144

42-
def get_local_driver_version():
45+
def unpack_zip(filepath, destination):
46+
with ZipFile(filepath, "r") as zipObj:
47+
zipObj.extractall(destination)
48+
49+
50+
def get_local_version(version_type, filepath="./src/versions.json"):
4351
version = None
4452
try:
45-
with open("./src/driver_version.json", "r", encoding="utf-8") as openfile:
53+
with open(filepath, "r", encoding="utf-8") as openfile:
4654
json_object = json.load(openfile)
47-
version = json_object["Chrome_Driver_Version"]
48-
except KeyError:
55+
version = json_object[version_type]
56+
except:
4957
version = ""
5058
return version
5159

5260

53-
def update_version(version):
61+
def update_version(version, version_type, filepath="./src/versions.json"):
5462
json_object = None
55-
with open("./src/driver_version.json", "r", encoding="utf-8") as openfile:
63+
with open(filepath, "r", encoding="utf-8") as openfile:
5664
json_object = json.load(openfile)
5765

58-
with open("./src/driver_version.json", "w", encoding="utf-8") as openfile:
59-
json_object["Chrome_Driver_Version"] = version
66+
with open(filepath, "w", encoding="utf-8") as openfile:
67+
json_object[version_type] = version
6068
json.dump(json_object, openfile, indent=4)
6169

6270

@@ -65,20 +73,25 @@ def clean():
6573
return
6674

6775

76+
def get_remote_zip(url, filename):
77+
urlretrieve(url, filename)
78+
return
79+
80+
6881
def compare_and_download():
6982
check_file()
70-
local_version = get_local_driver_version()
71-
remote_version = get_driver_version()
83+
local_version = get_local_version("Chrome_Driver_Version")
84+
remote_version = get_webpage_content(LATEST_RELEASE_STABLE)
7285

7386
if local_version.split(".")[0] == remote_version.split(".")[0]:
7487
print("chrome driver up to date")
7588
return
7689

7790
remote_download_link = get_download_link(remote_version)
7891

79-
urlretrieve(remote_download_link, "./driver.zip")
92+
get_remote_zip(remote_download_link, "./driver.zip")
93+
94+
unpack_zip("./driver.zip", "./")
8095

81-
with ZipFile("./driver.zip", "r") as zipObj:
82-
zipObj.extractall()
83-
update_version(remote_version)
96+
update_version(remote_version, "Chrome_Driver_Version")
8497
clean()

src/versions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Chrome_Driver_Version": "",
3+
"App_Version" : "v1.4"
4+
}

0 commit comments

Comments
 (0)