/
code.py
132 lines (108 loc) · 4.28 KB
/
code.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
import web
import view, config
import shutil
from view import render
import document_compare
import threading
import os
import Queue
urls = (
'/', 'index',
'/details', 'details',
'/update', 'update'
)
lo =['/media/pierre-eric/309451c6-b1c2-4554-99a1-30452150b211/libreoffice-master-ro/', '/media/pierre-eric/309451c6-b1c2-4554-99a1-30452150b211/libreoffice-4.1/', '/media/pierre-eric/309451c6-b1c2-4554-99a1-30452150b211/libreoffice-3.6/']
def sha_to_libreoffice(sha):
if sha == 'f5248b4':
return lo[0]
elif sha == '9c2b278':
return lo[1]
elif sha == 'b059b03':
return lo[2]
else:
raise
class index:
def GET(self):
return render.base(view.listing())
def POST(self):
form = web.input(doc={})
basename = form['doc'].filename.replace(' ', '_')
tempfile = '/tmp/' + basename
with open(tempfile, 'wb') as saved:
shutil.copyfileobj(form['doc'].file, saved)
uid = document_compare.compute_uid (tempfile)
# Remove old entries
config.DB.delete('scores', where='id = "' + uid + '"')
config.DB.delete('items', where='id = "' + uid + '"')
b, ext = os.path.splitext(form['doc'].filename)
# Insert
config.DB.insert('items', id=uid, name=b, pagecount=-1, extension=ext)
# Start bg thread
a = threading.Thread(target=worker, args=(tempfile, False, ))
a.start()
return render.base(view.listing())
class details:
def GET(self):
i = web.input(uid=None, sha=None)
if i.uid == None or i.sha == None:
return render.base(view.listing())
else:
results = config.DB.query("SELECT * FROM items JOIN scores WHERE scores.id = items.id AND scores.id = '" + str(i.uid) + "' AND scores.commitsha='" + i.sha + "'")
rows = results.list()
print ("Row count: " + str(len(rows)))
if len(rows) == 0:
return render.base(view.listing())
row = rows[0]
return render.details(row)
class update:
def GET(self):
i = web.input(uid=None, sha=None, full=0)
if i.uid == None:
return render.base(view.listing())
else:
results = config.DB.select('items', dict(n=str(i.uid)), where="id=$n")
rows = results.list()
print ("Row count: " + str(len(rows)))
if len(rows) > 0:
row = rows[0]
results = config.DB.query("SELECT * FROM scores WHERE id='" + str(i.uid) + "'")
if i.full == 0:
for r in results.list():
print ("Updating: " + r['commitsha'])
outdir = os.getcwd() + '/static/' + r['commitsha'] + '/'
score, pagecount, all_scores = document_compare.compare_pdf_using_images(row['id'], outdir)
config.DB.update('scores', where='id="' + str(i.uid) + '" AND commitsha="' + str(r['commitsha']) + '"', olscore=score[0],ollscore=score[1], olwscore=score[2], details=str(all_scores).strip('[]'))
else:
shutil.copy(os.getcwd() + '/static/originals/' + str(i.uid) + row['extension'], '/tmp/')
if results[0].total_version == len(lo):
config.DB.delete('scores', where='id = "' + str(i.uid) + '"')
a = threading.Thread(target=worker, args=('/tmp/' + str(i.uid) + row['extension'], False, ))
a.start()
else:
a = threading.Thread(target=worker, args=('/tmp/' + str(i.uid) + row['extension'], True, ))
a.start()
return render.base(view.listing())
def worker(tempfile, only_add_missing):
baseoutdir = os.getcwd() + '/static/'
uid = document_compare.init_document_compare (tempfile, baseoutdir)
for libreoffice in lo:
sha = document_compare.get_libreoffice_sha(libreoffice)
outdir = baseoutdir + sha + '/'
if only_add_missing:
results = config.DB.select('scores', dict(n=str(uid)), where="id=$n AND commitsha='" + str(sha) + "'")
if len(results.list()) > 0:
continue
b, ext = os.path.splitext(tempfile)
filename = uid + ext
document_compare.generate_pdf_for_doc(baseoutdir + 'originals/' + filename, uid, libreoffice, outdir)
document_compare.generate_fullres_images_from_pdf(filename, uid, outdir)
score, pagecount, all_scores = document_compare.compare_pdf_using_images(uid, outdir)
# Update page count
config.DB.update('items', where='id="' + str(uid) + '"', pagecount=pagecount)
# Update score
config.DB.insert('scores', id=str(uid), commitsha=sha, olscore=score[0],ollscore=score[1], olwscore=score[2], details=str(all_scores).strip('[]'))
os.remove(tempfile)
if __name__ == "__main__":
app = web.application(urls, globals())
app.internalerror = web.debugerror
app.run()