3 """Sync dak.conf 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 ################################################################################
25 from daklib import utils
26 from daklib.DBConn import DBConn
27 from daklib.Config import Config
29 ################################################################################
31 def usage(exit_code=0):
32 """Print a usage message and exit with 'exit_code'."""
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 ################################################################################
42 def sql_get (config, key):
43 """Return the value of config[key] or None if it doesn't exist."""
50 ################################################################################
53 def __init__(self, Cnf, projectB):
55 self.projectB = projectB
58 """Initalize the archive table."""
60 c = self.projectB.cursor()
61 c.execute("DELETE FROM archive")
62 archive_add = "INSERT INTO archive (name, origin_server, description) VALUES (%s, %s, %s)"
63 for name in self.Cnf.SubTree("Archive").List():
64 archive_config = self.Cnf.SubTree("Archive::%s" % (name))
65 origin_server = sql_get(archive_config, "OriginServer")
66 description = sql_get(archive_config, "Description")
67 c.execute(archive_add, [name, origin_server, description])
68 self.projectB.commit()
70 def do_architecture(self):
71 """Initalize the architecture table."""
73 c = self.projectB.cursor()
74 c.execute("DELETE FROM architecture")
75 arch_add = "INSERT INTO architecture (arch_string, description) VALUES (%s, %s)"
76 for arch in self.Cnf.SubTree("Architectures").List():
77 description = self.Cnf["Architectures::%s" % (arch)]
78 c.execute(arch_add, [arch, description])
79 self.projectB.commit()
81 def do_component(self):
82 """Initalize the component table."""
84 c = self.projectB.cursor()
85 c.execute("DELETE FROM component")
87 comp_add = "INSERT INTO component (name, description, meets_dfsg) " + \
90 for name in self.Cnf.SubTree("Component").List():
91 component_config = self.Cnf.SubTree("Component::%s" % (name))
92 description = sql_get(component_config, "Description")
93 meets_dfsg = (component_config.get("MeetsDFSG").lower() == "true")
94 c.execute(comp_add, [name, description, meets_dfsg])
96 self.projectB.commit()
98 def do_location(self):
99 """Initalize the location table."""
101 c = self.projectB.cursor()
102 c.execute("DELETE FROM location")
104 loc_add_mixed = "INSERT INTO location (path, archive, type) " + \
105 "VALUES (%s, %s, %s)"
107 loc_add = "INSERT INTO location (path, component, archive, type) " + \
108 "VALUES (%s, %s, %s, %s)"
110 for location in self.Cnf.SubTree("Location").List():
111 location_config = self.Cnf.SubTree("Location::%s" % (location))
112 archive_id = self.projectB.get_archive_id(location_config["Archive"])
114 utils.fubar("Archive '%s' for location '%s' not found."
115 % (location_config["Archive"], location))
116 location_type = location_config.get("type")
117 if location_type == "legacy-mixed":
118 c.execute(loc_add_mixed, [location, archive_id, location_config["type"]])
119 elif location_type == "legacy" or location_type == "pool":
120 for component in self.Cnf.SubTree("Component").List():
121 component_id = self.projectB.get_component_id(component)
122 c.execute(loc_add, [location, component_id, archive_id, location_type])
124 utils.fubar("E: type '%s' not recognised in location %s."
125 % (location_type, location))
127 self.projectB.commit()
130 """Initalize the suite table."""
132 c = self.projectB.cursor()
133 c.execute("DELETE FROM suite")
135 suite_add = "INSERT INTO suite (suite_name, version, origin, description) " + \
136 "VALUES (%s, %s, %s, %s)"
138 sa_add = "INSERT INTO suite_architectures (suite, architecture) " + \
139 "VALUES (currval('suite_id_seq'), %s)"
141 for suite in self.Cnf.SubTree("Suite").List():
142 suite_config = self.Cnf.SubTree("Suite::%s" %(suite))
143 version = sql_get(suite_config, "Version")
144 origin = sql_get(suite_config, "Origin")
145 description = sql_get(suite_config, "Description")
146 c.execute(suite_add, [suite.lower(), version, origin, description])
147 for architecture in self.Cnf.ValueList("Suite::%s::Architectures" % (suite)):
148 architecture_id = self.projectB.get_architecture_id (architecture)
149 if architecture_id < 0:
150 utils.fubar("architecture '%s' not found in architecture"
151 " table for suite %s."
152 % (architecture, suite))
153 c.execute(sa_add, [architecture_id])
155 self.projectB.commit()
157 def do_override_type(self):
158 """Initalize the override_type table."""
160 c = self.projectB.cursor()
161 c.execute("DELETE FROM override_type")
163 over_add = "INSERT INTO override_type (type) VALUES (%s)"
165 for override_type in self.Cnf.ValueList("OverrideType"):
166 c.execute(over_add, [override_type])
168 self.projectB.commit()
170 def do_priority(self):
171 """Initialize the priority table."""
173 c = self.projectB.cursor()
174 c.execute("DELETE FROM priority")
176 prio_add = "INSERT INTO priority (priority, level) VALUES (%s, %s)"
178 for priority in self.Cnf.SubTree("Priority").List():
179 c.execute(prio_add, [priority, self.Cnf["Priority::%s" % (priority)]])
181 self.projectB.commit()
183 def do_section(self):
184 """Initalize the section table."""
186 c = projectB.cursor()
187 c.execute("DELETE FROM section")
189 sect_add = "INSERT INTO section (section) VALUES (%s)"
191 for component in self.Cnf.SubTree("Component").List():
192 if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
194 if component != "main":
195 prefix = component + '/'
200 if component != "main":
201 suffix = '/' + component
204 for section in self.Cnf.ValueList("Section"):
205 c.execute(sect_add, [prefix + section + suffix])
207 self.projectB.commit()
211 self.do_architecture()
215 self.do_override_type()
219 ################################################################################
222 """Sync dak.conf configuartion file and the SQL database"""
224 Cnf = utils.get_conf()
225 arguments = [('h', "help", "Init-DB::Options::Help")]
227 if not Cnf.has_key("Init-DB::Options::%s" % (i)):
228 Cnf["Init-DB::Options::%s" % (i)] = ""
230 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
232 options = Cnf.SubTree("Init-DB::Options")
236 utils.warn("dak init-db takes no arguments.")
239 # Just let connection failures be reported to the user
243 InitDB(Cnf, projectB).do_all()
245 ################################################################################
247 if __name__ == '__main__':