]> git.decadent.org.uk Git - dak.git/blob - dak/generate_releases.py
Merge commit 'mhy/sqlalchemy' into merge
[dak.git] / dak / generate_releases.py
1 #!/usr/bin/env python
2
3 """ Create all the Release files """
4
5 # Copyright (C) 2001, 2002, 2006  Anthony Towns <ajt@debian.org>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 #   ``Bored now''
22
23 ################################################################################
24
25 import sys, os, stat, time
26 import gzip, bz2
27 import apt_pkg
28
29 from daklib import utils
30 from daklib.dak_exceptions import *
31 from daklib.dbconn import *
32
33 ################################################################################
34
35 Cnf = None
36 out = None
37 AptCnf = None
38
39 ################################################################################
40
41 def usage (exit_code=0):
42     print """Usage: dak generate-releases [OPTION]... [SUITE]...
43 Generate Release files (for SUITE).
44
45   -h, --help                 show this help and exit
46   -a, --apt-conf FILE        use FILE instead of default apt.conf
47   -f, --force-touch          ignore Untouchable directives in dak.conf
48
49 If no SUITE is given Release files are generated for all suites."""
50
51     sys.exit(exit_code)
52
53 ################################################################################
54
55 def add_tiffani (files, path, indexstem):
56     index = "%s.diff/Index" % (indexstem)
57     filepath = "%s/%s" % (path, index)
58     if os.path.exists(filepath):
59         #print "ALERT: there was a tiffani file %s" % (filepath)
60         files.append(index)
61
62 def gen_i18n_index (files, tree, sec):
63     path = Cnf["Dir::Root"] + tree + "/"
64     i18n_path = "%s/i18n" % (sec)
65     if os.path.exists("%s/%s" % (path, i18n_path)):
66         index = "%s/Index" % (i18n_path)
67         out = open("%s/%s" % (path, index), "w")
68         out.write("SHA1:\n")
69         for x in os.listdir("%s/%s" % (path, i18n_path)):
70             if x.startswith('Translation-'):
71                 f = open("%s/%s/%s" % (path, i18n_path, x), "r")
72                 size = os.fstat(f.fileno())[6]
73                 f.seek(0)
74                 sha1sum = apt_pkg.sha1sum(f)
75                 f.close()
76                 out.write(" %s %7d %s\n" % (sha1sum, size, x))
77         out.close()
78         files.append(index)
79
80 def compressnames (tree,type,file):
81     compress = AptCnf.get("%s::%s::Compress" % (tree,type), AptCnf.get("Default::%s::Compress" % (type), ". gzip"))
82     result = []
83     cl = compress.split()
84     uncompress = ("." not in cl)
85     for mode in compress.split():
86         if mode == ".":
87             result.append(file)
88         elif mode == "gzip":
89             if uncompress:
90                 result.append("<zcat/.gz>" + file)
91                 uncompress = 0
92             result.append(file + ".gz")
93         elif mode == "bzip2":
94             if uncompress:
95                 result.append("<bzcat/.bz2>" + file)
96                 uncompress = 0
97             result.append(file + ".bz2")
98     return result
99
100 decompressors = { 'zcat' : gzip.GzipFile,
101                   'bzip2' : bz2.BZ2File }
102
103 def print_md5sha_files (tree, files, hashop):
104     path = Cnf["Dir::Root"] + tree + "/"
105     for name in files:
106         hashvalue = ""
107         hashlen = 0
108         try:
109             if name[0] == "<":
110                 j = name.index("/")
111                 k = name.index(">")
112                 (cat, ext, name) = (name[1:j], name[j+1:k], name[k+1:])
113                 file_handle = decompressors[ cat ]( "%s%s%s" % (path, name, ext) )
114                 contents = file_handle.read()
115                 hashvalue = hashop(contents)
116                 hashlen = len(contents)
117             else:
118                 try:
119                     file_handle = utils.open_file(path + name)
120                     hashvalue = hashop(file_handle)
121                     hashlen = os.stat(path + name).st_size
122                 except:
123                     raise
124                 else:
125                     if file_handle:
126                         file_handle.close()
127
128         except CantOpenError:
129             print "ALERT: Couldn't open " + path + name
130         else:
131             out.write(" %s %8d %s\n" % (hashvalue, hashlen, name))
132
133 def print_md5_files (tree, files):
134     print_md5sha_files (tree, files, apt_pkg.md5sum)
135
136 def print_sha1_files (tree, files):
137     print_md5sha_files (tree, files, apt_pkg.sha1sum)
138
139 def print_sha256_files (tree, files):
140     print_md5sha_files (tree, files, apt_pkg.sha256sum)
141
142 def write_release_file (relpath, suite, component, origin, label, arch, version="", suite_suffix="", notautomatic=""):
143     try:
144         if os.access(relpath, os.F_OK):
145             if os.stat(relpath).st_nlink > 1:
146                 os.unlink(relpath)
147         release = open(relpath, "w")
148     except IOError:
149         utils.fubar("Couldn't write to " + relpath)
150
151     release.write("Archive: %s\n" % (suite))
152     if version != "":
153         release.write("Version: %s\n" % (version))
154
155     if suite_suffix:
156         release.write("Component: %s/%s\n" % (suite_suffix,component))
157     else:
158         release.write("Component: %s\n" % (component))
159
160     release.write("Origin: %s\n" % (origin))
161     release.write("Label: %s\n" % (label))
162     if notautomatic != "":
163         release.write("NotAutomatic: %s\n" % (notautomatic))
164     release.write("Architecture: %s\n" % (arch))
165     release.close()
166
167 ################################################################################
168
169 def main ():
170     global Cnf, AptCnf, out
171     out = sys.stdout
172
173     Cnf = utils.get_conf()
174
175     Arguments = [('h',"help","Generate-Releases::Options::Help"),
176                  ('a',"apt-conf","Generate-Releases::Options::Apt-Conf", "HasArg"),
177                  ('f',"force-touch","Generate-Releases::Options::Force-Touch"),
178                 ]
179     for i in [ "help", "apt-conf", "force-touch" ]:
180         if not Cnf.has_key("Generate-Releases::Options::%s" % (i)):
181             Cnf["Generate-Releases::Options::%s" % (i)] = ""
182
183     suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
184     Options = Cnf.SubTree("Generate-Releases::Options")
185
186     if Options["Help"]:
187         usage()
188
189     if not Options["Apt-Conf"]:
190         Options["Apt-Conf"] = utils.which_apt_conf_file()
191
192     AptCnf = apt_pkg.newConfiguration()
193     apt_pkg.ReadConfigFileISC(AptCnf, Options["Apt-Conf"])
194
195     if not suites:
196         suites = Cnf.SubTree("Suite").List()
197
198     for suitename in suites:
199         print "Processing: " + suitename
200         SuiteBlock = Cnf.SubTree("Suite::" + suite)
201         suiteobj = get_suite(suitename)
202
203         suite = suite.suite_name.lower()
204
205         if suite.untouchable and not Options["Force-Touch"]:
206             print "Skipping: " + suite + " (untouchable)"
207             continue
208
209         origin = suite.origin
210         label = suite.label or suite.origin
211         codename = suite.codename or ""
212         version = ""
213         if suite.version and suite.version != '-':
214             version = suite.version
215         description = suite.description or ""
216
217         architectures = get_suite_architectures(suite, skipall=True, skipsrc=True)
218
219         if SuiteBlock.has_key("NotAutomatic"):
220             notautomatic = "yes"
221         else:
222             notautomatic = ""
223
224         if SuiteBlock.has_key("Components"):
225             components = SuiteBlock.ValueList("Components")
226         else:
227             components = []
228
229         suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
230         if components and suite_suffix:
231             longsuite = suite + "/" + suite_suffix
232         else:
233             longsuite = suite
234
235         tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
236
237         if AptCnf.has_key("tree::%s" % (tree)):
238             pass
239         elif AptCnf.has_key("bindirectory::%s" % (tree)):
240             pass
241         else:
242             aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
243             print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
244             continue
245
246         print Cnf["Dir::Root"] + tree + "/Release"
247         out = open(Cnf["Dir::Root"] + tree + "/Release", "w")
248
249         out.write("Origin: %s\n" % (suiteobj.origin))
250         out.write("Label: %s\n" % (label))
251         out.write("Suite: %s\n" % (suite))
252         if version != "":
253             out.write("Version: %s\n" % (version))
254         if codename != "":
255             out.write("Codename: %s\n" % (codename))
256         out.write("Date: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()))))
257
258         if SuiteBlock.has_key("ValidTime"):
259             validtime=float(SuiteBlock["ValidTime"])
260             out.write("Valid-Until: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()+validtime))))
261
262         if notautomatic != "":
263             out.write("NotAutomatic: %s\n" % (notautomatic))
264         out.write("Architectures: %s\n" % (" ".join([a.arch_string for a in architectures])))
265         if components:
266             out.write("Components: %s\n" % (" ".join(components)))
267
268         if description:
269             out.write("Description: %s\n" % (description))
270
271         files = []
272
273         if AptCnf.has_key("tree::%s" % (tree)):
274             if AptCnf.has_key("tree::%s::Contents" % (tree)):
275                 pass
276             else:
277                 for x in os.listdir("%s/%s" % (Cnf["Dir::Root"], tree)):
278                     if x.startswith('Contents-'):
279                         if x.endswith('.diff'):
280                             files.append("%s/Index" % (x))
281                         else:
282                             files.append(x)
283
284             for sec in AptCnf["tree::%s::Sections" % (tree)].split():
285                 for arch in AptCnf["tree::%s::Architectures" % (tree)].split():
286                     if arch == "source":
287                         filepath = "%s/%s/Sources" % (sec, arch)
288                         for cfile in compressnames("tree::%s" % (tree), "Sources", filepath):
289                             files.append(cfile)
290                         add_tiffani(files, Cnf["Dir::Root"] + tree, filepath)
291                     else:
292                         disks = "%s/disks-%s" % (sec, arch)
293                         diskspath = Cnf["Dir::Root"]+tree+"/"+disks
294                         if os.path.exists(diskspath):
295                             for dir in os.listdir(diskspath):
296                                 if os.path.exists("%s/%s/md5sum.txt" % (diskspath, dir)):
297                                     files.append("%s/%s/md5sum.txt" % (disks, dir))
298
299                         filepath = "%s/binary-%s/Packages" % (sec, arch)
300                         for cfile in compressnames("tree::%s" % (tree), "Packages", filepath):
301                             files.append(cfile)
302                         add_tiffani(files, Cnf["Dir::Root"] + tree, filepath)
303
304                     if arch == "source":
305                         rel = "%s/%s/Release" % (sec, arch)
306                     else:
307                         rel = "%s/binary-%s/Release" % (sec, arch)
308                     relpath = Cnf["Dir::Root"]+tree+"/"+rel
309                     write_release_file(relpath, suite, sec, origin, label, arch, version, suite_suffix, notautomatic)
310                     files.append(rel)
311                 gen_i18n_index(files, tree, sec)
312
313             if AptCnf.has_key("tree::%s/main" % (tree)):
314                 for dis in ["main", "contrib", "non-free"]:
315                     if not AptCnf.has_key("tree::%s/%s" % (tree, dis)): continue
316                     sec = AptCnf["tree::%s/%s::Sections" % (tree,dis)].split()[0]
317                     if sec != "debian-installer":
318                         print "ALERT: weird non debian-installer section in %s" % (tree)
319
320                     for arch in AptCnf["tree::%s/%s::Architectures" % (tree,dis)].split():
321                         if arch != "source":  # always true
322                             rel = "%s/%s/binary-%s/Release" % (dis, sec, arch)
323                             relpath = Cnf["Dir::Root"]+tree+"/"+rel
324                             write_release_file(relpath, suite, dis, origin, label, arch, version, suite_suffix, notautomatic)
325                             files.append(rel)
326                             for cfile in compressnames("tree::%s/%s" % (tree,dis),
327                                 "Packages",
328                                 "%s/%s/binary-%s/Packages" % (dis, sec, arch)):
329                                 files.append(cfile)
330             elif AptCnf.has_key("tree::%s::FakeDI" % (tree)):
331                 usetree = AptCnf["tree::%s::FakeDI" % (tree)]
332                 sec = AptCnf["tree::%s/main::Sections" % (usetree)].split()[0]
333                 if sec != "debian-installer":
334                     print "ALERT: weird non debian-installer section in %s" % (usetree)
335
336                 for arch in AptCnf["tree::%s/main::Architectures" % (usetree)].split():
337                     if arch != "source":  # always true
338                         for cfile in compressnames("tree::%s/main" % (usetree), "Packages", "main/%s/binary-%s/Packages" % (sec, arch)):
339                             files.append(cfile)
340
341         elif AptCnf.has_key("bindirectory::%s" % (tree)):
342             for cfile in compressnames("bindirectory::%s" % (tree), "Packages", AptCnf["bindirectory::%s::Packages" % (tree)]):
343                 files.append(cfile.replace(tree+"/","",1))
344             for cfile in compressnames("bindirectory::%s" % (tree), "Sources", AptCnf["bindirectory::%s::Sources" % (tree)]):
345                 files.append(cfile.replace(tree+"/","",1))
346         else:
347             print "ALERT: no tree/bindirectory for %s" % (tree)
348
349         out.write("MD5Sum:\n")
350         print_md5_files(tree, files)
351         out.write("SHA1:\n")
352         print_sha1_files(tree, files)
353         out.write("SHA256:\n")
354         print_sha256_files(tree, files)
355
356         out.close()
357         if Cnf.has_key("Dinstall::SigningKeyring"):
358             keyring = "--secret-keyring \"%s\"" % Cnf["Dinstall::SigningKeyring"]
359             if Cnf.has_key("Dinstall::SigningPubKeyring"):
360                 keyring += " --keyring \"%s\"" % Cnf["Dinstall::SigningPubKeyring"]
361
362             arguments = "--no-options --batch --no-tty --armour"
363             if Cnf.has_key("Dinstall::SigningKeyIds"):
364                 signkeyids = Cnf["Dinstall::SigningKeyIds"].split()
365             else:
366                 signkeyids = [""]
367
368             dest = Cnf["Dir::Root"] + tree + "/Release.gpg"
369             if os.path.exists(dest):
370                 os.unlink(dest)
371
372             for keyid in signkeyids:
373                 if keyid != "": defkeyid = "--default-key %s" % keyid
374                 else: defkeyid = ""
375                 os.system("gpg %s %s %s --detach-sign <%s >>%s" %
376                         (keyring, defkeyid, arguments,
377                         Cnf["Dir::Root"] + tree + "/Release", dest))
378
379 #######################################################################################
380
381 if __name__ == '__main__':
382     main()