-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
93 lines (84 loc) · 2.81 KB
/
launcher.py
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
from pathlib import Path
from importlib import reload, import_module
import sys
import subprocess
# implement pip as a subprocess:
#subprocess.check_call([sys.executable, '-m', 'pip', 'install', '<packagename>'])
def check_installation():
print("Checking Installed Modules...")
result = subprocess.run([sys.executable, '-m', 'pip', 'freeze'], capture_output=True)
if 'eco2ai' in str(result.stdout):
print("Modules Installed.")
return True
print("Needed modules aren't installed.")
return False
def install():
print("Installing Modules...")
subprocess.run([sys.executable, '-m', 'pip', 'install', 'eco2ai'], capture_output=True)
print("Modules Installed.")
def init():
try:
if not version_file.exists() or not worker_file.exists():
remote_version = requests.get(version_raw_url).text
worker_script = requests.get(worker_raw_url).text
with version_file.open("wt") as f:
f.write(remote_version)
with worker_file.open("wt") as f:
f.write(worker_script)
except Exception as e:
print(e)
version_raw_url = 'https://raw.githubusercontent.com/pikvic/fefu-eco2ai/main/version.txt'
version_file = Path() / 'version.txt'
worker_file = Path() / 'worker.py'
worker_raw_url = 'https://raw.githubusercontent.com/pikvic/fefu-eco2ai/main/worker.py'
def check_updates():
try:
local_version = version_file.read_text()
remote_version = requests.get(version_raw_url).text
print("Local version:", local_version, "Remote Version:", remote_version)
if local_version != remote_version:
return True
return False
except Exception as e:
print(e)
return False
def update():
try:
print("Updating...")
local_version = version_file.read_text()
remote_version = requests.get(version_raw_url).text
worker_file.write_text(requests.get(worker_raw_url).text)
reload(worker)
version_file.write_text(remote_version)
print("Update Finished")
except Exception as e:
print(e)
def start():
work = worker.work()
for i, v in enumerate(work):
print(v)
if i % 5 == 0:
if check_updates():
print("Need to update")
work.send('stop')
break
else:
work.send(None)
else:
work.send(None)
return True
if __name__ == "__main__":
if not check_installation():
install()
if not check_installation():
print("Eco2AI is not installed. Exit")
exit(0)
import requests
init()
import_module("worker")
import worker
while True:
print("Starting worker...")
needsUpdate = start()
if needsUpdate:
update()