]> git.decadent.org.uk Git - dak.git/blob - dak/init_db.py
key expire
[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 == "pool":
110             for component in Cnf.SubTree("Component").List():
111                 component_id = database.get_component_id(component)
112                 projectB.query("INSERT INTO location (path, component, "
113                                "archive, type) VALUES ('%s', %d, %d, '%s')"
114                                % (location, component_id, archive_id,
115                                   location_type))
116         else:
117             utils.fubar("E: type '%s' not recognised in location %s."
118                                % (location_type, location))
119     projectB.query("COMMIT WORK")
120
121 def do_suite():
122     """Initalize the suite table."""
123
124     projectB.query("BEGIN WORK")
125     projectB.query("DELETE FROM suite")
126     for suite in Cnf.SubTree("Suite").List():
127         suite_config = Cnf.SubTree("Suite::%s" %(suite))
128         version = sql_get(suite_config, "Version")
129         origin = sql_get(suite_config, "Origin")
130         description = sql_get(suite_config, "Description")
131         projectB.query("INSERT INTO suite (suite_name, version, origin, "
132                        "description) VALUES ('%s', %s, %s, %s)"
133                        % (suite.lower(), version, origin, description))
134         for architecture in database.get_suite_architectures(suite):
135             architecture_id = database.get_architecture_id (architecture)
136             if architecture_id < 0:
137                 utils.fubar("architecture '%s' not found in architecture"
138                                    " table for suite %s."
139                                    % (architecture, suite))
140             projectB.query("INSERT INTO suite_architectures (suite, "
141                            "architecture) VALUES (currval('suite_id_seq'), %d)"
142                            % (architecture_id))
143     projectB.query("COMMIT WORK")
144
145 def do_override_type():
146     """Initalize the override_type table."""
147
148     projectB.query("BEGIN WORK")
149     projectB.query("DELETE FROM override_type")
150     for override_type in Cnf.ValueList("OverrideType"):
151         projectB.query("INSERT INTO override_type (type) VALUES ('%s')"
152                        % (override_type))
153     projectB.query("COMMIT WORK")
154
155 def do_priority():
156     """Initialize the priority table."""
157
158     projectB.query("BEGIN WORK")
159     projectB.query("DELETE FROM priority")
160     for priority in Cnf.SubTree("Priority").List():
161         projectB.query("INSERT INTO priority (priority, level) VALUES "
162                        "('%s', %s)"
163                        % (priority, Cnf["Priority::%s" % (priority)]))
164     projectB.query("COMMIT WORK")
165
166 def do_section():
167     """Initalize the section table."""
168     projectB.query("BEGIN WORK")
169     projectB.query("DELETE FROM section")
170     for component in Cnf.SubTree("Component").List():
171         if Cnf["Control-Overrides::ComponentPosition"] == "prefix":
172             suffix = ""
173             if component != "main":
174                 prefix = component + '/'
175             else:
176                 prefix = ""
177         else:
178             prefix = ""
179             if component != "main":
180                 suffix = '/' + component
181             else:
182                 suffix = ""
183         for section in Cnf.ValueList("Section"):
184             projectB.query("INSERT INTO section (section) VALUES "
185                            "('%s%s%s')" % (prefix, section, suffix))
186     projectB.query("COMMIT WORK")
187
188 ################################################################################
189
190 def main ():
191     """Sync dak.conf configuartion file and the SQL database"""
192
193     global Cnf, projectB
194
195     Cnf = utils.get_conf()
196     arguments = [('h', "help", "Init-DB::Options::Help")]
197     for i in [ "help" ]:
198         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
199             Cnf["Init-DB::Options::%s" % (i)] = ""
200
201     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
202
203     options = Cnf.SubTree("Init-DB::Options")
204     if options["Help"]:
205         usage()
206     elif arguments:
207         utils.warn("dak init-db takes no arguments.")
208         usage(exit_code=1)
209
210     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],
211                           int(Cnf["DB::Port"]))
212     database.init(Cnf, projectB)
213
214     do_archive()
215     do_architecture()
216     do_component()
217     do_location()
218     do_suite()
219     do_override_type()
220     do_priority()
221     do_section()
222
223 ################################################################################
224
225 if __name__ == '__main__':
226     main()