3 # Sync the ISC configuartion file and the SQL database
4 # Copyright (C) 2000, 2001, 2002, 2003, 2006 James Troup <james@nocrew.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ################################################################################
23 import dak.lib.utils, dak.lib.database
26 ################################################################################
31 ################################################################################
33 def usage(exit_code=0):
34 print """Usage: dak init-db
35 Initalizes some tables in the projectB database based on the config file.
37 -h, --help show this help and exit."""
40 ################################################################################
44 return "'%s'" % (c[i])
51 Cnf = dak.lib.utils.get_conf()
52 Arguments = [('h',"help","Init-DB::Options::Help")]
54 if not Cnf.has_key("Init-DB::Options::%s" % (i)):
55 Cnf["Init-DB::Options::%s" % (i)] = ""
57 apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
59 Options = Cnf.SubTree("Init-DB::Options")
63 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
64 dak.lib.database.init(Cnf, projectB)
68 projectB.query("BEGIN WORK")
69 projectB.query("DELETE FROM archive")
70 for name in Cnf.SubTree("Archive").List():
71 Archive = Cnf.SubTree("Archive::%s" % (name))
72 origin_server = get(Archive, "OriginServer")
73 description = get(Archive, "Description")
74 projectB.query("INSERT INTO archive (name, origin_server, description) VALUES ('%s', %s, %s)" % (name, origin_server, description))
75 projectB.query("COMMIT WORK")
79 projectB.query("BEGIN WORK")
80 projectB.query("DELETE FROM architecture")
81 for arch in Cnf.SubTree("Architectures").List():
82 description = Cnf["Architectures::%s" % (arch)]
83 projectB.query("INSERT INTO architecture (arch_string, description) VALUES ('%s', '%s')" % (arch, description))
84 projectB.query("COMMIT WORK")
88 projectB.query("BEGIN WORK")
89 projectB.query("DELETE FROM component")
90 for name in Cnf.SubTree("Component").List():
91 Component = Cnf.SubTree("Component::%s" % (name))
92 description = get(Component, "Description")
93 if Component.get("MeetsDFSG").lower() == "true":
97 projectB.query("INSERT INTO component (name, description, meets_dfsg) VALUES ('%s', %s, %s)" % (name, description, meets_dfsg))
98 projectB.query("COMMIT WORK")
102 projectB.query("BEGIN WORK")
103 projectB.query("DELETE FROM location")
104 for location in Cnf.SubTree("Location").List():
105 Location = Cnf.SubTree("Location::%s" % (location))
106 archive_id = dak.lib.database.get_archive_id(Location["Archive"])
107 type = Location.get("type")
108 if type == "legacy-mixed":
109 projectB.query("INSERT INTO location (path, archive, type) VALUES ('%s', %d, '%s')" % (location, archive_id, Location["type"]))
110 elif type == "legacy" or type == "pool":
111 for component in Cnf.SubTree("Component").List():
112 component_id = dak.lib.database.get_component_id(component)
113 projectB.query("INSERT INTO location (path, component, archive, type) VALUES ('%s', %d, %d, '%s')" %
114 (location, component_id, archive_id, type))
116 dak.lib.utils.fubar("E: type '%s' not recognised in location %s." % (type, location))
117 projectB.query("COMMIT WORK")
121 projectB.query("BEGIN WORK")
122 projectB.query("DELETE FROM suite")
123 for suite in Cnf.SubTree("Suite").List():
124 Suite = Cnf.SubTree("Suite::%s" %(suite))
125 version = get(Suite, "Version")
126 origin = get(Suite, "Origin")
127 description = get(Suite, "Description")
128 projectB.query("INSERT INTO suite (suite_name, version, origin, description) VALUES ('%s', %s, %s, %s)"
129 % (suite.lower(), version, origin, description))
130 for architecture in Cnf.ValueList("Suite::%s::Architectures" % (suite)):
131 architecture_id = dak.lib.database.get_architecture_id (architecture)
132 if architecture_id < 0:
133 dak.lib.utils.fubar("architecture '%s' not found in architecture table for suite %s." % (architecture, suite))
134 projectB.query("INSERT INTO suite_architectures (suite, architecture) VALUES (currval('suite_id_seq'), %d)" % (architecture_id))
135 projectB.query("COMMIT WORK")
139 projectB.query("BEGIN WORK")
140 projectB.query("DELETE FROM override_type")
141 for type in Cnf.ValueList("OverrideType"):
142 projectB.query("INSERT INTO override_type (type) VALUES ('%s')" % (type))
143 projectB.query("COMMIT WORK")
147 projectB.query("BEGIN WORK")
148 projectB.query("DELETE FROM priority")
149 for priority in Cnf.SubTree("Priority").List():
150 projectB.query("INSERT INTO priority (priority, level) VALUES ('%s', %s)" % (priority, Cnf["Priority::%s" % (priority)]))
151 projectB.query("COMMIT WORK")
155 projectB.query("BEGIN WORK")
156 projectB.query("DELETE FROM section")
157 for component in Cnf.SubTree("Component").List():
158 if Cnf["Control-Overrides::ComponentPosition"] == "prefix":
160 if component != "main":
161 prefix = component + '/'
166 component = component.replace("non-US/", "")
167 if component != "main":
168 suffix = '/' + component
171 for section in Cnf.ValueList("Section"):
172 projectB.query("INSERT INTO section (section) VALUES ('%s%s%s')" % (prefix, section, suffix))
173 projectB.query("COMMIT WORK")
175 ################################################################################
177 if __name__ == '__main__':