]> git.decadent.org.uk Git - dak.git/blob - dak/init_db.py
remove UserExtensions
[dak.git] / dak / init_db.py
1 #!/usr/bin/env python
2
3 """Sync dak.conf 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 sys
23 import apt_pkg
24
25 from daklib import utils
26 from daklib.dbconn import *
27 from daklib.config import Config
28
29 ################################################################################
30
31 def usage(exit_code=0):
32     """Print a usage message and exit with 'exit_code'."""
33
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 class InitDB(object):
43     def __init__(self, Cnf, projectB):
44         self.Cnf = Cnf
45         self.projectB = projectB
46
47     def do_archive(self):
48         """initalize the archive table."""
49
50         # Remove existing archives
51         s = self.projectB.session()
52         s.query(Archive).delete()
53
54         for name in self.Cnf.SubTree("Archive").List():
55             a = Archive()
56             a.archive_name  = name
57             a.origin_server = self.Cnf.get("Archive::%s::OriginServer" % name, "")
58             a.description   = self.Cnf.get("Archive::%s::Description" % name,  "")
59             s.add(a)
60
61         s.commit()
62
63     def do_architecture(self):
64         """Initalize the architecture table."""
65
66         # Remove existing architectures
67         s = self.projectB.session()
68         s.query(Architecture).delete()
69
70         for arch in self.Cnf.SubTree("Architectures").List():
71             a = Architecture()
72             a.arch_string  = arch
73             a.description  = self.Cnf.get("Architecture::%s" % arch, "")
74             s.add(a)
75
76         s.commit()
77
78     def do_component(self):
79         """Initalize the component table."""
80
81         # Remove existing components
82         s = self.projectB.session()
83         s.query(Component).delete()
84
85         for name in self.Cnf.SubTree("Component").List():
86             c = Component()
87             c.component_name = name
88             c.description = self.Cnf.get("Component::%s::Description" % name, "")
89             c.meets_dfsg  = False
90             if self.Cnf.get("Component::%s::MeetsDFSG" % name, "false").lower() == 'true':
91                 c.meets_dfsg = True
92             s.add(c)
93
94         s.commit()
95
96     def do_location(self):
97         """Initalize the location table."""
98
99         # Remove existing locations
100         s = self.projectB.session()
101         s.query(Location).delete()
102
103         for location in self.Cnf.SubTree("Location").List():
104             archive_name = self.Cnf.get("Location::%s::Archive" % location, "")
105             a = s.query(Archive).filter_by(archive_name=archive_name)
106             if a.count() < 1:
107                 utils.fubar("E: Archive '%s' for location '%s' not found" % (archive_name, location))
108             archive_id = a.one().archive_id
109
110             location_type = self.Cnf.get("Location::%s::Type" % location, "")
111             if location_type != 'pool':
112                 utils.fubar("E: type %s not recognised for location %s" % (location_type, location))
113
114             for component in self.Cnf.SubTree("Component").List():
115                 c = s.query(Component).filter_by(component_name=component)
116                 if c.count() < 1:
117                     utils.fubar("E: Can't find component %s for location %s" % (component, location))
118                 component_id = c.one().component_id
119
120                 l = Location()
121                 l.path = location
122                 l.archive_id = archive_id
123                 l.component_id = component_id
124                 l.archive_type = location_type
125                 s.add(l)
126
127         s.commit()
128
129     def do_suite(self):
130         """Initalize the suite table."""
131
132         s = self.projectB.session()
133         s.query(Suite).delete()
134
135         for suite in self.Cnf.SubTree("Suite").List():
136             su = Suite()
137             su.version     = self.Cnf.get("Suite::%s::Version" % suite, "-")
138             su.origin      = self.Cnf.get("Suite::%s::Origin" % suite, "")
139             su.description = self.Cnf.get("Suite::%s::Description" % suite, "")
140             s.add(su)
141
142             for architecture in self.Cnf.ValueList("Suite::%s::Architectures" % (suite)):
143                 sa = SuiteArchitecture()
144                 sa.suite_id = su.suite_id
145                 a = s.query(Architecture).filter_by(arch_string=architecture)
146                 if a.count() < 1:
147                     utils.fubar("E: Architecture %s not found for suite %s" % (architecture, suite))
148                 sa.arch_id = a.one().arch_id
149                 s.add(sa)
150
151         s.commit()
152
153     def do_override_type(self):
154         """Initalize the override_type table."""
155
156         s = self.projectB.session()
157         s.query(OverrideType).delete()
158
159         for override_type in self.Cnf.ValueList("OverrideType"):
160             ot = OverrideType()
161             ot.overridetype = override_type
162             s.add(ot)
163
164         s.commit()
165
166     def do_priority(self):
167         """Initialize the priority table."""
168
169         s = self.projectB.session()
170         s.query(Priority).delete()
171
172         for priority in self.Cnf.SubTree("Priority").List():
173             p = Priority()
174             p.priority = priority
175             p.level    = self.Cnf.get("Priority::%s", 0)
176             s.add(p)
177
178         s.commit()
179
180     def do_section(self):
181         """Initalize the section table."""
182
183         s = self.projectB.session()
184         s.query(Section).delete()
185
186         for component in self.Cnf.SubTree("Component").List():
187             if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
188                 suffix = ""
189                 if component != "main":
190                     prefix = component + '/'
191                 else:
192                     prefix = ""
193             else:
194                 prefix = ""
195                 if component != "main":
196                     suffix = '/' + component
197                 else:
198                     suffix = ""
199
200             for section in self.Cnf.ValueList("Section"):
201                 sec = Section()
202                 sec.section = prefix + section + suffix
203                 s.add(sec)
204
205         s.commit()
206
207     def do_all(self):
208         self.do_archive()
209         self.do_architecture()
210         self.do_component()
211         self.do_location()
212         self.do_suite()
213         self.do_override_type()
214         self.do_priority()
215         self.do_section()
216
217 ################################################################################
218
219 def main ():
220     """Sync dak.conf configuartion file and the SQL database"""
221
222     Cnf = utils.get_conf()
223     arguments = [('h', "help", "Init-DB::Options::Help")]
224     for i in [ "help" ]:
225         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
226             Cnf["Init-DB::Options::%s" % (i)] = ""
227
228     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
229
230     options = Cnf.SubTree("Init-DB::Options")
231     if options["Help"]:
232         usage()
233     elif arguments:
234         utils.warn("dak init-db takes no arguments.")
235         usage(exit_code=1)
236
237     # Just let connection failures be reported to the user
238     projectB = DBConn()
239     Cnf = Config()
240
241     InitDB(Cnf, projectB).do_all()
242
243 ################################################################################
244
245 if __name__ == '__main__':
246     main()