]> git.decadent.org.uk Git - dak.git/blob - dak/init_db.py
untested conversion to sqla class
[dak.git] / dak / init_db.py
1 #!/usr/bin/env python
2
3 """Sync dak.conf configuartion file and the SQL database"""
4 # Copyright (C) 2000, 2001, 2002, 2003, 2006  James Troup <james@nocrew.org>
5
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.
10
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.
15
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
19
20 ################################################################################
21
22 import sys
23 import apt_pkg
24
25 from daklib import utils
26 from daklib.dbconn import DBConn
27 from daklib.config import Config
28
29 ################################################################################
30
31 def usage(exit_code=0):
32     """Print a usage message and exit with 'exit_code'."""
33
34     print """Usage: dak init-db
35 Initalizes some tables in the projectB database based on the config file.
36
37   -h, --help                show this help and exit."""
38     sys.exit(exit_code)
39
40 ################################################################################
41
42 def sql_get (config, key):
43     """Return the value of config[key] or None if it doesn't exist."""
44
45     try:
46         return config[key]
47     except KeyError:
48         return None
49
50 ################################################################################
51
52 class InitDB(object):
53     def __init__(self, Cnf, projectB):
54         self.Cnf = Cnf
55         self.projectB = projectB
56
57     def do_archive(self):
58         """initalize the archive table."""
59
60         s = self.projectB.session()
61         s.begin()
62         s.execute("DELETE FROM archive")
63         archive_add = "INSERT INTO archive (name, origin_server, description) VALUES (%s, %s, %s)"
64         for name in self.Cnf.SubTree("Archive").List():
65             archive_config = self.Cnf.SubTree("Archive::%s" % (name))
66             origin_server = sql_get(archive_config, "OriginServer")
67             description = sql_get(archive_config, "Description")
68             s.execute(archive_add, [name, origin_server, description])
69         s.commit()
70
71     def do_architecture(self):
72         """Initalize the architecture table."""
73
74         s = self.projectB.session()
75         s.begin()
76         s.execute("DELETE FROM architecture")
77         arch_add = "INSERT INTO architecture (arch_string, description) VALUES (%s, %s)"
78         for arch in self.Cnf.SubTree("Architectures").List():
79             description = self.Cnf["Architectures::%s" % (arch)]
80             s.execute(arch_add, [arch, description])
81         s.commit()
82
83     def do_component(self):
84         """Initalize the component table."""
85
86         s = self.projectB.session()
87         s.begin()
88         s.execute("DELETE FROM component")
89
90         comp_add = "INSERT INTO component (name, description, meets_dfsg) " + \
91                    "VALUES (%s, %s, %s)"
92
93         for name in self.Cnf.SubTree("Component").List():
94             component_config = self.Cnf.SubTree("Component::%s" % (name))
95             description = sql_get(component_config, "Description")
96             meets_dfsg = (component_config.get("MeetsDFSG").lower() == "true")
97             s.execute(comp_add, [name, description, meets_dfsg])
98
99         s.commit()
100
101     def do_location(self):
102         """Initalize the location table."""
103
104         s = self.projectB.session()
105         s.begin()
106         s.execute("DELETE FROM location")
107
108         loc_add = "INSERT INTO location (path, component, archive, type) " + \
109                   "VALUES (%s, %s, %s, %s)"
110
111         for location in self.Cnf.SubTree("Location").List():
112             location_config = self.Cnf.SubTree("Location::%s" % (location))
113             archive_id = self.projectB.get_archive_id(location_config["Archive"])
114             if archive_id == -1:
115                 utils.fubar("Archive '%s' for location '%s' not found."
116                                    % (location_config["Archive"], location))
117             location_type = location_config.get("type")
118             if location_type == "pool":
119                 for component in self.Cnf.SubTree("Component").List():
120                     component_id = self.projectB.get_component_id(component)
121                     s.execute(loc_add, [location, component_id, archive_id, location_type])
122             else:
123                 utils.fubar("E: type '%s' not recognised in location %s."
124                                    % (location_type, location))
125
126         s.commit()
127
128     def do_suite(self):
129         """Initalize the suite table."""
130
131         s = self.projectB.session()
132         s.begin()
133         s.execute("DELETE FROM suite")
134
135         suite_add = "INSERT INTO suite (suite_name, version, origin, description) " + \
136                     "VALUES (%s, %s, %s, %s)"
137
138         sa_add = "INSERT INTO suite_architectures (suite, architecture) " + \
139                  "VALUES (currval('suite_id_seq'), %s)"
140
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             s.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                 s.execute(sa_add, [architecture_id])
154
155         s.commit()
156
157     def do_override_type(self):
158         """Initalize the override_type table."""
159
160         s = self.projectB.session()
161         s.begin()
162         s.execute("DELETE FROM override_type")
163
164         over_add = "INSERT INTO override_type (type) VALUES (%s)"
165
166         for override_type in self.Cnf.ValueList("OverrideType"):
167             s.execute(over_add, [override_type])
168
169         s.commit()
170
171     def do_priority(self):
172         """Initialize the priority table."""
173
174         s = self.projectB.session()
175         s.begin()
176         s.execute("DELETE FROM priority")
177
178         prio_add = "INSERT INTO priority (priority, level) VALUES (%s, %s)"
179
180         for priority in self.Cnf.SubTree("Priority").List():
181             s.execute(prio_add, [priority, self.Cnf["Priority::%s" % (priority)]])
182
183         s.commit()
184
185     def do_section(self):
186         """Initalize the section table."""
187
188         s = self.projectB.session()
189         s.begin()
190         s.execute("DELETE FROM section")
191
192         sect_add = "INSERT INTO section (section) VALUES (%s)"
193
194         for component in self.Cnf.SubTree("Component").List():
195             if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
196                 suffix = ""
197                 if component != "main":
198                     prefix = component + '/'
199                 else:
200                     prefix = ""
201             else:
202                 prefix = ""
203                 if component != "main":
204                     suffix = '/' + component
205                 else:
206                     suffix = ""
207             for section in self.Cnf.ValueList("Section"):
208                 s.execute(sect_add, [prefix + section + suffix])
209
210         s.commit()
211
212     def do_all(self):
213         self.do_archive()
214         self.do_architecture()
215         self.do_component()
216         self.do_location()
217         self.do_suite()
218         self.do_override_type()
219         self.do_priority()
220         self.do_section()
221
222 ################################################################################
223
224 def main ():
225     """Sync dak.conf configuartion file and the SQL database"""
226
227     Cnf = utils.get_conf()
228     arguments = [('h', "help", "Init-DB::Options::Help")]
229     for i in [ "help" ]:
230         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
231             Cnf["Init-DB::Options::%s" % (i)] = ""
232
233     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
234
235     options = Cnf.SubTree("Init-DB::Options")
236     if options["Help"]:
237         usage()
238     elif arguments:
239         utils.warn("dak init-db takes no arguments.")
240         usage(exit_code=1)
241
242     # Just let connection failures be reported to the user
243     projectB = DBConn()
244     Cnf = Config()
245
246     InitDB(Cnf, projectB).do_all()
247
248 ################################################################################
249
250 if __name__ == '__main__':
251     main()