3 # Sync the ISC configuartion file and the SQL database
4 # Copyright (C) 2000, 2001, 2002, 2003, 2006 James Troup <james@nocrew.org>
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.
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.
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
20 ################################################################################
24 import daklib.database
27 ################################################################################
32 ################################################################################
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.
38 -h, --help show this help and exit."""
41 ################################################################################
45 return "'%s'" % (c[i])
52 Cnf = daklib.utils.get_conf()
53 Arguments = [('h',"help","Init-DB::Options::Help")]
55 if not Cnf.has_key("Init-DB::Options::%s" % (i)):
56 Cnf["Init-DB::Options::%s" % (i)] = ""
58 apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
60 Options = Cnf.SubTree("Init-DB::Options")
64 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
65 daklib.database.init(Cnf, projectB)
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")
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")
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":
98 projectB.query("INSERT INTO component (name, description, meets_dfsg) VALUES ('%s', %s, %s)" % (name, description, meets_dfsg))
99 projectB.query("COMMIT WORK")
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"])
109 daklib.utils.fubar("Archive '%s' for location '%s' not found."
110 % (Location["Archive"], location))
111 type = Location.get("type")
112 if type == "legacy-mixed":
113 projectB.query("INSERT INTO location (path, archive, type) VALUES ('%s', %d, '%s')" % (location, archive_id, Location["type"]))
114 elif type == "legacy" or type == "pool":
115 for component in Cnf.SubTree("Component").List():
116 component_id = daklib.database.get_component_id(component)
117 projectB.query("INSERT INTO location (path, component, archive, type) VALUES ('%s', %d, %d, '%s')" %
118 (location, component_id, archive_id, type))
120 daklib.utils.fubar("E: type '%s' not recognised in location %s." % (type, location))
121 projectB.query("COMMIT WORK")
125 projectB.query("BEGIN WORK")
126 projectB.query("DELETE FROM suite")
127 for suite in Cnf.SubTree("Suite").List():
128 Suite = Cnf.SubTree("Suite::%s" %(suite))
129 version = get(Suite, "Version")
130 origin = get(Suite, "Origin")
131 description = get(Suite, "Description")
132 projectB.query("INSERT INTO suite (suite_name, version, origin, description) VALUES ('%s', %s, %s, %s)"
133 % (suite.lower(), version, origin, description))
134 for architecture in Cnf.ValueList("Suite::%s::Architectures" % (suite)):
135 architecture_id = daklib.database.get_architecture_id (architecture)
136 if architecture_id < 0:
137 daklib.utils.fubar("architecture '%s' not found in architecture table for suite %s." % (architecture, suite))
138 projectB.query("INSERT INTO suite_architectures (suite, architecture) VALUES (currval('suite_id_seq'), %d)" % (architecture_id))
139 projectB.query("COMMIT WORK")
143 projectB.query("BEGIN WORK")
144 projectB.query("DELETE FROM override_type")
145 for type in Cnf.ValueList("OverrideType"):
146 projectB.query("INSERT INTO override_type (type) VALUES ('%s')" % (type))
147 projectB.query("COMMIT WORK")
151 projectB.query("BEGIN WORK")
152 projectB.query("DELETE FROM priority")
153 for priority in Cnf.SubTree("Priority").List():
154 projectB.query("INSERT INTO priority (priority, level) VALUES ('%s', %s)" % (priority, Cnf["Priority::%s" % (priority)]))
155 projectB.query("COMMIT WORK")
159 projectB.query("BEGIN WORK")
160 projectB.query("DELETE FROM section")
161 for component in Cnf.SubTree("Component").List():
162 if Cnf["Control-Overrides::ComponentPosition"] == "prefix":
164 if component != "main":
165 prefix = component + '/'
170 component = component.replace("non-US/", "")
171 if component != "main":
172 suffix = '/' + component
175 for section in Cnf.ValueList("Section"):
176 projectB.query("INSERT INTO section (section) VALUES ('%s%s%s')" % (prefix, section, suffix))
177 projectB.query("COMMIT WORK")
179 ################################################################################
181 if __name__ == '__main__':