1 | #!/usr/bin/env python3 |
---|
2 | |
---|
3 | import gi |
---|
4 | gi.require_version('Gtk', '3.0') |
---|
5 | from gi.repository import Gtk, Pango, GdkPixbuf, Gdk, Gio, GObject,GLib |
---|
6 | |
---|
7 | |
---|
8 | import signal |
---|
9 | import os |
---|
10 | import subprocess |
---|
11 | import json |
---|
12 | import sys |
---|
13 | import syslog |
---|
14 | import time |
---|
15 | import threading |
---|
16 | import tempfile |
---|
17 | import pwd |
---|
18 | |
---|
19 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
20 | |
---|
21 | from . import settings |
---|
22 | import gettext |
---|
23 | gettext.textdomain(settings.TEXT_DOMAIN) |
---|
24 | _ = gettext.gettext |
---|
25 | |
---|
26 | class MainWindow: |
---|
27 | |
---|
28 | def __init__(self): |
---|
29 | |
---|
30 | self.core=Core.Core.get_core() |
---|
31 | |
---|
32 | #def init |
---|
33 | |
---|
34 | |
---|
35 | def load_gui(self): |
---|
36 | |
---|
37 | builder=Gtk.Builder() |
---|
38 | builder.set_translation_domain(settings.TEXT_DOMAIN) |
---|
39 | ui_path=self.core.ui_path |
---|
40 | builder.add_from_file(ui_path) |
---|
41 | |
---|
42 | |
---|
43 | self.css_file=self.core.rsrc_dir+"dpkgunlocker-gui.css" |
---|
44 | |
---|
45 | self.stack = Gtk.Stack() |
---|
46 | self.stack.set_transition_duration(10) |
---|
47 | self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT) |
---|
48 | |
---|
49 | self.main_window=builder.get_object("main_window") |
---|
50 | self.main_window.set_title("Dpkg-Unlocker") |
---|
51 | self.main_box=builder.get_object("main_box") |
---|
52 | self.main_window.resize(640,620) |
---|
53 | self.image_box=builder.get_object("image_box") |
---|
54 | self.help_button=builder.get_object("help_button") |
---|
55 | self.unlock_button=builder.get_object("unlock_button") |
---|
56 | |
---|
57 | self.process_box=self.core.processBox |
---|
58 | self.stack.add_titled(self.process_box,"processBox","ProcessBox") |
---|
59 | self.main_box.pack_start(self.stack,True,False,5) |
---|
60 | self.main_window.show_all() |
---|
61 | self.process_box.terminal_viewport.hide() |
---|
62 | |
---|
63 | self.final_column=0 |
---|
64 | self.final_row=0 |
---|
65 | |
---|
66 | self.set_css_info() |
---|
67 | self.init_threads() |
---|
68 | self.connect_signals() |
---|
69 | self.is_working=False |
---|
70 | self.load_info(True) |
---|
71 | GLib.timeout_add_seconds(5, self.is_worker) |
---|
72 | |
---|
73 | #def load_gui |
---|
74 | |
---|
75 | |
---|
76 | def set_css_info(self): |
---|
77 | |
---|
78 | self.style_provider=Gtk.CssProvider() |
---|
79 | f=Gio.File.new_for_path(self.css_file) |
---|
80 | self.style_provider.load_from_file(f) |
---|
81 | Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),self.style_provider,Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) |
---|
82 | self.main_window.set_name("WINDOW") |
---|
83 | self.image_box.set_name("IMAGE_BOX") |
---|
84 | self.process_box.terminal_label.set_name("MSG_LABEL") |
---|
85 | |
---|
86 | |
---|
87 | #def set_css_info |
---|
88 | |
---|
89 | |
---|
90 | def init_threads(self): |
---|
91 | |
---|
92 | self.open_help_t=threading.Thread(target=self.open_help) |
---|
93 | self.open_help_t.daemon=True |
---|
94 | |
---|
95 | GObject.threads_init() |
---|
96 | |
---|
97 | #def init_threads |
---|
98 | |
---|
99 | |
---|
100 | def connect_signals(self): |
---|
101 | |
---|
102 | self.main_window.connect("destroy",self.quit) |
---|
103 | self.help_button.connect("clicked",self.help_clicked) |
---|
104 | self.unlock_button.connect("clicked",self.unlock_process) |
---|
105 | |
---|
106 | #def connect_signals |
---|
107 | |
---|
108 | def is_worker(self): |
---|
109 | |
---|
110 | if not self.is_working: |
---|
111 | self.load_info(False) |
---|
112 | |
---|
113 | return True |
---|
114 | |
---|
115 | #def is_worker |
---|
116 | |
---|
117 | def load_info(self,log): |
---|
118 | |
---|
119 | info=self.core.unlockerManager.checkingLocks() |
---|
120 | self.manage_unlock_button(info) |
---|
121 | self.process_box.load_info(info,log) |
---|
122 | |
---|
123 | #def load_info |
---|
124 | |
---|
125 | |
---|
126 | def manage_unlock_button(self,info): |
---|
127 | |
---|
128 | cont=0 |
---|
129 | ok_status=[0,1,3] |
---|
130 | running_status=[1,3] |
---|
131 | liveProcess=0 |
---|
132 | |
---|
133 | for item in info: |
---|
134 | if info[item] in ok_status: |
---|
135 | cont+=1 |
---|
136 | if info [item] in running_status: |
---|
137 | liveProcess+=1 |
---|
138 | |
---|
139 | if cont==3: |
---|
140 | self.unlock_button.set_sensitive(False) |
---|
141 | self.process_box.terminal_label.set_text(self.get_msg_text(0)) |
---|
142 | |
---|
143 | else: |
---|
144 | if cont==0: |
---|
145 | self.unlock_button.set_sensitive(True) |
---|
146 | else: |
---|
147 | if liveProcess>0: |
---|
148 | self.unlock_button.set_sensitive(False) |
---|
149 | self.process_box.terminal_label.set_text(self.get_msg_text(11)) |
---|
150 | else: |
---|
151 | self.unlock_button.set_sensitive(True) |
---|
152 | self.process_box.terminal_label.set_text(self.get_msg_text(12)) |
---|
153 | |
---|
154 | #def manage_unlock_button |
---|
155 | |
---|
156 | def init_unlocker_processes(self): |
---|
157 | |
---|
158 | self.remove_llxup_lock_launched=False |
---|
159 | self.remove_llxup_lock_done=False |
---|
160 | |
---|
161 | self.remove_dpkg_lock_launched=False |
---|
162 | self.remove_dpkg_lock_done=False |
---|
163 | |
---|
164 | self.remove_apt_lock_launched=False |
---|
165 | self.remove_apt_lock_done=False |
---|
166 | |
---|
167 | self.fixing_system_launched=False |
---|
168 | self.fixing_system_done=False |
---|
169 | |
---|
170 | #def init_unlocker_processes |
---|
171 | |
---|
172 | def create_process_token(self,command,action): |
---|
173 | |
---|
174 | |
---|
175 | if action=="Lliurex-Up": |
---|
176 | self.token_llxup_process=tempfile.mkstemp('_LlxUp') |
---|
177 | remove_tmp=' rm -f ' + self.token_llxup_process[1] + ';'+'\n' |
---|
178 | |
---|
179 | elif action=="Dpkg": |
---|
180 | self.token_dpkg_process=tempfile.mkstemp('_Dpkg') |
---|
181 | remove_tmp=' rm -f ' + self.token_dpkg_process[1] + ';'+'\n' |
---|
182 | |
---|
183 | elif action=="Apt": |
---|
184 | self.token_apt_process=tempfile.mkstemp('_Apt') |
---|
185 | remove_tmp=' rm -f ' + self.token_apt_process[1] + ';'+'\n' |
---|
186 | |
---|
187 | elif action=="Fixing": |
---|
188 | self.token_fixing_process=tempfile.mkstemp('_Fixing') |
---|
189 | remove_tmp=' rm -f ' + self.token_fixing_process[1] + ';'+'\n' |
---|
190 | |
---|
191 | cmd=command+remove_tmp |
---|
192 | return cmd |
---|
193 | |
---|
194 | #def create_process_token |
---|
195 | |
---|
196 | def create_result_token(self,command,action): |
---|
197 | |
---|
198 | |
---|
199 | if action=="Lliurex-Up": |
---|
200 | self.token_llxup_result=tempfile.mkstemp('_LlxUp') |
---|
201 | result_tmp=' echo $? >' + self.token_llxup_result[1]+ ';' |
---|
202 | |
---|
203 | elif action=="Dpkg": |
---|
204 | self.token_dpkg_result=tempfile.mkstemp('_Dpkg') |
---|
205 | result_tmp=' echo $? > ' + self.token_dpkg_result[1] + ';' |
---|
206 | |
---|
207 | elif action=="Apt": |
---|
208 | self.token_apt_result=tempfile.mkstemp('_Apt') |
---|
209 | result_tmp=' echo $? > ' + self.token_apt_result[1] + ';' |
---|
210 | |
---|
211 | elif action=="Fixing": |
---|
212 | self.token_fixing_result=tempfile.mkstemp('_Fixing') |
---|
213 | result_tmp=' echo $? > ' + self.token_fixing_result[1] + ';' |
---|
214 | |
---|
215 | cmd=command+';'+result_tmp |
---|
216 | return cmd |
---|
217 | |
---|
218 | #def create_result_token |
---|
219 | |
---|
220 | |
---|
221 | def unlock_process(self,wigdet): |
---|
222 | |
---|
223 | self.is_working=True |
---|
224 | dialog = Gtk.MessageDialog(None,0,Gtk.MessageType.WARNING, Gtk.ButtonsType.YES_NO, "Dpkg-Unlocker") |
---|
225 | msg=self.get_msg_text(10) |
---|
226 | dialog.format_secondary_text(msg) |
---|
227 | response=dialog.run() |
---|
228 | dialog.destroy() |
---|
229 | |
---|
230 | if response==Gtk.ResponseType.YES: |
---|
231 | self.core.processBox.manage_vterminal(True,False) |
---|
232 | self.unlock_button.set_sensitive(False) |
---|
233 | self.process_box.terminal_viewport.show() |
---|
234 | self.unlockInfo=self.core.unlockerManager.getUnlockerCommand() |
---|
235 | self.unlock_command,self.fixing_command,liveProcess=self.core.unlockerManager.getUnlockerCommand() |
---|
236 | #self.write_log("Dpkg-Unlocked-Gui") |
---|
237 | self.init_unlocker_processes() |
---|
238 | |
---|
239 | GLib.timeout_add(100,self.pulsate_unlock_process) |
---|
240 | |
---|
241 | else: |
---|
242 | self.is_working=False |
---|
243 | |
---|
244 | #def unlock_process |
---|
245 | |
---|
246 | |
---|
247 | def pulsate_unlock_process(self): |
---|
248 | |
---|
249 | error=False |
---|
250 | |
---|
251 | if not self.remove_llxup_lock_launched: |
---|
252 | if "Lliurex-Up" in self.unlockInfo["unlockCmd"]: |
---|
253 | self.process_box.terminal_label.show() |
---|
254 | msg=self.get_msg_text(1) |
---|
255 | self.write_log(msg) |
---|
256 | self.process_box.terminal_label.set_text(msg) |
---|
257 | self.remove_llxup_lock_launched=True |
---|
258 | self.llxup_lock_check=True |
---|
259 | self.exec_command("Lliurex-Up","remove") |
---|
260 | |
---|
261 | else: |
---|
262 | self.remove_llxup_lock_done=True |
---|
263 | self.llxup_lock_check=False |
---|
264 | self.llxup_result=True |
---|
265 | |
---|
266 | if self.remove_llxup_lock_done: |
---|
267 | if self.llxup_lock_check: |
---|
268 | self.llxup_result=self.check_process("Lliurex-Up") |
---|
269 | |
---|
270 | if self.llxup_result: |
---|
271 | |
---|
272 | if not self.remove_dpkg_lock_launched: |
---|
273 | if "Dpkg" in self.unlockInfo["unlockCmd"]: |
---|
274 | msg=self.get_msg_text(2) |
---|
275 | self.write_log(msg) |
---|
276 | self.process_box.terminal_label.set_text(msg) |
---|
277 | self.remove_dpkg_lock_launched=True |
---|
278 | self.dpkg_lock_check=True |
---|
279 | self.exec_command("Dpkg","remove") |
---|
280 | else: |
---|
281 | self.remove_dpkg_lock_done=True |
---|
282 | self.dpkg_lock_check=False |
---|
283 | self.dpkg_result=True |
---|
284 | |
---|
285 | if self.remove_dpkg_lock_done: |
---|
286 | if self.dpkg_lock_check: |
---|
287 | self.dpkg_result=self.check_process("Dpkg") |
---|
288 | |
---|
289 | if self.dpkg_result: |
---|
290 | |
---|
291 | if not self.remove_apt_lock_launched: |
---|
292 | if "Apt" in self.unlockInfo["unlockCmd"]: |
---|
293 | msg=self.get_msg_text(3) |
---|
294 | self.write_log(msg) |
---|
295 | self.process_box.terminal_label.set_text(msg) |
---|
296 | self.remove_apt_lock_launched=True |
---|
297 | self.apt_lock_check=True |
---|
298 | self.exec_command("Apt","remove") |
---|
299 | else: |
---|
300 | self.remove_apt_lock_done=True |
---|
301 | self.apt_lock_check=False |
---|
302 | self.apt_result=True |
---|
303 | |
---|
304 | if self.remove_apt_lock_done: |
---|
305 | if self.apt_lock_check: |
---|
306 | self.apt_result=self.check_process("Apt") |
---|
307 | |
---|
308 | if self.apt_result: |
---|
309 | |
---|
310 | if not self.fixing_system_launched: |
---|
311 | if self.unlockInfo["commonCmd"]!="": |
---|
312 | self.process_box.terminal_label.set_text(self.get_msg_text(4)) |
---|
313 | self.fixing_system_launched=True |
---|
314 | self.fixig_lock_check=True |
---|
315 | self.exec_command("Fixing","fixing") |
---|
316 | else: |
---|
317 | self.fixing_system_done=True |
---|
318 | self.fixig_lock_check=False |
---|
319 | self.fixing_result=True |
---|
320 | |
---|
321 | if self.fixing_system_done: |
---|
322 | if self.fixig_lock_check: |
---|
323 | self.fixing_result=self.check_process("Fixing") |
---|
324 | |
---|
325 | self.core.processBox.manage_vterminal(False,True) |
---|
326 | if self.fixing_result: |
---|
327 | msg=self.get_msg_text(5) |
---|
328 | self.write_log_terminal() |
---|
329 | self.write_log(msg) |
---|
330 | self.load_info(False) |
---|
331 | self.process_box.terminal_label.set_text(msg) |
---|
332 | return False |
---|
333 | else: |
---|
334 | error=True |
---|
335 | self.write_log_terminal() |
---|
336 | code=6 |
---|
337 | |
---|
338 | |
---|
339 | else: |
---|
340 | error=True |
---|
341 | code=7 |
---|
342 | else: |
---|
343 | error=True |
---|
344 | code=8 |
---|
345 | |
---|
346 | else: |
---|
347 | error=True |
---|
348 | code=9 |
---|
349 | |
---|
350 | |
---|
351 | if error: |
---|
352 | self.core.processBox.manage_vterminal(False,True) |
---|
353 | msg_error=self.get_msg_text(code) |
---|
354 | self.process_box.terminal_label.set_name("MSG_ERROR_LABEL") |
---|
355 | self.process_box.terminal_label.set_text(msg_error) |
---|
356 | self.write_log(msg_error) |
---|
357 | return False |
---|
358 | |
---|
359 | |
---|
360 | if self.remove_llxup_lock_launched: |
---|
361 | if not self.remove_llxup_lock_done: |
---|
362 | if os.path.exists(self.token_llxup_process[1]): |
---|
363 | return True |
---|
364 | else: |
---|
365 | self.remove_llxup_lock_done=True |
---|
366 | return True |
---|
367 | |
---|
368 | if self.remove_dpkg_lock_launched: |
---|
369 | if not self.remove_dpkg_lock_done: |
---|
370 | if os.path.exists(self.token_dpkg_process[1]): |
---|
371 | return True |
---|
372 | else: |
---|
373 | self.remove_dpkg_lock_done=True |
---|
374 | return True |
---|
375 | |
---|
376 | if self.remove_apt_lock_launched: |
---|
377 | if not self.remove_apt_lock_done: |
---|
378 | if os.path.exists(self.token_apt_process[1]): |
---|
379 | return True |
---|
380 | else: |
---|
381 | self.remove_apt_lock_done=True |
---|
382 | return True |
---|
383 | |
---|
384 | if self.fixing_system_launched: |
---|
385 | if not self.fixing_system_done: |
---|
386 | if os.path.exists(self.token_fixing_process[1]): |
---|
387 | return True |
---|
388 | else: |
---|
389 | self.fixing_system_done=True |
---|
390 | return True |
---|
391 | |
---|
392 | #def pulsate_unlock_process |
---|
393 | |
---|
394 | def exec_command(self,action,type_cmd): |
---|
395 | |
---|
396 | if type_cmd=="remove": |
---|
397 | command=self.unlockInfo["unlockCmd"][action] |
---|
398 | else: |
---|
399 | command=self.unlockInfo["commonCmd"] |
---|
400 | |
---|
401 | length=len(command) |
---|
402 | |
---|
403 | if length>0: |
---|
404 | command=self.create_result_token(command,action) |
---|
405 | command=self.create_process_token(command,action) |
---|
406 | length=len(command) |
---|
407 | self.process_box.vterminal.feed_child(command,length) |
---|
408 | else: |
---|
409 | if action=="Lliurex-Up": |
---|
410 | self.remove_llxup_lock_done=True |
---|
411 | elif action=="Dpkg": |
---|
412 | self.remove_dpkg_lock_done=True |
---|
413 | elif action=="Apt": |
---|
414 | self.remove_ap_lock_done=True |
---|
415 | elif action=="Fixing": |
---|
416 | self.fixing_system_done=True |
---|
417 | |
---|
418 | #def exec_command |
---|
419 | |
---|
420 | |
---|
421 | def check_process(self,action): |
---|
422 | |
---|
423 | result=True |
---|
424 | |
---|
425 | if action=="Lliurex-Up": |
---|
426 | token=self.token_llxup_result[1] |
---|
427 | elif action=="Dpkg": |
---|
428 | token=self.token_dpkg_result[1] |
---|
429 | elif action=="Apt": |
---|
430 | token=self.token_apt_result[1] |
---|
431 | elif action=="Fixing": |
---|
432 | token=self.token_fixing_result[1] |
---|
433 | |
---|
434 | |
---|
435 | if os.path.exists(token): |
---|
436 | file=open(token) |
---|
437 | content=file.readline() |
---|
438 | if '0' not in content: |
---|
439 | result=False |
---|
440 | file.close() |
---|
441 | os.remove(token) |
---|
442 | |
---|
443 | return result |
---|
444 | |
---|
445 | #def check_process |
---|
446 | |
---|
447 | def get_msg_text(self,code): |
---|
448 | |
---|
449 | if code==0: |
---|
450 | msg=_("All processes seem correct. Nothing to do") |
---|
451 | elif code==1: |
---|
452 | msg=_("Removing Lliurex-Up lock file...") |
---|
453 | elif code==2: |
---|
454 | msg=_("Removing Dpkg lock file...") |
---|
455 | elif code==3: |
---|
456 | msg=_("Removing Apt lock file...") |
---|
457 | elif code==4: |
---|
458 | msg=_("Fixing the system...") |
---|
459 | elif code==5: |
---|
460 | msg=_("Unlocking process finished successfully") |
---|
461 | elif code==6: |
---|
462 | msg=_("Error fixing the system") |
---|
463 | elif code==7: |
---|
464 | msg=_("Error removing Apt lock file") |
---|
465 | elif code==8: |
---|
466 | msg=_("Error removing Dpg lock file") |
---|
467 | elif code==9: |
---|
468 | msg=_("Error removing Lliurex-Up lock file") |
---|
469 | elif code==10: |
---|
470 | msg=_("Do you want to execute the unlocking process?") |
---|
471 | elif code==11: |
---|
472 | msg=_("Some process are running. Wait a moment") |
---|
473 | elif code==12: |
---|
474 | msg=_("Detected some blocked process") |
---|
475 | return msg |
---|
476 | |
---|
477 | #def get_msg_text |
---|
478 | |
---|
479 | |
---|
480 | def quit(self,widget): |
---|
481 | |
---|
482 | msg_log='Quit' |
---|
483 | self.core.unlockerManager.cleanLockToken() |
---|
484 | self.write_log(msg_log) |
---|
485 | Gtk.main_quit() |
---|
486 | |
---|
487 | #def quit |
---|
488 | |
---|
489 | def write_log_terminal(self): |
---|
490 | |
---|
491 | init_row=self.final_row |
---|
492 | init_column=self.final_column |
---|
493 | |
---|
494 | self.final_column,self.final_row = self.process_box.vterminal.get_cursor_position() |
---|
495 | log_text=self.process_box.vterminal.get_text_range(init_row,init_column,self.final_row,self.final_column)[0] |
---|
496 | |
---|
497 | log_text=log_text.split("\n") |
---|
498 | |
---|
499 | syslog.openlog("DpkgUnlocker") |
---|
500 | syslog.syslog("Fixing the system") |
---|
501 | |
---|
502 | |
---|
503 | for item in log_text: |
---|
504 | if item!="": |
---|
505 | self.write_log(item) |
---|
506 | |
---|
507 | return |
---|
508 | |
---|
509 | #def write_log_terminal |
---|
510 | |
---|
511 | def write_log(self,msg): |
---|
512 | |
---|
513 | syslog.openlog("DpkgUnlocker") |
---|
514 | syslog.syslog(msg) |
---|
515 | |
---|
516 | #def write_log |
---|
517 | |
---|
518 | def help_clicked(self,widget): |
---|
519 | |
---|
520 | lang=os.environ["LANG"] |
---|
521 | run_pkexec=False |
---|
522 | |
---|
523 | if "PKEXEC_UID" in os.environ: |
---|
524 | run_pkexec=True |
---|
525 | |
---|
526 | if 'ca_ES' in lang: |
---|
527 | cmd='xdg-open http://wiki.lliurex.net/tiki-index.php?page=Dpkg-Unlocker_va' |
---|
528 | else: |
---|
529 | cmd='xdg-open http://wiki.lliurex.net/tiki-index.php?page=Dpkg-Unlocker' |
---|
530 | |
---|
531 | if not run_pkexec: |
---|
532 | self.fcmd="su -c '%s' $USER" %cmd |
---|
533 | else: |
---|
534 | user=pwd.getpwuid(int(os.environ["PKEXEC_UID"])).pw_name |
---|
535 | self.fcmd=fcmd="su -c '" +cmd+ "' "+ user |
---|
536 | |
---|
537 | self.init_threads() |
---|
538 | self.open_help_t.start() |
---|
539 | |
---|
540 | #help_clicked |
---|
541 | |
---|
542 | |
---|
543 | def open_help(self): |
---|
544 | |
---|
545 | os.system(self.fcmd) |
---|
546 | |
---|
547 | #def open_help |
---|
548 | |
---|
549 | |
---|
550 | def start_gui(self): |
---|
551 | |
---|
552 | Gtk.main() |
---|
553 | |
---|
554 | #def start_gui |
---|
555 | |
---|
556 | |
---|
557 | |
---|
558 | #class MainWindow |
---|
559 | from . import Core |
---|