]> git.decadent.org.uk Git - dak.git/blobdiff - dak/init_db.py
Actualy close bugs associated to removed packaged on request
[dak.git] / dak / init_db.py
index e15d7680799ab0dc0d69e24ebf5743cc249f3ba4..169f30057cb1d91ff94881057926f8dc4a0dbcb9 100755 (executable)
 
 ################################################################################
 
-import psycopg2, sys
+import sys
 import apt_pkg
 
 from daklib import utils
-from daklib.dbconn import DBConn
+from daklib.dbconn import *
 from daklib.config import Config
 
 ################################################################################
@@ -39,16 +39,6 @@ Initalizes some tables in the projectB database based on the config file.
 
 ################################################################################
 
-def sql_get (config, key):
-    """Return the value of config[key] or None if it doesn't exist."""
-
-    try:
-        return config[key]
-    except KeyError:
-        return None
-
-################################################################################
-
 class InitDB(object):
     def __init__(self, Cnf, projectB):
         self.Cnf = Cnf
@@ -57,131 +47,143 @@ class InitDB(object):
     def do_archive(self):
         """initalize the archive table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM archive")
-        archive_add = "INSERT INTO archive (name, origin_server, description) VALUES (%s, %s, %s)"
+        # Remove existing archives
+        s = self.projectB.session()
+        s.query(Archive).delete()
+
         for name in self.Cnf.SubTree("Archive").List():
-            archive_config = self.Cnf.SubTree("Archive::%s" % (name))
-            origin_server = sql_get(archive_config, "OriginServer")
-            description = sql_get(archive_config, "Description")
-            c.execute(archive_add, [name, origin_server, description])
-        self.projectB.commit()
+            a = Archive()
+            a.archive_name  = name
+            a.origin_server = self.Cnf.get("Archive::%s::OriginServer" % name, "")
+            a.description   = self.Cnf.get("Archive::%s::Description" % name,  "")
+            s.add(a)
+
+        s.commit()
 
     def do_architecture(self):
         """Initalize the architecture table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM architecture")
-        arch_add = "INSERT INTO architecture (arch_string, description) VALUES (%s, %s)"
+        # Remove existing architectures
+        s = self.projectB.session()
+        s.query(Architecture).delete()
+
         for arch in self.Cnf.SubTree("Architectures").List():
-            description = self.Cnf["Architectures::%s" % (arch)]
-            c.execute(arch_add, [arch, description])
-        self.projectB.commit()
+            a = Architecture()
+            a.arch_string  = arch
+            a.description  = self.Cnf.get("Architecture::%s" % arch, "")
+            s.add(a)
+
+        s.commit()
 
     def do_component(self):
         """Initalize the component table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM component")
-
-        comp_add = "INSERT INTO component (name, description, meets_dfsg) " + \
-                   "VALUES (%s, %s, %s)"
+        # Remove existing components
+        s = self.projectB.session()
+        s.query(Component).delete()
 
         for name in self.Cnf.SubTree("Component").List():
-            component_config = self.Cnf.SubTree("Component::%s" % (name))
-            description = sql_get(component_config, "Description")
-            meets_dfsg = (component_config.get("MeetsDFSG").lower() == "true")
-            c.execute(comp_add, [name, description, meets_dfsg])
+            c = Component()
+            c.component_name = name
+            c.description = self.Cnf.get("Component::%s::Description" % name, "")
+            c.meets_dfsg  = False
+            if self.Cnf.get("Component::%s::MeetsDFSG" % name, "false").lower() == 'true':
+                c.meets_dfsg = True
+            s.add(c)
 
-        self.projectB.commit()
+        s.commit()
 
     def do_location(self):
         """Initalize the location table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM location")
-
-        loc_add = "INSERT INTO location (path, component, archive, type) " + \
-                  "VALUES (%s, %s, %s, %s)"
+        # Remove existing locations
+        s = self.projectB.session()
+        s.query(Location).delete()
 
         for location in self.Cnf.SubTree("Location").List():
