-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoUpdater.py
131 lines (98 loc) · 3.32 KB
/
AutoUpdater.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
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
"""
- LICENSE: GPL-3.0
- Made by sByte
Auto update your scripts, its useful when you use the same
script in various servers and need to change something
in a specific server, this is compatible with local files
and web files, like in github.
Always keep this script at the top of all other scripts that
will receive an update.
Add this to your config:
[autoupdater]
[autoupdater.yourscript]
url = "../path/to/the/script/yourscript.py"
[autoupdater.mycoolscript]
url = "http://myhttpserver/mycoolscript.py"
"""
from piqueserver.config import config
from urllib.request import urlopen
import hashlib
import os
def updateScript(content, scriptname):
if isinstance(content, bytes):
content = content.decode("utf-8")
try:
f = open("./scripts/%s.py"%scriptname, "w")
f.write(content)
f.close()
return True
except Exception as e:
print(e)
return False
def getScriptContent(configurl):
content = ""
if configurl.startswith("."):
f = open(configurl)
content = f.read()
f.close()
elif configurl.startswith("http"):
content = urlopen(configurl).read()
return content
# if True, needs update
def needUpdate(content_url, scriptpath):
if not os.path.isfile(scriptpath):
return True
if not content_url:
return False
fscript = open(scriptpath)
content_script = fscript.read()
fscript.close()
if not content_script:
return True
return not getfileHash(content_url) == getfileHash(content_script)
def getfileHash(content):
if isinstance(content, bytes):
content = content.decode("utf-8")
_hash = hashlib.sha512()
_hash.update(content.encode("utf-8"))
return _hash.hexdigest()
def checkOriginalScriptExists(url_or_path):
if url_or_path.startswith("."):
return os.path.isfile(url_or_path)
elif url_or_path.startswith("http"):
try:
code = urlopen(url_or_path).getcode()
return code == 200
except:
return False
config_d = config.get_dict()
if not "autoupdater" in config_d:
print("\033[93m--------------------------------")
print("\033[93m!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("\033[91m- PLEASE CREATE THE AUTO UPDATER")
print("\033[91m- SECTOR IN YOUR config.TOML")
print("\033[93m!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("\033[93m--------------------------------\033[0m")
exit(0)
print("\033[92m--------------------------")
print("\033[92m- STARTING SCRIPTS UPDATE!\033[0m")
for script_name in config_d["autoupdater"]:
if script_name not in config_d["scripts"]:
print("\033[94m - Ignoring \033[35m%s\033[94m, script is not enabled in this server.\033[0m"%script_name)
continue
script_obj = config_d["autoupdater"][script_name]
if not checkOriginalScriptExists(script_obj["url"]):
print("\033[31m - Can't find the script \033[35m%s\033[31m, please check if the URL/PATH: \033[35m%s \033[31mis correctly."%(script_name,script_obj["url"]))
continue
content = getScriptContent(script_obj["url"])
if not needUpdate(content, "./scripts/%s.py"%script_name):
print("\033[37m - \033[35m%s\033[37m Its already up to date."%script_name)
continue
if updateScript(content, script_name):
print("\033[37m - \033[35m%s\033[37m Got updated."%script_name)
else:
print("\033[93m - \033[35m%s\033[93m Cant get updated."%script_name)
print("\033[92m- ALL SCRIPTS GOT UPDATED!")
print("\033[92m--------------------------\033[0m")
def apply_script(protocol, connection, config):
return protocol, connection