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