This repository has been archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.py
186 lines (139 loc) · 4.46 KB
/
Updater.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
import os
import sys
import sqlite3
import shutil
import requests
import threading
import zipfile
import locale
import time
import urllib.request
import html2text as h2t
import tkinter.ttk as ttk
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
if 'en' in str(locale.getdefaultlocale()):
string = 'Downloading new version...'
string2 = 'Installing new version...'
string3 = 'The update is running, exit now will cause error! Do you want continue?'
string4 = 'Warning'
elif 'it' in str(locale.getdefaultlocale()):
string = 'Download della nuova versione...'
string2 = 'Installazione della nuova versione...'
string3 = "Aggiornamento in corso, uscire ora causera' errori! Vuoi continuare?"
string4 = 'Attenzione'
else:
string = 'Downloading new version...'
string2 = 'Installing new version...'
string3 = 'The update is running, exit now will cause error! Do you want continue?'
string4 = 'Warning'
class Gui(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def callback(self):
self.message = messagebox.askyesno(string4, string3)
if self.message:
sys.exit(0)
def run(self):
self.download_window = Tk()
self.download_window.protocol("WM_DELETE_WINDOW", self.callback)
self.download_window.title('Lite Mails Updater')
self.download_window.geometry('430x100')
self.download_window.resizable(False, False)
self.download_window.iconbitmap('updater.ico')
self.download_window.style = Style()
self.download_window.style.theme_use('vista')
self.pb = ttk.Progressbar(self.download_window, mode='determinate')
self.pb.grid(ipadx=150, padx=15, pady=15, sticky='n')
self.lb = Label(self.download_window, text=string)
self.lb.grid(padx=15, sticky='s')
self.lb2 = Label(self.download_window)
self.lb2.grid(padx=25, sticky='s')
self.download_window.mainloop()
def update():
if os.path.isfile('bin/version.txt'):
with open('bin/version.txt', 'r') as f:
current_version = f.read()
f.close()
else:
print('Version file not found! Cannot run updater!')
sys.exit()
try:
r = requests.get('http://alex3025.github.io/litemails.html')
new_version = h2t.html2text(r.text).strip()
except:
print('No connection!')
sys.exit(0)
pass
if current_version < new_version:
gui = Gui()
try:
print('Downloading new version...')
if not os.path.isdir("update"):
os.makedirs("update")
def reporthook(count, data_size, total_data):
global start_time
if count == 0:
gui.pb.configure(maximum=total_data)
start_time = time.time()
return
else:
gui.pb.step(data_size)
duration = time.time() - start_time
progress_size = int(count * data_size)
speed = int(progress_size / (1024 * duration))
percent = int(count * data_size * 100 / total_data)
gui.lb2['text'] = "{0}% - {1} MB - {2} KB/s".format(percent, round(progress_size / (1024 * 1024), 1), speed)
source = 'https://github.com/alex3025/litemails/releases/download/{0}/LiteMails.zip'.format(new_version)
urllib.request.urlretrieve(source, 'update/LiteMails.zip', reporthook)
print('Installing new version...')
gui.lb['text'] = string2
gui.pb['mode'] = 'indeterminate'
gui.pb.configure(maximum=100)
gui.pb.start(30)
gui.lb2['text'] = ' '
# Creating init folders
if not os.path.isdir('bin'):
os.makedirs('bin')
if not os.path.isdir('backup'):
os.makedirs('backup/emails')
# Backup files
if os.path.isfile('bin/config.db'):
shutil.copy('bin/config.db', 'backup')
if os.path.isdir('bin/emails'):
for item in os.listdir('bin/emails'):
shutil.copy('bin/emails/' + item, 'backup/emails')
# Delete folders
shutil.rmtree('bin')
# Install version
unzipper = zipfile.ZipFile('update/LiteMails.zip', 'r')
if not os.path.isdir('bin'):
os.makedirs('bin/emails')
unzipper.extractall('bin')
unzipper.close()
# Move files
if os.path.isfile('backup/config.db'):
shutil.copy('backup/config.db', 'bin')
if os.path.isdir('backup/emails'):
for item in os.listdir('backup/emails'):
shutil.copy('backup/emails/' + item, 'bin/emails')
# Remove dirs
shutil.rmtree('update')
shutil.rmtree('backup')
# Finish
print('Installed!')
gui.download_window.destroy()
os.chdir('bin')
os.system('Lite Mails.exe')
gui.stop()
sys.exit(0)
except Exception as e:
print('Error when installing new version!')
print(e)
sys.exit(0)
else:
print('No updates found!')
sys.exit(0)
update()