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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# -*- coding: utf-8 -*-
from PyQt4.QtCore import (
QDateTime,
SIGNAL,
)
from PyQt4.QtGui import (
QMessageBox,
)
from PyQt4.QtSql import (
QSqlDatabase,
QSqlQuery,
QSqlTableModel,
QVariant,
)
import ConfigParser
import os
import shutil
import sys
import threading
import urllib2
import zipfile
from util import (
doAlert,
doQuestion,
)
class DataModel(QSqlTableModel):
defaultDbFile = os.path.join(
os.path.split(os.path.realpath(__file__))[0], "models.sqlite")
def __init__(self, parent=None, filename=None):
self.installThreads = {}
self.processes = set()
if filename is None:
filename = DataModel.defaultDbFile
self.db = QSqlDatabase.addDatabase('QSQLITE')
print >> sys.stderr, "Open database at %s" % filename
self.db.setDatabaseName(filename)
self.db.open()
query = QSqlQuery(
'SELECT COUNT(*) '
'FROM sqlite_master '
'WHERE type="table" AND tbl_name="models"',
self.db)
if not query.next() or query.value(0).toInt()[0] < 1:
# Create new table.
print >> sys.stderr, "Table not find, create the table"
query = QSqlQuery(
'CREATE TABLE models ('
'ID INTEGER PRIMARY KEY AUTOINCREMENT, '
'name TEXT, '
'status TEXT, '
'srclang TEXT, '
'trglang TEXT, '
'date DATE, '
'path TEXT, '
'mosesini TEXT, '
'origin TEXT, '
'originMode TEXT, '
'deleted TEXT)',
self.db)
if query.next():
print >> sys.stderr, query.value(0).toString()
# TODO: shoudn't design the deletion checking like this
# Change all deleted models into not deleted in case it failed last
# time.
query = QSqlQuery(
'UPDATE models SET deleted="False" WHERE deleted="True"', self.db)
query = QSqlQuery(
'UPDATE models SET status="READY" WHERE status="ON"', self.db)
super(DataModel, self).__init__(parent, self.db)
self.setTable("models")
self.select()
self.setEditStrategy(QSqlTableModel.OnFieldChange)
def destroy(self):
bExit = False
for i in self.installThreads:
t, flag = self.installThreads[i]
if t.isAlive() and flag:
if not bExit:
if not doQuestion(
"Installing process is running in the background, "
"do you want to terminate them and exit?"):
return False
else:
bExit = True
self.installThreads[i][1] = False
t.join()
if self.db:
self.db.close()
self.db = None
return True
def getQSqlDatabase(self):
return self.db
def getRowID(self, row):
record = self.record(row)
return record.value('ID')
def delModel(self, row):
record = self.record(row)
if str(record.value('deleted').toString()) == 'True':
self.emit(
SIGNAL("messageBox(QString)"),
"The model is deleting, please be patient!")
return
# Hint to decide what to delete.
text = '''You are going to delete the selected model entry.
Do you also want to delete all the model files on the disk?
Click "Yes" to delete model entry and model files.
Click "No" to delete model entry but keep model files.
Click "Cancel" to do nothing.'''
reply = QMessageBox.question(
None, 'Message', text, QMessageBox.Yes, QMessageBox.No,
QMessageBox.Cancel)
if reply == QMessageBox.Cancel:
return
else:
record.setValue('deleted', 'True')
self.changeRecord(row, record)
def delModelThread():
irowid, _ = record.value("ID").toInt()
if irowid in self.installThreads:
t, flag = self.installThreads[irowid]
if t.isAlive() and flag:
self.installThreads[irowid][1] = False
t.join()
if reply == QMessageBox.Yes:
destDir = str(record.value("path").toString())
try:
shutil.rmtree(destDir)
except Exception as e:
self.emit(
SIGNAL("messageBox(QString)"),
"Failed to remove dir: " + destDir)
print >> sys.stderr, str(e)
self.removeRow(row)
# End of Model deleting thread.
t = threading.Thread(target=delModelThread)
t.start()
def newEntry(self):
import random
rec = self.record()
for i in xrange(1, 10):
rec.setValue(i, QVariant(str(random.random())))
self.insertRecord(-1, rec)
doAlert(self.query().lastInsertId().toString())
def changeRecord(self, curRow, record):
# self.emit(SIGNAL("recordUpdated(bool)"), True) #record selection
self.setRecord(curRow, record)
# self.emit(SIGNAL("recordUpdated(bool)"), False) #restore selection
def installModel(self, installParam):
dest = installParam['dest']
# Make dir.
if not os.path.exists(dest):
try:
os.makedirs(str(dest))
except:
doAlert("Failed to create install directory: %s" % dest)
return
# Create entry in db.
rec = self.record()
rec.setValue('name', installParam['modelName'])
rec.setValue('status', 'Fetching Source...')
rec.setValue('path', dest)
rec.setValue('origin', installParam['source'])
rec.setValue('originMode', installParam['sourceMode'])
rec.setValue('date', QDateTime.currentDateTime())
rec.setValue('deleted', 'False')
self.insertRecord(-1, rec)
rowid = self.query().lastInsertId()
# Start thread.
def installThread(irowid):
# Find the current row in model.
def updateRecord(keyvalues):
curRow = None
# TODO: use binary search instead of linear
for i in xrange(0, self.rowCount()):
if self.record(i).value("ID") == rowid:
curRow = i
break
if curRow is not None:
record = self.record(curRow)
for key in keyvalues:
record.setValue(key, keyvalues[key])
self.changeRecord(curRow, record)
return curRow
def checkExit():
# Check thread is ok to run.
if irowid not in self.installThreads or not self.installThreads[irowid][1]:
return True
else:
return False
def markExit():
if irowid in self.installThreads: # Set thread to dead.
self.installThreads[irowid][1] = False
def statusMessageLogMarkExit(status=None, message=None,
exception=None):
if status is not None:
updateRecord({'status': status})
if message is not None:
self.emit(SIGNAL("messageBox(QString)"), message)
print >> sys.stderr, message
if exception is not None:
print >> sys.stderr, str(exception)
markExit()
# 1. Download or copy from local.
# Where the downloaded/copied zip file is:
destFile = os.path.join(str(dest), "model.zip")
# Where the unzipped contents are:
destDir = os.path.join(str(dest), "model")
if installParam['sourceMode'] == 'Local':
fin = fout = None
try:
inFile = str(installParam['source'])
total_size = os.path.getsize(inFile)
fin = open(inFile, 'rb')
chunk_size = 52428800 # 50MB as chunk size
fout = open(destFile, 'wb')
content = fin.read(chunk_size)
download_size = content_size = len(content)
lastMsg = ""
while content_size > 0:
# Check if thread is notified as exit.
if checkExit():
return statusMessageLogMarkExit()
fout.write(content)
if total_size > 0:
msg = 'COPY %.0f%%' % (
download_size * 100.0 / total_size)
else:
msg = 'COPY %d MB' % (download_size / 1048576)
if msg != lastMsg:
updateRecord({'status': msg})
lastMsg = msg
content = fin.read(chunk_size)
content_size = len(content)
download_size += content_size
except Exception as e:
return statusMessageLogMarkExit(
status=(
'Failed copying from: %s'
% installParam['source']),
message=(
"Failed copy model: %s"
% installParam['modelName']),
exception=e)
finally:
if fin:
fin.close()
if fout:
fout.close()
elif installParam['sourceMode'] == 'Internet':
conn = fout = None
try:
conn = urllib2.urlopen(str(installParam['source']))
total_size = int(conn.headers['Content-Length'])
chunk_size = 1048576 # 1MB as chunk size
fout = open(destFile, 'wb')
content = conn.read(chunk_size)
download_size = content_size = len(content)
lastMsg = ""
while content_size > 0:
# Check if thread is notified as exit.
if checkExit():
return statusMessageLogMarkExit()
fout.write(content)
if total_size > 0:
msg = 'DOWNLOAD %.0f%%' % (
download_size * 100.0 / total_size)
else:
msg = 'DOWNLOAD %d MB' % (download_size / 1048576)
if msg != lastMsg:
updateRecord({'status': msg})
lastMsg = msg
content = conn.read(chunk_size)
content_size = len(content)
download_size += content_size
except Exception as e:
return statusMessageLogMarkExit(
status=(
'Failed downloading from: %s'
% installParam['source']),
message=(
"Failed download model: %s"
% installParam['modelName']),
exception=e)
finally:
if conn:
conn.close()
if fout:
fout.close()
else:
return statusMessageLogMarkExit(
status='Unsupported source mode: %s'
% installParam['sourceMode'])
# 2. Unzip.
zfile = fout = None
try:
zfile = zipfile.ZipFile(destFile)
# Check property files.
if "model.ini" not in zfile.namelist():
return statusMessageLogMarkExit(
status=(
'Missing model.ini in model file: %s'
% installParam['sourceMode']),
message=(
"Invalid modle file format because model.ini "
"is missing in the zipped model file, exit "
"installation for model %s"
% installParam['modelName']))
chunk_size = 52428800 # 50MB as chunk size
# Get file size uncompressed.
total_size = 0
for name in zfile.namelist():
total_size += zfile.getinfo(name).file_size
download_size = 0
lastMsg = ""
for i, name in enumerate(zfile.namelist()):
(dirname, filename) = os.path.split(name)
outDir = os.path.join(destDir, dirname)
if not os.path.exists(outDir):
os.makedirs(outDir)
if filename:
fin = zfile.open(name, 'r')
outFile = os.path.join(destDir, name)
fout = open(outFile, 'wb')
content = fin.read(chunk_size)
content_size = len(content)
download_size += content_size
while content_size > 0:
# Check if thread is notified as exit.
if checkExit():
return statusMessageLogMarkExit()
fout.write(content)
if total_size > 0:
msg = 'UNZIP %.0f%%' % (
download_size * 100.0 / total_size)
else:
msg = 'UNZIP %d MB' % (
download_size / 1048576)
if msg != lastMsg:
updateRecord({'status': msg})
lastMsg = msg
content = fin.read(chunk_size)
content_size = len(content)
download_size += content_size
fin.close()
fout.close()
except Exception as e:
return statusMessageLogMarkExit(
status=(
'Failed unzipping from: %s' % installParam['source']),
message=(
"Failed unzip model: %s" % installParam['modelName']),
exception=e)
finally:
if zfile:
zfile.close()
if fin:
fin.close()
if fout:
fout.close()
# 3 Post process and check data validity.
try:
modelini = os.path.join(destDir, "model.ini")
cp = ConfigParser.RawConfigParser()
cp.read(modelini)
mosesini = os.path.join(destDir, 'moses.ini')
if not os.path.exists(mosesini):
raise Exception("Moses ini doesn't exist")
updateRecord({
'srclang': cp.get("Language", 'Source Language').upper(),
'trglang': cp.get("Language", 'Target Language').upper(),
'mosesini': mosesini},
)
except Exception as e:
return statusMessageLogMarkExit(
status='ERROR model format %s' % installParam['source'],
message=(
"Unspported model format: %s"
% installParam['modelName']),
exception=e)
statusMessageLogMarkExit(
status='READY',
message="Model %s Installed!" % installParam['modelName'])
# Send new model signal.
self.emit(SIGNAL("modelInstalled()")) # Record selection.
return
# End of thread func.
# Start the thread.
irowid, _ = rowid.toInt()
t = threading.Thread(target=installThread, args=(irowid, ))
if irowid in self.installThreads: # If old thread is there.
print >> sys.stderr, (
"table rowid %d already has a thread running, stop it"
% irowid)
self.installThreads[irowid][1] = False
self.installThreads[irowid] = [t, True]
t.start()
|