Commits

Moises Henriquez committed b988f00

Added database sanity check and properly closing db. dropped urllib for urllib2

Comments (0)

Files changed (2)

 import time
 import os
 import sqlite3
-import urllib
+import urllib2
 
 
 __author__ = 'M0E-lnx'
             if desc.startswith('/'):
                 shutil.copy2(desc, 'slack-desc')
             else:
-                urllib.urlretrieve(desc, 'slack-desc')
+                u = urllib2.urlopen(desc)
+                f = open('slack-desc', 'w')
+                f.write(u.read())
+                f.close()
+                u.close()
             job = srcob.buildSlackBuild()
             job.observer.add_callback(self.parent._play_output)
             job.run()
         self.connect('destroy', self._exit)
         self.set_size_request(950, 500)
         self.body = gtk.VBox()
+        # make sure the db is ok
+        self._check_db_sanity()
         self.outputplayer = None
         self.historyman = None
         self.queueman = None
         time.sleep(0.25)
         return gtk.main_quit()
 
+    def _check_db_sanity(self):
+        dbutils.create_vpackager_db()
+
 if __name__ == '__main__':
     gtk.gdk.threads_init()
     w = App()

vpackager/dbutils.py

 
 dbpath = '/tmp/vpackager.db'
 
+def __verify_tables():
+    ''' Verify that the essential tables for the vpackager environment
+    exist'''
+    db = sqlite3.connect(dbpath)
+    c = db.cursor()
+    c.execute('select name from sqlite_master where type="table"')
+    res = c.fetchall()
+    c.close()
+    db.close()
+    if len(res) > 0:
+        return False
+    return True
+
+def create_vpackager_db():
+    ''' Create the essential tables for the vpackager environment'''
+    if __verify_tables() is True:
+        __create_queue_table()
+        __create_history_table()
+        return
+    return
+
+
 def __create_queue_table():
     """ Create the job queue table. NEVER to be called from the program
     itself!"""
     install text)""")
     db.commit()
     c.close()
+    db.close()
 
     return
 
 
     db.commit()
     c.close()
+    db.close()
 
     return
 
 
     db.commit()
     c.close()
+    db.close()
 
 def RemoveJob(_id):
     """ Delete a job from the job queue"""
     c.execute('delete from queue where id=?', t)
     db.commit()
     c.close()
+    db.close()
 
 def LogJobStart(jobid):
     """ Move job from queue to history and add the timestamp to the buidl
     c.execute('delete from queue where id=?',t)
     c.close()
     db.commit()
+    db.close()
 
 def LogJobEnd(jobid, stdoutpath, result):
     """ Update the job history to log the end time and end result of the
     c.execute('''update history set result=? where id=?''',t)
     c.close()
     db.commit()
+    db.close()
 
 def GetNextJob():
     """ Find the next job in line to be built"""
     except StopIteration:
         ret = None
     c.close()
+    db.close()
     return ret
 
 def GetQueue():
     c = db.cursor()
     ret = c.execute('select * from queue').fetchall()
     c.close()
+    db.close()
     return ret
 
 def GetHistory():
     c = db.cursor()
     ret = c.execute('select * from history').fetchall()
     c.close()
+    db.close()
     return ret