forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 21
/
simplecv-installer.py
271 lines (212 loc) · 6.49 KB
/
simplecv-installer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import sys
import tempfile
import urllib2
import warnings
import zipfile
import Tkinter
import simplejson as json
import time
#Global Definitions
output_status = ""
cache_directory = ""
repo_url = "http://www.simplecv.org/installation/manifest.json"
version_to_use = "1.2"
operating_system = None
app = None
current_file = None
update_time = 0.5
def InstallButtonClick():
"""
This is the callback for the Install Button
"""
run()
def CancelButtonClick():
"""
This is the callback for the Cancel Button
"""
sys.exit()
def writeText(text):
"""
This is used to write to the UI console and terminal at the same time
"""
global output_status
output_status.insert(Tkinter.END, text)
output_status.insert(Tkinter.END, "\n")
sys.stdout.write(text)
sys.stdout.write("\n")
def download(URL):
"""
This downloads the file at the specified URL
"""
global cache_directory
global operating_system
global current_file
if URL == None:
warnings.warn("Please provide URL")
return None
tmpdir = cache_directory
writeText("downloading file from:")
writeText(URL)
filename = os.path.basename(URL)
file_extension = filename.split(".")[-1]
current_file = filename
filepath = tmpdir + "\\" + filename
if operating_system == "windows":
filepath = tmpdir + "\\" + filename
else:
filepath = tmpdir + "/" + filename
data = urllib2.urlopen(URL)
chunk_read(data, report_hook=chunk_report, filepath=filepath)
writeText(filename + " saved to disk")
writeText("The file path is:")
writeText(filepath)
return (filepath, filename, file_extension)
def chunk_report(bytes_so_far, chunk_size, total_size):
"""
This is the update callback for the download
"""
global app
global output_status
global current_file
percent = float(bytes_so_far) / total_size
percent = round(percent*100, 2)
writeText("Downloaded " + str(percent) + "% of: " + current_file)
if bytes_so_far >= total_size:
sys.stdout.write('\n')
app.update()
output_status.yview(Tkinter.END)
def chunk_read(response, chunk_size=1024, report_hook=None, filepath = None):
"""
This is for reading parts of the file in chunks for the download
"""
global update_time
if(filepath == None):
#todo: make it setup temp filepath
filepath = None
total_size = response.info().getheader('Content-Length').strip()
total_size = int(total_size)
bytes_so_far = 0
start_time = time.time()
file = open(filepath, 'wb')
while 1:
current_time = time.time()
chunk = response.read(chunk_size)
bytes_so_far += len(chunk)
if not chunk:
break
file.write(chunk)
if report_hook:
if (current_time - start_time) > update_time:
start_time = time.time()
report_hook(bytes_so_far, chunk_size, total_size)
return bytes_so_far
def extract(path):
"""
This extracts a zip file at the specfied system path
"""
tmpdir = cache_directory
zfile = zipfile.ZipFile(path)
writeText("Extracting zipfile")
try:
zfile.extractall(tmpdir)
except:
warnings.warn("Couldn't extract zip file")
return None
return tmpdir
def run_install(path, name = "UNKNOWN", args = None):
"""
This is used to run the installer at the specified path
"""
writeText("Running install for:" + name)
writeText("on path: " + path)
if args:
path = path + " " + args
writeText("width args: " + str(args))
os.system(path)
def run():
"""
This runs the installation process
"""
global cache_directory
global repo_url
global app
global output_status
writeText("Running the installer")
cache_directory = tempfile.mkdtemp()
manifest_path, manifest_name, extension = download(repo_url)
filelist = json.load(open(manifest_path, "r"))
if filelist.has_key("current"):
version_to_use = filelist['current']
if not filelist.has_key(version_to_use):
writeText("I'm sorry but the manifest file didn't include the version number you requested: " + version_to_use)
return
distro_list = filelist[version_to_use]
if not distro_list.has_key(operating_system):
writeText("I'm sorry but the operating system you requested is not found in the manifest file: " + operating_system)
return
urllist = distro_list[operating_system]
extracted_path = None
for url in urllist:
path,name,extension = download(url)
args = None
if(extracted_path):
args = extracted_path
if(extension.lower() == "zip"):
extracted_path = extract(path)
else:
run_install(path, name, args)
app.update()
output_status.yview(Tkinter.END)
writeText("")
writeText("")
writeText("---------------------------------------------------------")
writeText("SimpleCV should now be successfully installed")
writeText("visit http://www.simplecv.org if you run into problems")
writeText("You can now close this installer")
if __name__ == "__main__":
if(len(sys.argv) > 1):
repo_url = sys.argv[1]
if(len(sys.argv) > 2):
version_to_use = sys.argv[2]
#These are name variations associated with what sys.platform returns
windows = ['win32', 'win64']
linux = ['linux2']
mac = ['darwin']
temp_os = sys.platform.lower()
if(any(x in temp_os for x in linux)):
operating_system = "linux"
elif(any(x in temp_os for x in windows)):
operating_system = "windows"
elif(any(x in temp_os for x in mac)):
operating_system = "mac"
else:
operating_system = temp_os
header = "Welcome to the SimpleCV installer"
instructions = "This installer will try to download and install all the libraries you need"
instructions_cont = "please be patient while files download, if you are having problems visit:"
website = "http://www.simplecv.org"
app = Tkinter.Tk()
w = Tkinter.Label(app, text=header, font=("default",20))
w.pack(padx=30, pady=20)
w = Tkinter.Label(app, text="")
w.pack()
w = Tkinter.Label(app, text=instructions)
w.pack()
w = Tkinter.Label(app, text=instructions_cont)
w.pack()
w = Tkinter.Label(app, text=website)
w.pack(pady=10)
w = Tkinter.Button(app, text="Install SimpleCV", command=InstallButtonClick)
w.pack(side=Tkinter.LEFT, padx=10, pady=10)
w = Tkinter.Button(app, text="Cancel", command=CancelButtonClick)
w.pack(side=Tkinter.LEFT, padx=10, pady=10)
scrollbar = Tkinter.Scrollbar(app)
scrollbar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)
output_status=Tkinter.Text(app,height=10,width=50,background='black', foreground='white', yscrollcommand=scrollbar.set)
output_status.pack(side=Tkinter.BOTTOM)
scrollbar.config(command=output_status.yview)
output_status.insert(Tkinter.END, "Output Console\n")
output_status.insert(Tkinter.END, "------------------------------\n")
app.title('SimpleCV Installer - http://www.simplecv.org')
app.mainloop()