forked from seeebek/EliteOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
144 lines (122 loc) · 5.51 KB
/
update.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
# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, QUrl, QThread, QString
from updateUI import Ui_Update
from threadworker import Worker
import os
from os import makedirs, rename, remove
from os.path import isdir, isfile
import requests
import time
import sys
import subprocess
class UpdateDialog(QDialog, Ui_Update):
def __init__(self, path, appversion, newupd):
QDialog.__init__(self)
self.app_path = path
self.setupUi(self)
self.exiting = False
self.appversion = appversion
self.newupd = newupd
self.filepath = ""
if not newupd is None:
self.label.setText("New version found: "+newupd[1])
self.download.setEnabled(True)
#self.download.setEnabled(True)
self.download.clicked.connect(self.downloadFile)
self.check.clicked.connect(self.checkUpdate)
self.thread = Worker()
self.connect(self.thread, SIGNAL("output(QString, QString)"), self.showUpdateAvailable)
self.connect(self.thread, SIGNAL("end()"), self.showNoUpdate)
self.downloader = Downloader(self)
self.connect(self.downloader, SIGNAL("loaded(int, int)"), self.updateProgress)
self.connect(self.downloader, SIGNAL("finished()"), self.downloadFinished)
self.connect(self.downloader, SIGNAL("finishederror()"), self.downloadFinishedError)
def closeEvent(self, event):
self.emit(SIGNAL("exiting()"))
event.accept()
def downloadFile(self):
self.check.setEnabled(False)
self.download.setEnabled(False)
if not self.newupd is None:
self.label.setText("Starting download.")
url = "http://sourceforge.net/projects/eliteocr/files/"+self.newupd[0]+"/EliteOCR."+self.newupd[1]+".zip"
#url = "http://blog.fancyrhino.com/wp-content/uploads/2014/03/3.jpg"
if not isdir(unicode(self.app_path)+u""+ os.sep +".."+ os.sep +"update"+ os.sep +""):
makedirs(unicode(self.app_path)+u""+ os.sep +".."+ os.sep +"update"+ os.sep +"")
self.filepath = unicode(self.app_path)+u""+ os.sep +".."+ os.sep +"update"+ os.sep +"EliteOCR."+unicode(self.newupd[1])+u".zip.part"
#print self.filepath
self.downloader.get(url, self.filepath)
def downloadFinished(self):
if isfile(self.filepath[:-5]):
remove(self.filepath[:-5])
rename(self.filepath, self.filepath[:-5])
self.progress_bar.setMaximum(1)
self.progress_bar.setValue(0)
self.label.setText("Download finished. You can find it in the update directory.")
subprocess.Popen(r'explorer /select,"'+unicode(self.app_path).encode(sys.getfilesystemencoding())+ os.sep +".."+ os.sep +"update"+ os.sep +"EliteOCR."+unicode(self.newupd[1]).encode(sys.getfilesystemencoding())+".zip")
def downloadFinishedError(self):
self.progress_bar.setMaximum(1)
self.progress_bar.setValue(0)
self.label.setText("Error while downloading.")
def updateProgress(self, done, total):
self.progress_bar.setMaximum(total)
self.progress_bar.setValue(done)
self.label.setText("Downloading: "+unicode("%.2f" % (done/1048576.0))+"MB / "+unicode("%.2f" % (total/1048576.0))+"MB")
def showNoUpdate(self):
self.label.setText("No updates found.")
self.check.setEnabled(True)
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(1)
self.progress_bar.setValue(0)
def showUpdateAvailable(self, dir, appversion):
self.newupd = (dir, appversion)
self.label.setText("New version found: "+appversion)
self.check.setEnabled(True)
self.download.setEnabled(True)
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(1)
self.progress_bar.setValue(0)
def checkUpdate(self):
self.label.setText("Looking for new updates")
self.check.setEnabled(False)
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(0)
self.progress_bar.setValue(0)
self.thread.check(self.appversion)
class Downloader(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.url = ""
self.path = "./"
self.exiting = False
self.connect(parent, SIGNAL("exiting()"), self.abort)
def abort(self):
self.exiting = True
def get(self, url, path):
self.url = url
self.path = path
self.start()
def run(self):
url = self.url
localFilename = self.path
with open( localFilename, 'wb') as f:
start = time.clock()
r = requests.get(url, stream=True)
total_length = r.headers.get('content-length')
dl = 0
if total_length is None: # no content length header
f.write(r.content)
else:
for chunk in r.iter_content(1024):
if self.exiting:
return
dl += len(chunk)
f.write(chunk)
done = int(50 * dl / int(total_length))
speed = (dl//(time.clock() - start)) #in bps
self.emit(SIGNAL("loaded(int,int)"), int(dl), int(total_length))
if (dl == total_length) and (not self.exiting):
self.emit(SIGNAL("finished()"))
else:
self.emit(SIGNAL("finishederror()"))