]> git.decadent.org.uk Git - dak.git/blob - dak/init_db.py
Merge remote branch 'drkranz/master' into merge
[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         """Initialize 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             suite = suite.lower()
137             su = Suite()
138             su.suite_name  = suite
139             su.version     = self.Cnf.get("Suite::%s::Version" % suite, "-")
140             su.origin      = self.Cnf.get("Suite::%s::Origin" % suite, "")
141             su.description = self.Cnf.get("Suite::%s::Description" % suite, "")
142             s.add(su)
143
144             for architecture in self.Cnf.ValueList("Suite::%s::Architectures" % (suite)):
145                 sa = SuiteArchitecture()
146                 a = s.query(Architecture).filter_by(arch_string=architecture)
147                 if a.count() < 1:
148                     utils.fubar("E: Architecture %s not found for suite %s" % (architecture, suite))
149                 sa.arch_id = a.one().arch_id
150                 sa.suite_id = su.suite_id
151                 s.add(sa)
152
153         s.commit()
154
155     def do_override_type(self):
156         """Initalize the override_type table."""
157
158         s = self.projectB.session()
159         s.query(OverrideType).delete()
160
161         for override_type in self.Cnf.ValueList("OverrideType"):
162             ot = OverrideType()
163             ot.overridetype = override_type
164             s.add(ot)
165
166         s.commit()
167
168     def do_priority(self):
169         """Initialize the priority table."""
170
171         s = self.projectB.session()
172         s.query(Priority).delete()
173
174         for priority in self.Cnf.SubTree("Priority").List():
175             p = Priority()
176             p.priority = priority
177             p.level    = self.Cnf.get("Priority::" + priority, "0")
178             s.add(p)
179
180         s.commit()
181
182     def do_section(self):
183         """Initalize the section table."""
184
185         s = self.projectB.session()
186         s.query(Section).delete()
187
188         for component in self.Cnf.SubTree("Component").List():
189             if self.Cnf["Control-Overrides::ComponentPosition"] == "prefix":
190                 suffix = ""
191                 if component != "main":
192                     prefix = component + '/'
193                 else:
194                     prefix = ""
195             else:
196                 prefix = ""
197                 if component != "main":
198                     suffix = '/' + component
199                 else:
200                     suffix = ""
201
202             for section in self.Cnf.ValueList("Section"):
203                 sec = Section()
204                 sec.section = prefix + section + suffix
205                 s.add(sec)
206
207         s.commit()
208
209     def do_all(self):
210         self.do_archive()
211         self.do_architecture()
212         self.do_component()
213         self.do_location()
214         self.do_suite()
215         self.do_override_type()
216         self.do_priority()
217         self.do_section()
218
219 ################################################################################
220
221 def main ():
222     """Sync dak.conf configuartion file and the SQL database"""
223
224     Cnf = utils.get_conf()
225     arguments = [('h', "help", "Init-DB::Options::Help")]
226     for i in [ "help" ]:
227         if not Cnf.has_key("Init-DB::Options::%s" % (i)):
228             Cnf["Init-DB::Options::%s" % (i)] = ""
229
230     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
231
232     options = Cnf.SubTree("Init-DB::Options")
233     if options["Help"]:
234         usage()
235     elif arguments:
236         utils.warn("dak init-db takes no arguments.")
237         usage(exit_code=1)
238
239     # Just let connection failures be reported to the user
240     projectB = DBConn()
241     Cnf = Config()
242
243     InitDB(Cnf, projectB).do_all()
244
245 ################################################################################
246
247 if __name__ == '__main__':
248     main()