Source

vpackager / src / vpackager / bot.py

Diff from to

File src/vpackager/bot.py

 import urllib
 import time
 import preferences
+import psutil
 
 __author__ = 'M0E-lnx'
 __author_email__ = 'moc.liamg@xnl.E0M'[::-1]
 
     def PlayOutput(self, data):
         gtk.gdk.threads_enter()
-        buf = self.textview.get_buffer()
-        buf.insert(buf.get_end_iter(), data)
-        # scroll the lines if necessary
-        vad = self.scrl.get_vadjustment()
-        newpos = vad.get_upper() - vad.get_page_size()
-        vad.set_value(newpos)
-        self.scrl.set_vadjustment(vad)
+        try:
+            buf = self.textview.get_buffer()
+            buf.insert(buf.get_end_iter(), data)
+            # scroll the lines if necessary
+            vad = self.scrl.get_vadjustment()
+            newpos = vad.get_upper() - vad.get_page_size()
+            vad.set_value(newpos)
+            self.scrl.set_vadjustment(vad)
+        except:
+            pass
         gtk.gdk.threads_leave()
 
     def _get_output_display(self):
         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
         self.dbase_model = dbase_model
         self.RUN_BOT = False
-        self.CheckDatabase()
         self.body = gtk.VBox()
         datadir = os.path.dirname(os.path.abspath(__file__))
         datadir = os.path.join(datadir, 'data')
         self.body.pack_start(self.tabstrip, True, True, 2)
         self.add(self.body)
         self.show_all()
-        self.connect('destroy', gtk.main_quit)
+        #self.connect('destroy', self._exit)
+        self.connect('delete-event', self._exit)
         self.set_size_request(700,400)
 
         # Connect the start/stop buttons
         self.dispatcher = Dispatcher(model = self.dbase_model,
                                      view = self.bot_tab)
 
+        self.CheckDatabase()
+
         # Force initial display of the queue and history
         self.dbase_model.RefreshQueue()
         self.dbase_model.RefreshHistory()
 
     def CheckDatabase(self):
         """ Run a sanity check and make sure the databse tables exist"""
+        self.dbase_model._cleanup_history()
         if self.dbase_model.verify_tables() is True:
             return self.dbase_model.CreateTables()
 
 
         return dia.destroy()
 
-    def _exit(self, widget=None):
-        self.bot_tab.RUN_BOT = False
-        time.sleep(0.25)
+    def _force_exit(self):
+        """ Force close the application """
+        parent = psutil.Process(os.getpid())
+        for child in parent.get_children():
+            child.kill()
+        parent.kill()
         self.dbase_model.close()
         return gtk.main_quit()
 
+
+    def _exit(self, widget=None, event=None):
+        if self.RUN_BOT:
+            # Warn the user about running tasks
+            msg = "The build bot is currently running. Are you sure you "\
+                  "want to quit anyway?"
+            dia = guitools.message.Question(text=msg, parent=self)
+            res = dia.run()
+            dia.hide()
+            if res == gtk.RESPONSE_YES:
+                # Force quit
+                return self._force_exit()
+            else:
+                return True
+        else:
+            self.RUN_BOT = False
+            time.sleep(0.25)
+            self.dbase_model.close()
+            return gtk.main_quit()
+
     def _show_about(self, widget=None):
         """Display the about window"""
         dia = guitools.About()