-            location_config = self.Cnf.SubTree("Location::%s" % (location))
-            archive_id = self.projectB.get_archive_id(location_config["Archive"])
-            if archive_id == -1:
-                utils.fubar("Archive '%s' for location '%s' not found."
-                                   % (location_config["Archive"], location))
-            location_type = location_config.get("type")
-            if location_type == "pool":
-                for component in self.Cnf.SubTree("Component").List():
-                    component_id = self.projectB.get_component_id(component)
-                    c.execute(loc_add, [location, component_id, archive_id, location_type])
-            else:
-                utils.fubar("E: type '%s' not recognised in location %s."
-                                   % (location_type, location))
-
-        self.projectB.commit()
+            archive_name = self.Cnf.get("Location::%s::Archive" % location, "")
+            a = s.query(Archive).filter_by(archive_name=archive_name)
+            if a.count() < 1:
+                utils.fubar("E: Archive '%s' for location '%s' not found" % (archive_name, location))
+            archive_id = a.one().archive_id
+
+            location_type = self.Cnf.get("Location::%s::Type" % location, "")
+            if location_type != 'pool':
+                utils.fubar("E: type %s not recognised for location %s" % (location_type, location))
+
+            for component in self.Cnf.SubTree("Component").List():
+                c = s.query(Component).filter_by(component_name=component)
+                if c.count() < 1:
+                    utils.fubar("E: Can't find component %s for location %s" % (component, location))
+                component_id = c.one().component_id
+
+                l = Location()
+                l.path = location
+                l.archive_id = archive_id
+                l.component_id = component_id
+                l.archive_type = location_type
+                s.add(l)
+
+        s.commit()
 
     def do_suite(self):
-        """Initalize the suite table."""
-
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM suite")
-
-        suite_add = "INSERT INTO suite (suite_name, version, origin, description) " + \
-                    "VALUES (%s, %s, %s, %s)"
+        """Initialize the suite table."""
 
-        sa_add = "INSERT INTO suite_architectures (suite, architecture) " + \
-                 "VALUES (currval('suite_id_seq'), %s)"
+        s = self.projectB.session()
+        s.query(Suite).delete()
 
         for suite in self.Cnf.SubTree("Suite").List():
-            suite_config = self.Cnf.SubTree("Suite::%s" %(suite))
-            version = sql_get(suite_config, "Version")
-            origin = sql_get(suite_config, "Origin")
-            description = sql_get(suite_config, "Description")
-            c.execute(suite_add, [suite.lower(), version, origin, description])
-            for architecture in self.Cnf.SubTree("Architectures").List():
-                architecture_id = self.projectB.get_architecture_id (architecture)
-                if architecture_id < 0:
-                    utils.fubar("architecture '%s' not found in architecture"
-                                " table for suite %s."
-                                % (architecture, suite))
-                c.execute(sa_add, [architecture_id])
-
-        self.projectB.commit()
+            suite = suite.lower()
+            su = Suite()
+            su.suite_name  = suite
+            su.version     = self.Cnf.get("Suite::%s::Version" % suite, "-")
+            su.origin      = self.Cnf.get("Suite::%s::Origin" % suite, "")
+            su.description = self.Cnf.get("Suite::%s::Description" % suite, "")
+            s.add(su)
+
+            for architecture in self.Cnf.ValueList("Suite::%s::Architectures" % (suite)):
+                sa = SuiteArchitecture()
+                a = s.query(Architecture).filter_by(arch_string=architecture)
+                if a.count() < 1:
+                    utils.fubar("E: Architecture %s not found for suite %s" % (architecture, suite))
+                sa.arch_id = a.one().arch_id
+                sa.suite_id = su.suite_id
+                s.add(sa)
+
+        s.commit()
 
     def do_override_type(self):
         """Initalize the override_type table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM override_type")
-
-        over_add = "INSERT INTO override_type (type) VALUES (%s)"
+        s = self.projectB.session()
+        s.query(OverrideType).delete()
 
         for override_type in self.Cnf.ValueList("OverrideType"):
-            c.execute(over_add, [override_type])
+            ot = OverrideType()
+            ot.overridetype = override_type
+            s.add(ot)
 
-        self.projectB.commit()
+        s.commit()
 
     def do_priority(self):
         """Initialize the priority table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM priority")
-
-        prio_add = "INSERT INTO priority (priority, level) VALUES (%s, %s)"
+        s = self.projectB.session()
+        s.query(Priority).delete()
 
         for priority in self.Cnf.SubTree("Priority").List():
-            c.execute(prio_add, [priority, self.Cnf["Priority::%s" % (priority)]])
+            p = Priority()
+            p.priority = priority
+            p.level    = self.Cnf.get("Priority::" + priority, "0")
+            s.add(p)
 
-        self.projectB.commit()
+        s.commit()
 
     def do_section(self):
         """Initalize the section table."""
 
-        c = self.projectB.cursor()
-        c.execute("DELETE FROM section")
-
-        sect_add = "INSERT INTO section (section) VALUES (%s)"
+        s = self.projectB.session()
+        s.query(Section).delete()
 
         for component in self.Cnf.SubTree("Component").List():
             if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
@@ -196,10 +198,13 @@ class InitDB(object):
                     suffix = '/' + component
                 else:
                     suffix = ""
+
             for section in self.Cnf.ValueList("Section"):
-                c.execute(sect_add, [prefix + section + suffix])
+                sec = Section()
+                sec.section = prefix + section + suffix
+                s.add(sec)
 
-        self.projectB.commit()
+        s.commit()
 
     def do_all(self):
         self.do_archive()