]> git.decadent.org.uk Git - dak.git/blob - dak/init_db.py
Enmasse adaptation for removal of silly names.
[dak.git] / dak / init_db.py
1 #!/usr/bin/env python
2
3 # Sync the ISC 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 dak.lib.utils, dak.lib.database
24 import apt_pkg
25
26 ################################################################################
27
28 Cnf = None
29 projectB = None
30
31 ################################################################################
32
33 def usage(exit_code=0):
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 get (c, i):
43     if c.has_key(i):
44         return "'%s'" % (c[i])
45     else:
46         return "NULL"
47
48 def main ():
49     global Cnf, projectB
50
51     Cnf = dak.lib.utils.get_conf()
52     Arguments = [('h',"help","Init-DB::Options::Help")]
53     for i in [ "help" ]:
54         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
55             Cnf["Init-DB::Options::%s" % (i)] = ""
56
57     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
58
59     Options = Cnf.SubTree("Init-DB::Options")
60     if Options["Help"]:
61         usage()
62
63     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
64     dak.lib.database.init(Cnf, projectB)
65
66     # archive
67
68     projectB.query("BEGIN WORK")
69     projectB.query("DELETE FROM archive")
70     for name in Cnf.SubTree("Archive").List():
71         Archive = Cnf.SubTree("Archive::%s" % (name))
72         origin_server = get(Archive, "OriginServer")
73         description = get(Archive, "Description")
74         projectB.query("INSERT INTO archive (name, origin_server, description) VALUES ('%s', %s, %s)" % (name, origin_server, description))
75     projectB.query("COMMIT WORK")
76
77     # architecture
78
79     projectB.query("BEGIN WORK")
80     projectB.query("DELETE FROM architecture")
81     for arch in Cnf.SubTree("Architectures").List():
82         description = Cnf["Architectures::%s" % (arch)]
83         projectB.query("INSERT INTO architecture (arch_string, description) VALUES ('%s', '%s')" % (arch, description))
84     projectB.query("COMMIT WORK")
85
86     # component
87
88     projectB.query("BEGIN WORK")
89     projectB.query("DELETE FROM component")
90     for name in Cnf.SubTree("Component").List():
91         Component = Cnf.SubTree("Component::%s" % (name))
92         description = get(Component, "Description")
93         if Component.get("MeetsDFSG").lower() == "true":
94             meets_dfsg = "true"
95         else:
96             meets_dfsg = "false"
97         projectB.query("INSERT INTO component (name, description, meets_dfsg) VALUES ('%s', %s, %s)" % (name, description, meets_dfsg))
98     projectB.query("COMMIT WORK")
99
100     # location
101
102     projectB.query("BEGIN WORK")
103     projectB.query("DELETE FROM location")
104     for location in Cnf.SubTree("Location").List():
105         Location = Cnf.SubTree("Location::%s" % (location))
106         archive_id = dak.lib.database.get_archive_id(Location["Archive"])
107         type = Location.get("type")
108         if type == "legacy-mixed":
109             projectB.query("INSERT INTO location (path, archive, type) VALUES ('%s', %d, '%s')" % (location, archive_id, Location["type"]))
110         elif type == "legacy" or type == "pool":
111             for component in Cnf.SubTree("Component").List():
112                 component_id = dak.lib.database.get_component_id(component)
113                 projectB.query("INSERT INTO location (path, component, archive, type) VALUES ('%s', %d, %d, '%s')" %
114                                (location, component_id, archive_id, type))
115         else:
116             dak.lib.utils.fubar("E: type '%s' not recognised in location %s." % (type, location))
117     projectB.query("COMMIT WORK")
118
119     # suite
120
121     projectB.query("BEGIN WORK")
122     projectB.query("DELETE FROM suite")
123     for suite in Cnf.SubTree("Suite").List():
124         Suite = Cnf.SubTree("Suite::%s" %(suite))
125         version = get(Suite, "Version")
126         origin = get(Suite, "Origin")
127         description = get(Suite, "Description")
128         projectB.query("INSERT INTO suite (suite_name, version, origin, description) VALUES ('%s', %s, %s, %s)"
129                        % (suite.lower(), version, origin, description))
130         for architecture in Cnf.ValueList("Suite::%s::Architectures" % (suite)):
131             architecture_id = dak.lib.database.get_architecture_id (architecture)
132             if architecture_id < 0:
133                 dak.lib.utils.fubar("architecture '%s' not found in architecture table for suite %s." % (architecture, suite))
134             projectB.query("INSERT INTO suite_architectures (suite, architecture) VALUES (currval('suite_id_seq'), %d)" % (architecture_id))
135     projectB.query("COMMIT WORK")
136
137     # override_type
138
139     projectB.query("BEGIN WORK")
140     projectB.query("DELETE FROM override_type")
141     for type in Cnf.ValueList("OverrideType"):
142         projectB.query("INSERT INTO override_type (type) VALUES ('%s')" % (type))
143     projectB.query("COMMIT WORK")
144
145     # priority
146
147     projectB.query("BEGIN WORK")
148     projectB.query("DELETE FROM priority")
149     for priority in Cnf.SubTree("Priority").List():
150         projectB.query("INSERT INTO priority (priority, level) VALUES ('%s', %s)" % (priority, Cnf["Priority::%s" % (priority)]))
151     projectB.query("COMMIT WORK")
152
153     # section
154
155     projectB.query("BEGIN WORK")
156     projectB.query("DELETE FROM section")
157     for component in Cnf.SubTree("Component").List():
158         if Cnf["Control-Overrides::ComponentPosition"] == "prefix":
159             suffix = ""
160             if component != "main":
161                 prefix = component + '/'
162             else:
163                 prefix = ""
164         else:
165             prefix = ""
166             component = component.replace("non-US/", "")
167             if component != "main":
168                 suffix = '/' + component
169             else:
170                 suffix = ""
171         for section in Cnf.ValueList("Section"):
172             projectB.query("INSERT INTO section (section) VALUES ('%s%s%s')" % (prefix, section, suffix))
173     projectB.query("COMMIT WORK")
174
175 ################################################################################
176
177 if __name__ == '__main__':
178     main()
179