X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=dak%2Finit_db.py;h=169f30057cb1d91ff94881057926f8dc4a0dbcb9;hb=01496fe710b21e3922ef7b6f3ffa74f2b697b34d;hp=928c9a046babd2bfef87f4aa28701227820b3ee4;hpb=7aaaad3135c9164390af5897925660842368660b;p=dak.git diff --git a/dak/init_db.py b/dak/init_db.py index 928c9a04..169f3005 100755 --- a/dak/init_db.py +++ b/dak/init_db.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Sync the ISC configuartion file and the SQL database +"""Sync dak.conf configuartion file and the SQL database""" # Copyright (C) 2000, 2001, 2002, 2003, 2006 James Troup # This program is free software; you can redistribute it and/or modify @@ -19,19 +19,18 @@ ################################################################################ -import pg, sys +import sys import apt_pkg -import dak.lib.database as database -import dak.lib.utils as utils -################################################################################ - -Cnf = None -projectB = None +from daklib import utils +from daklib.dbconn import * +from daklib.config import Config ################################################################################ def usage(exit_code=0): + """Print a usage message and exit with 'exit_code'.""" + print """Usage: dak init-db Initalizes some tables in the projectB database based on the config file. @@ -40,141 +39,210 @@ Initalizes some tables in the projectB database based on the config file. ################################################################################ -def get (c, i): - if c.has_key(i): - return "'%s'" % (c[i]) - else: - return "NULL" +class InitDB(object): + def __init__(self, Cnf, projectB): + self.Cnf = Cnf + self.projectB = projectB + + def do_archive(self): + """initalize the archive table.""" + + # Remove existing archives + s = self.projectB.session() + s.query(Archive).delete() + + for name in self.Cnf.SubTree("Archive").List(): + 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.""" + + # Remove existing architectures + s = self.projectB.session() + s.query(Architecture).delete() + + for arch in self.Cnf.SubTree("Architectures").List(): + 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.""" + + # Remove existing components + s = self.projectB.session() + s.query(Component).delete() + + for name in self.Cnf.SubTree("Component").List(): + 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) + + s.commit() + + def do_location(self): + """Initalize the location table.""" + + # Remove existing locations + s = self.projectB.session() + s.query(Location).delete() + + for location in self.Cnf.SubTree("Location").List(): + 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): + """Initialize the suite table.""" + + s = self.projectB.session() + s.query(Suite).delete() + + for suite in self.Cnf.SubTree("Suite").List(): + 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.""" + + s = self.projectB.session() + s.query(OverrideType).delete() + + for override_type in self.Cnf.ValueList("OverrideType"): + ot = OverrideType() + ot.overridetype = override_type + s.add(ot) + + s.commit() + + def do_priority(self): + """Initialize the priority table.""" + + s = self.projectB.session() + s.query(Priority).delete() + + for priority in self.Cnf.SubTree("Priority").List(): + p = Priority() + p.priority = priority + p.level = self.Cnf.get("Priority::" + priority, "0") + s.add(p) + + s.commit() + + def do_section(self): + """Initalize the section table.""" + + s = self.projectB.session() + s.query(Section).delete() + + for component in self.Cnf.SubTree("Component").List(): + if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix": + suffix = "" + if component != "main": + prefix = component + '/' + else: + prefix = "" + else: + prefix = "" + if component != "main": + suffix = '/' + component + else: + suffix = "" + + for section in self.Cnf.ValueList("Section"): + sec = Section() + sec.section = prefix + section + suffix + s.add(sec) + + s.commit() + + def do_all(self): + self.do_archive() + self.do_architecture() + self.do_component() + self.do_location() + self.do_suite() + self.do_override_type() + self.do_priority() + self.do_section() + +################################################################################ def main (): - global Cnf, projectB + """Sync dak.conf configuartion file and the SQL database""" Cnf = utils.get_conf() - Arguments = [('h',"help","Init-DB::Options::Help")] + arguments = [('h', "help", "Init-DB::Options::Help")] for i in [ "help" ]: - if not Cnf.has_key("Init-DB::Options::%s" % (i)): - Cnf["Init-DB::Options::%s" % (i)] = "" - - apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv) - - Options = Cnf.SubTree("Init-DB::Options") - if Options["Help"]: - usage() - - projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"])) - database.init(Cnf, projectB) - - # archive - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM archive") - for name in Cnf.SubTree("Archive").List(): - Archive = Cnf.SubTree("Archive::%s" % (name)) - origin_server = get(Archive, "OriginServer") - description = get(Archive, "Description") - projectB.query("INSERT INTO archive (name, origin_server, description) VALUES ('%s', %s, %s)" % (name, origin_server, description)) - projectB.query("COMMIT WORK") - - # architecture - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM architecture") - for arch in Cnf.SubTree("Architectures").List(): - description = Cnf["Architectures::%s" % (arch)] - projectB.query("INSERT INTO architecture (arch_string, description) VALUES ('%s', '%s')" % (arch, description)) - projectB.query("COMMIT WORK") - - # component - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM component") - for name in Cnf.SubTree("Component").List(): - Component = Cnf.SubTree("Component::%s" % (name)) - description = get(Component, "Description") - if Component.get("MeetsDFSG").lower() == "true": - meets_dfsg = "true" - else: - meets_dfsg = "false" - projectB.query("INSERT INTO component (name, description, meets_dfsg) VALUES ('%s', %s, %s)" % (name, description, meets_dfsg)) - projectB.query("COMMIT WORK") - - # location - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM location") - for location in Cnf.SubTree("Location").List(): - Location = Cnf.SubTree("Location::%s" % (location)) - archive_id = database.get_archive_id(Location["Archive"]) - type = Location.get("type") - if type == "legacy-mixed": - projectB.query("INSERT INTO location (path, archive, type) VALUES ('%s', %d, '%s')" % (location, archive_id, Location["type"])) - elif type == "legacy" or type == "pool": - for component in Cnf.SubTree("Component").List(): - component_id = database.get_component_id(component) - projectB.query("INSERT INTO location (path, component, archive, type) VALUES ('%s', %d, %d, '%s')" % - (location, component_id, archive_id, type)) - else: - utils.fubar("E: type '%s' not recognised in location %s." % (type, location)) - projectB.query("COMMIT WORK") - - # suite - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM suite") - for suite in Cnf.SubTree("Suite").List(): - Suite = Cnf.SubTree("Suite::%s" %(suite)) - version = get(Suite, "Version") - origin = get(Suite, "Origin") - description = get(Suite, "Description") - projectB.query("INSERT INTO suite (suite_name, version, origin, description) VALUES ('%s', %s, %s, %s)" - % (suite.lower(), version, origin, description)) - for architecture in Cnf.ValueList("Suite::%s::Architectures" % (suite)): - architecture_id = database.get_architecture_id (architecture) - if architecture_id < 0: - utils.fubar("architecture '%s' not found in architecture table for suite %s." % (architecture, suite)) - projectB.query("INSERT INTO suite_architectures (suite, architecture) VALUES (currval('suite_id_seq'), %d)" % (architecture_id)) - projectB.query("COMMIT WORK") - - # override_type - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM override_type") - for type in Cnf.ValueList("OverrideType"): - projectB.query("INSERT INTO override_type (type) VALUES ('%s')" % (type)) - projectB.query("COMMIT WORK") - - # priority - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM priority") - for priority in Cnf.SubTree("Priority").List(): - projectB.query("INSERT INTO priority (priority, level) VALUES ('%s', %s)" % (priority, Cnf["Priority::%s" % (priority)])) - projectB.query("COMMIT WORK") - - # section - - projectB.query("BEGIN WORK") - projectB.query("DELETE FROM section") - for component in Cnf.SubTree("Component").List(): - if Cnf["Control-Overrides::ComponentPosition"] == "prefix": - suffix = "" - if component != "main": - prefix = component + '/' - else: - prefix = "" - else: - prefix = "" - component = component.replace("non-US/", "") - if component != "main": - suffix = '/' + component - else: - suffix = "" - for section in Cnf.ValueList("Section"): - projectB.query("INSERT INTO section (section) VALUES ('%s%s%s')" % (prefix, section, suffix)) - projectB.query("COMMIT WORK") + if not Cnf.has_key("Init-DB::Options::%s" % (i)): + Cnf["Init-DB::Options::%s" % (i)] = "" + + arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv) + + options = Cnf.SubTree("Init-DB::Options") + if options["Help"]: + usage() + elif arguments: + utils.warn("dak init-db takes no arguments.") + usage(exit_code=1) + + # Just let connection failures be reported to the user + projectB = DBConn() + Cnf = Config() + + InitDB(Cnf, projectB).do_all() ################################################################################ if __name__ == '__main__': main() -