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 *
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 ################################################################################
43 def __init__(self, Cnf, projectB):
45 self.projectB = projectB
48 """initalize the archive table."""
50 # Remove existing archives
51 s = self.projectB.session()
52 s.query(Archive).delete()
54 for name in self.Cnf.SubTree("Archive").List():
57 a.origin_server = self.Cnf.get("Archive::%s::OriginServer" % name, "")
58 a.description = self.Cnf.get("Archive::%s::Description" % name, "")
63 def do_architecture(self):
64 """Initalize the architecture table."""
66 # Remove existing architectures
67 s = self.projectB.session()
68 s.query(Architecture).delete()
70 for arch in self.Cnf.SubTree("Architectures").List():
73 a.description = self.Cnf.get("Architecture::%s" % arch, "")
78 def do_component(self):
79 """Initalize the component table."""
81 # Remove existing components
82 s = self.projectB.session()
83 s.query(Component).delete()
85 for name in self.Cnf.SubTree("Component").List():
87 c.component_name = name
88 c.description = self.Cnf.get("Component::%s::Description" % name, "")
90 if self.Cnf.get("Component::%s::MeetsDFSG" % name, "false").lower() == 'true':
96 def do_location(self):
97 """Initalize the location table."""
99 # Remove existing locations
100 s = self.projectB.session()
101 s.query(Location).delete()
103 for location in self.Cnf.SubTree("Location").List():
104 archive_name = self.Cnf.get("Location::%s::Archive" % location, "")
105 a = s.query(Archive).filter_by(archive_name=archive_name)
107 utils.fubar("E: Archive '%s' for location '%s' not found" % (archive_name, location))
108 archive_id = a.one().archive_id
110 location_type = self.Cnf.get("Location::%s::Type" % location, "")
111 if location_type != 'pool':
112 utils.fubar("E: type %s not recognised for location %s" % (location_type, location))
114 for component in self.Cnf.SubTree("Component").List():
115 c = s.query(Component).filter_by(component_name=component)
117 utils.fubar("E: Can't find component %s for location %s" % (component, location))
118 component_id = c.one().component_id
122 l.archive_id = archive_id
123 l.component_id = component_id
124 l.archive_type = location_type
130 """Initialize the suite table."""
132 s = self.projectB.session()
133 s.query(Suite).delete()
135 for suite in self.Cnf.SubTree("Suite").List():
136 suite = suite.lower()
138 su.suite_name = suite
139 su.version = self.Cnf.get("Suite::%s::Version" % suite, "-")
140 su.origin = self.Cnf.get("Suite::%s::Origin" % suite, "")
141 su.description = self.Cnf.get("Suite::%s::Description" % suite, "")
144 for architecture in self.Cnf.ValueList("Suite::%s::Architectures" % (suite)):
145 sa = SuiteArchitecture()
146 a = s.query(Architecture).filter_by(arch_string=architecture)
148 utils.fubar("E: Architecture %s not found for suite %s" % (architecture, suite))
149 sa.arch_id = a.one().arch_id
150 sa.suite_id = su.suite_id
155 def do_override_type(self):
156 """Initalize the override_type table."""
158 s = self.projectB.session()
159 s.query(OverrideType).delete()
161 for override_type in self.Cnf.ValueList("OverrideType"):
163 ot.overridetype = override_type
168 def do_priority(self):
169 """Initialize the priority table."""
171 s = self.projectB.session()
172 s.query(Priority).delete()
174 for priority in self.Cnf.SubTree("Priority").List():
176 p.priority = priority
177 p.level = self.Cnf.get("Priority::" + priority, "0")
182 def do_section(self):
183 """Initalize the section table."""
185 s = self.projectB.session()
186 s.query(Section).delete()
188 for component in self.Cnf.SubTree("Component").List():
189 if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
191 if component != "main":
192 prefix = component + '/'
197 if component != "main":
198 suffix = '/' + component
202 for section in self.Cnf.ValueList("Section"):
204 sec.section = prefix + section + suffix
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__':