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 == "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,
117 utils.fubar("E: type '%s' not recognised in location %s."
118 % (location_type, location))
119 projectB.query("COMMIT WORK")
122 """Initalize the suite table."""
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)"
143 projectB.query("COMMIT WORK")
145 def do_override_type():
146 """Initalize the override_type table."""
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')"
153 projectB.query("COMMIT WORK")
156 """Initialize the priority table."""
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 "
163 % (priority, Cnf["Priority::%s" % (priority)]))
164 projectB.query("COMMIT WORK")
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":
173 if component != "main":
174 prefix = component + '/'
179 if component != "main":
180 suffix = '/' + component
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")
188 ################################################################################
191 """Sync dak.conf configuartion file and the SQL database"""
195 Cnf = utils.get_conf()
196 arguments = [('h', "help", "Init-DB::Options::Help")]
198 if not Cnf.has_key("Init-DB::Options::%s" % (i)):
199 Cnf["Init-DB::Options::%s" % (i)] = ""
201 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
203 options = Cnf.SubTree("Init-DB::Options")
207 utils.warn("dak init-db takes no arguments.")
210 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],
211 int(Cnf["DB::Port"]))
212 database.init(Cnf, projectB)
223 ################################################################################
225 if __name__ == '__main__':