]> git.donarmstrong.com Git - dak.git/blob - dak/init_db.py
fix update script
[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 psycopg2, 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         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()
69
70     def do_architecture(self):
71         """Initalize the architecture table."""
72
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()
80
81     def do_component(self):
82         """Initalize the component table."""
83
84         c = self.projectB.cursor()
85         c.execute("DELETE FROM component")
86
87         comp_add = "INSERT INTO component (name, description, meets_dfsg) " + \
88                    "VALUES (%s, %s, %s)"
89
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])
95
96         self.projectB.commit()
97
98     def do_location(self):
99         """Initalize the location table."""
100
101         c = self.projectB.cursor()
102         c.execute("DELETE FROM location")
103
104         loc_add = "INSERT INTO location (path, component, archive, type) " + \
105                   "VALUES (%s, %s, %s, %s)"
106
107         for location in self.Cnf.SubTree("Location").List():
108             location_config = self.Cnf.SubTree("Location::%s" % (location))
109             archive_id = self.projectB.get_archive_id(location_config["Archive"])
110             if archive_id == -1:
111                 utils.fubar("Archive '%s' for location '%s' not found."
112                                    % (location_config["Archive"], location))
113             location_type = location_config.get("type")
114             if location_type == "pool":
115                 for component in self.Cnf.SubTree("Component").List():
116                     component_id = self.projectB.get_component_id(component)
117                     c.execute(loc_add, [location, component_id, archive_id, location_type])
118             else:
119                 utils.fubar("E: type '%s' not recognised in location %s."
120                                    % (location_type, location))
121
122         self.projectB.commit()
123
124     def do_suite(self):
125         """Initalize the suite table."""
126
127         c = self.projectB.cursor()
128         c.execute("DELETE FROM suite")
129
130         suite_add = "INSERT INTO suite (suite_name, version, origin, description) " + \
131                     "VALUES (%s, %s, %s, %s)"
132
133         sa_add = "INSERT INTO suite_architectures (suite, architecture) " + \
134                  "VALUES (currval('suite_id_seq'), %s)"
135
136         for suite in self.Cnf.SubTree("Suite").List():
137             suite_config = self.Cnf.SubTree("Suite::%s" %(suite))
138             version = sql_get(suite_config, "Version")
139             origin = sql_get(suite_config, "Origin")
140             description = sql_get(suite_config, "Description")
141             c.execute(suite_add, [suite.lower(), version, origin, description])
142             for architecture in self.Cnf.ValueList("Suite::%s::Architectures" % (suite)):
143                 architecture_id = self.projectB.get_architecture_id (architecture)
144                 if architecture_id < 0:
145                     utils.fubar("architecture '%s' not found in architecture"
146                                 " table for suite %s."
147                                 % (architecture, suite))
148                 c.execute(sa_add, [architecture_id])
149
150         self.projectB.commit()
151
152     def do_override_type(self):
153         """Initalize the override_type table."""
154
155         c = self.projectB.cursor()
156         c.execute("DELETE FROM override_type")
157
158         over_add = "INSERT INTO override_type (type) VALUES (%s)"
159
160         for override_type in self.Cnf.ValueList("OverrideType"):
161             c.execute(over_add, [override_type])
162
163         self.projectB.commit()
164
165     def do_priority(self):
166         """Initialize the priority table."""
167
168         c = self.projectB.cursor()
169         c.execute("DELETE FROM priority")
170
171         prio_add = "INSERT INTO priority (priority, level) VALUES (%s, %s)"
172
173         for priority in self.Cnf.SubTree("Priority").List():
174             c.execute(prio_add, [priority, self.Cnf["Priority::%s" % (priority)]])
175
176         self.projectB.commit()
177
178     def do_section(self):
179         """Initalize the section table."""
180
181         c = self.projectB.cursor()
182         c.execute("DELETE FROM section")
183
184         sect_add = "INSERT INTO section (section) VALUES (%s)"
185
186         for component in self.Cnf.SubTree("Component").List():
187             if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
188                 suffix = ""
189                 if component != "main":
190                     prefix = component + '/'
191                 else:
192                     prefix = ""
193             else:
194                 prefix = ""
195                 if component != "main":
196                     suffix = '/' + component
197                 else:
198                     suffix = ""
199             for section in self.Cnf.ValueList("Section"):
200                 c.execute(sect_add, [prefix + section + suffix])
201
202         self.projectB.commit()
203
204     def do_all(self):
205         self.do_archive()
206         self.do_architecture()
207         self.do_component()
208         self.do_location()
209         self.do_suite()
210         self.do_override_type()
211         self.do_priority()
212         self.do_section()
213
214 ################################################################################
215
216 def main ():
217     """Sync dak.conf configuartion file and the SQL database"""
218
219     Cnf = utils.get_conf()
220     arguments = [('h', "help", "Init-DB::Options::Help")]
221     for i in [ "help" ]:
222         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
223             Cnf["Init-DB::Options::%s" % (i)] = ""
224
225     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
226
227     options = Cnf.SubTree("Init-DB::Options")
228     if options["Help"]:
229         usage()
230     elif arguments:
231         utils.warn("dak init-db takes no arguments.")
232         usage(exit_code=1)
233
234     # Just let connection failures be reported to the user
235     projectB = DBConn()
236     Cnf = Config()
237
238     InitDB(Cnf, projectB).do_all()
239
240 ################################################################################
241
242 if __name__ == '__main__':
243     main()