]> git.decadent.org.uk Git - dak.git/blob - dak/init_db.py
update db
[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 pg, sys
23 import apt_pkg
24 from daklib import database
25 from daklib import utils
26
27 ################################################################################
28
29 Cnf = None
30 projectB = None
31
32 ################################################################################
33
34 def usage(exit_code=0):
35     """Print a usage message and exit with 'exit_code'."""
36
37     print """Usage: dak init-db
38 Initalizes some tables in the projectB database based on the config file.
39
40   -h, --help                show this help and exit."""
41     sys.exit(exit_code)
42
43 ################################################################################
44
45 def sql_get (config, key):
46     """Return the value of config[key] in quotes or NULL if it doesn't exist."""
47
48     if config.has_key(key):
49         return "'%s'" % (config[key])
50     else:
51         return "NULL"
52
53 ################################################################################
54
55 def do_archive():
56     """Initalize the archive table."""
57
58     projectB.query("BEGIN WORK")
59     projectB.query("DELETE FROM archive")
60     for name in Cnf.SubTree("Archive").List():
61         archive_config = Cnf.SubTree("Archive::%s" % (name))
62         origin_server = sql_get(archive_config, "OriginServer")
63         description = sql_get(archive_config, "Description")
64         projectB.query("INSERT INTO archive (name, origin_server, description) "
65                        "VALUES ('%s', %s, %s)"
66                        % (name, origin_server, description))
67     projectB.query("COMMIT WORK")
68
69 def do_architecture():
70     """Initalize the architecture table."""
71
72     projectB.query("BEGIN WORK")
73     projectB.query("DELETE FROM architecture")
74     for arch in Cnf.SubTree("Architectures").List():
75         description = Cnf["Architectures::%s" % (arch)]
76         projectB.query("INSERT INTO architecture (arch_string, description) "
77                        "VALUES ('%s', '%s')" % (arch, description))
78     projectB.query("COMMIT WORK")
79
80 def do_component():
81     """Initalize the component table."""
82
83     projectB.query("BEGIN WORK")
84     projectB.query("DELETE FROM component")
85     for name in Cnf.SubTree("Component").List():
86         component_config = Cnf.SubTree("Component::%s" % (name))
87         description = sql_get(component_config, "Description")
88         if component_config.get("MeetsDFSG").lower() == "true":
89             meets_dfsg = "true"
90         else:
91             meets_dfsg = "false"
92         projectB.query("INSERT INTO component (name, description, meets_dfsg) "
93                        "VALUES ('%s', %s, %s)"
94                        % (name, description, meets_dfsg))
95     projectB.query("COMMIT WORK")
96
97 def do_location():
98     """Initalize the location table."""
99
100     projectB.query("BEGIN WORK")
101     projectB.query("DELETE FROM location")
102     for location in Cnf.SubTree("Location").List():
103         location_config = Cnf.SubTree("Location::%s" % (location))
104         archive_id = database.get_archive_id(location_config["Archive"])
105         if archive_id == -1:
106             utils.fubar("Archive '%s' for location '%s' not found."
107                                % (location_config["Archive"], location))
108         location_type = location_config.get("type")
109         if location_type == "legacy-mixed":
110             projectB.query("INSERT INTO location (path, archive, type) VALUES "
111                            "('%s', %d, '%s')"
112                            % (location, archive_id, location_config["type"]))
113         elif location_type == "legacy" or location_type == "pool":
114             for component in Cnf.SubTree("Component").List():
115                 component_id = database.get_component_id(component)
116                 projectB.query("INSERT INTO location (path, component, "
117                                "archive, type) VALUES ('%s', %d, %d, '%s')"
118                                % (location, component_id, archive_id,
119                                   location_type))
120         else:
121             utils.fubar("E: type '%s' not recognised in location %s."
122                                % (location_type, location))
123     projectB.query("COMMIT WORK")
124
125 def do_suite():
126     """Initalize the suite table."""
127
128     projectB.query("BEGIN WORK")
129     projectB.query("DELETE FROM suite")
130     for suite in Cnf.SubTree("Suite").List():
131         suite_config = Cnf.SubTree("Suite::%s" %(suite))
132         version = sql_get(suite_config, "Version")
133         origin = sql_get(suite_config, "Origin")
134         description = sql_get(suite_config, "Description")
135         projectB.query("INSERT INTO suite (suite_name, version, origin, "
136                        "description) VALUES ('%s', %s, %s, %s)"
137                        % (suite.lower(), version, origin, description))
138         for architecture in Cnf.ValueList("Suite::%s::Architectures" % (suite)):
139             architecture_id = database.get_architecture_id (architecture)
140             if architecture_id < 0:
141                 utils.fubar("architecture '%s' not found in architecture"
142                                    " table for suite %s."
143                                    % (architecture, suite))
144             projectB.query("INSERT INTO suite_architectures (suite, "
145                            "architecture) VALUES (currval('suite_id_seq'), %d)"
146                            % (architecture_id))
147     projectB.query("COMMIT WORK")
148
149 def do_override_type():
150     """Initalize the override_type table."""
151
152     projectB.query("BEGIN WORK")
153     projectB.query("DELETE FROM override_type")
154     for override_type in Cnf.ValueList("OverrideType"):
155         projectB.query("INSERT INTO override_type (type) VALUES ('%s')"
156                        % (override_type))
157     projectB.query("COMMIT WORK")
158
159 def do_priority():
160     """Initialize the priority table."""
161
162     projectB.query("BEGIN WORK")
163     projectB.query("DELETE FROM priority")
164     for priority in Cnf.SubTree("Priority").List():
165         projectB.query("INSERT INTO priority (priority, level) VALUES "
166                        "('%s', %s)"
167                        % (priority, Cnf["Priority::%s" % (priority)]))
168     projectB.query("COMMIT WORK")
169
170 def do_section():
171     """Initalize the section table."""
172     projectB.query("BEGIN WORK")
173     projectB.query("DELETE FROM section")
174     for component in Cnf.SubTree("Component").List():
175         if Cnf["Control-Overrides::ComponentPosition"] == "prefix":
176             suffix = ""
177             if component != "main":
178                 prefix = component + '/'
179             else:
180                 prefix = ""
181         else:
182             prefix = ""
183             if component != "main":
184                 suffix = '/' + component
185             else:
186                 suffix = ""
187         for section in Cnf.ValueList("Section"):
188             projectB.query("INSERT INTO section (section) VALUES "
189                            "('%s%s%s')" % (prefix, section, suffix))
190     projectB.query("COMMIT WORK")
191
192 ################################################################################
193
194 def main ():
195     """Sync dak.conf configuartion file and the SQL database"""
196
197     global Cnf, projectB
198
199     Cnf = utils.get_conf()
200     arguments = [('h', "help", "Init-DB::Options::Help")]
201     for i in [ "help" ]:
202         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
203             Cnf["Init-DB::Options::%s" % (i)] = ""
204
205     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
206
207     options = Cnf.SubTree("Init-DB::Options")
208     if options["Help"]:
209         usage()
210     elif arguments:
211         utils.warn("dak init-db takes no arguments.")
212         usage(exit_code=1)
213
214     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],
215                           int(Cnf["DB::Port"]))
216     database.init(Cnf, projectB)
217
218     do_archive()
219     do_architecture()
220     do_component()
221     do_location()
222     do_suite()
223     do_override_type()
224     do_priority()
225     do_section()
226
227 ################################################################################
228
229 if __name__ == '__main__':
230     main()