3 """Sync dak.conf 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 from daklib import database
25 from daklib import utils
27 ################################################################################
32 ################################################################################
34 def usage(exit_code=0):
35 """Print a usage message and exit with 'exit_code'."""
37 print """Usage: dak init-db
38 Initalizes some tables in the projectB database based on the config file.
40 -h, --help show this help and exit."""
43 ################################################################################
45 def sql_get (config, key):
46 """Return the value of config[key] in quotes or NULL if it doesn't exist."""
48 if config.has_key(key):
49 return "'%s'" % (config[key])
53 ################################################################################
56 """Initalize the archive table."""
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")
69 def do_architecture():
70 """Initalize the architecture table."""
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")
81 """Initalize the component table."""
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":
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")
98 """Initalize the location table."""
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"])
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 "
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,
121 utils.fubar("E: type '%s' not recognised in location %s."
122 % (location_type, location))
123 projectB.query("COMMIT WORK")
126 """Initalize the suite table."""
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)"
147 projectB.query("COMMIT WORK")
149 def do_override_type():
150 """Initalize the override_type table."""
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')"
157 projectB.query("COMMIT WORK")
160 """Initialize the priority table."""
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 "
167 % (priority, Cnf["Priority::%s" % (priority)]))
168 projectB.query("COMMIT WORK")
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":
177 if component != "main":
178 prefix = component + '/'
183 if component != "main":
184 suffix = '/' + component
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")
192 ################################################################################
195 """Sync dak.conf configuartion file and the SQL database"""
199 Cnf = utils.get_conf()
200 arguments = [('h', "help", "Init-DB::Options::Help")]
202 if not Cnf.has_key("Init-DB::Options::%s" % (i)):
203 Cnf["Init-DB::Options::%s" % (i)] = ""
205 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
207 options = Cnf.SubTree("Init-DB::Options")
211 utils.warn("dak init-db takes no arguments.")
214 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],
215 int(Cnf["DB::Port"]))
216 database.init(Cnf, projectB)
227 ################################################################################
229 if __name__ == '__main__':