-
Notifications
You must be signed in to change notification settings - Fork 9
/
fwget.py
177 lines (168 loc) · 7.14 KB
/
fwget.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import requests
import hashlib
try:
from kivymd.toast import toast
except:
print('no toast for you')
# toast or print
def tprint(msg):
try:
toast(msg)
except:
print(msg)
class FWGet():
def __init__(self, cache):
self.cachePath = cache
self.repoURL = "http://null"
self.dirname = "null"
if not os.path.exists(self.cachePath):
os.makedirs(self.cachePath)
tprint("Created NineRiFt cache directory")
self.progress = 0
self.maxprogress = 100
self.model = ''
self.getboth = False
self.BLE = []
self.DRV = []
self.BMS = []
def setModel(self, model):
self.model = model
def setRepo(self, repo):
self.repoURL = repo
def md5Checksum(self, filePath, url):
m = hashlib.md5()
if url==None:
with open(filePath, 'rb') as fh:
m = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
else:
r = requests.get(url)
for data in r.iter_content(8192):
m.update(data)
return m.hexdigest()
def getFile(self, FWtype, version):
if (self.repoURL == "http://null" or self.dirname == "null"):
tprint("You need to load a valid repo first.")
return(False)
noInternet = False
if not os.path.exists(self.cachePath + "/" + self.dirname + "/"):
os.makedirs(self.cachePath + "/" + self.dirname + "/")
print("Created repo cache directory")
try:
r = requests.head(self.repoURL)
if (r.status_code != 200):
noInternet = True
tprint("Failed to connect to the repo, using local files if available (Server response not 200)")
except requests.ConnectionError:
tprint("Failed to connect to the repo, using local files if available (requests.ConnectionError)")
noInternet = True
if self.model.startswith('m365'):
if FWtype is 'DRV' and self.getboth is False:
filename = FWtype.upper() + version + ".bin.enc"
self.getboth = True
else:
filename = FWtype.upper() + version + ".bin"
else:
filename = FWtype.upper() + version + ".bin.enc"
completePath = self.cachePath + "/" + self.dirname + "/" + filename
isFilePresent = os.path.isfile(completePath)
if noInternet == False:
response = requests.get(self.repoURL + FWtype.lower() + "/" + filename + ".md5")
with open(completePath + ".md5", 'wb') as f:
f.write(response.content)
checksum = response.text
if isFilePresent:
match = self.md5Checksum(completePath, None) == checksum
else:
match = False
else:
if isFilePresent:
with open(completePath + ".md5", "r") as md5cached:
checksum = md5cached.read()
match = self.md5Checksum(completePath, None) == checksum
if (isFilePresent and match):
print(filename + ' was cached; moving on')
return(True, completePath)
else:
url = self.repoURL + FWtype.lower() + "/" + filename
try:
print('download started')
r = requests.head(url)
if (r.status_code == 404):
tprint("Failed to fetch " + filename + " (Error 404 file not found)")
return(False, completePath)
print('Beginning file download; writing to ' + completePath)
url = self.repoURL + FWtype.lower() + "/" + filename
print("URL: " + url)
r = requests.get(url)
with open(completePath, 'wb') as f:
f.write(r.content)
if (r.status_code == 200):
print(filename + " downloaded successfully.")
print('download finished')
with open(completePath + ".md5", "r") as md5cached:
checksum = md5cached.read()
match = self.md5Checksum(completePath, None) == checksum
if not match:
if os.path.exists(completePath):
os.remove(completePath)
else:
print("The file does not exist")
tprint('File was corrupted. try again?')
return(True, completePath)
else:
tprint("Server couldn't respond to download request. Local files aren't available. Aborting.")
return(False, completePath)
except requests.ConnectionError:
tprint("Connection error. Local files aren't available. Aborting.")
return(False, completePath)
def loadRepo(self, jsonURL):
d = ''
noInternet = False
hashedName = hashlib.md5(jsonURL.encode("utf-8")).hexdigest()
try:
r = requests.head(jsonURL)
if (r.status_code != 200):
noInternet = True
tprint("Failed to fetch JSON! Will use cached if available. (Server response not 200)")
except requests.ConnectionError:
tprint("Failed to fetch JSON! Will use cached if available. (requests.ConnectionError)")
noInternet = True
if noInternet == False:
try:
r = requests.get(jsonURL)
with open(self.cachePath + hashedName + ".json", 'wb') as f:
f.write(r.content)
with open(self.cachePath + hashedName + ".json") as f:
d = eval(f.read())
print("Fetched repo JSON.")
except requests.ConnectionError:
tprint("Failed to grab JSON! (requests.ConnectionError)")
return(False)
elif os.path.isfile(self.cachePath + hashedName + ".json"):
with open(self.cachePath + hashedName + ".json") as f:
d = eval(f.read())
print("Fetched cached repo JSON.")
else:
tprint("Couldn't download file and couldn't load from cache. Aborting.")
return(False)
self.dirname = str(d["repo"]["infos"]["dirname"])
self.repoURL = str(d["repo"]["infos"]["files_URL"])
name = str(d["repo"]["infos"]["name"])
self.DRV = d["repo"]["files"]["DRV"]
self.BMS = d["repo"]["files"]["BMS"]
self.BLE = d["repo"]["files"]["BLE"]
print("Loaded the repo \"" + name+ "\" hosted at " + self.repoURL + ". DRV:"
+ str(self.DRV) + " BMS:" + str(self.BMS) + " BLE:" + str(self.BLE))
return(True, self.dirname, self.repoURL, name, self.DRV, self.BMS, self.BLE)
def Gimme(self, firm, ver):
tprint('download started')
self.getFile(firm,ver)
self.getFile(firm, ver)
tprint('download finished')