]> git.decadent.org.uk Git - dak.git/blob - dak/generate_releases.py
oops DEcompress, dummy
[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, pg
26 import gzip, bz2
27 import apt_pkg
28 from daklib import utils
29 from daklib.dak_exceptions import *
30
31 ################################################################################
32
33 Cnf = None
34 projectB = None
35 out = None
36 AptCnf = None
37
38 ################################################################################
39
40 def usage (exit_code=0):
41     print """Usage: dak generate-releases [OPTION]... [SUITE]...
42 Generate Release files (for SUITE).
43
44   -h, --help                 show this help and exit
45   -a, --apt-conf FILE        use FILE instead of default apt.conf
46   -f, --force-touch          ignore Untouchable directives in dak.conf
47
48 If no SUITE is given Release files are generated for all suites."""
49
50     sys.exit(exit_code)
51
52 ################################################################################
53
54 def add_tiffani (files, path, indexstem):
55     index = "%s.diff/Index" % (indexstem)
56     filepath = "%s/%s" % (path, index)
57     if os.path.exists(filepath):
58         #print "ALERT: there was a tiffani file %s" % (filepath)
59         files.append(index)
60
61 def compressnames (tree,type,file):
62     compress = AptCnf.get("%s::%s::Compress" % (tree,type), AptCnf.get("Default::%s::Compress" % (type), ". gzip"))
63     result = []
64     cl = compress.split()
65     uncompress = ("." not in cl)
66     for mode in compress.split():
67         if mode == ".":
68             result.append(file)
69         elif mode == "gzip":
70             if uncompress:
71                 result.append("<zcat/.gz>" + file)
72                 uncompress = 0
73             result.append(file + ".gz")
74         elif mode == "bzip2":
75             if uncompress:
76                 result.append("<bzcat/.bz2>" + file)
77                 uncompress = 0
78             result.append(file + ".bz2")
79     return result
80
81 decompressors = { 'zcat' : gzip.GzipFile,
82                   'bzip2' : bz2.BZ2File }
83
84 def print_md5sha_files (tree, files, hashop):
85     path = Cnf["Dir::Root"] + tree + "/"
86     for name in files:
87         try:
88             if name[0] == "<":
89                 j = name.index("/")
90                 k = name.index(">")
91                 (cat, ext, name) = (name[1:j], name[j+1:k], name[k+1:])
92                 file_handle = decompressors[ cat ]( "%s%s%s" % (path, name, ext) )
93                 contents = file_handle.read()
94             else:
95                 try:
96                     file_handle = utils.open_file(path + name)
97                     contents = file_handle.read()
98                 except:
99                     raise
100                 else:
101                     if file_handle:
102                         file_handle.close()
103
104         except CantOpenError:
105             print "ALERT: Couldn't open " + path + name
106         else:
107             hash = hashop(contents)
108             out.write(" %s %8d %s\n" % (hash, len(contents), name))
109
110 def print_md5_files (tree, files):
111     print_md5sha_files (tree, files, apt_pkg.md5sum)
112
113 def print_sha1_files (tree, files):
114     print_md5sha_files (tree, files, apt_pkg.sha1sum)
115
116 def print_sha256_files (tree, files):
117     print_md5sha_files (tree, files, apt_pkg.sha256sum)
118
119 ################################################################################
120
121 def main ():
122     global Cnf, AptCnf, projectB, out
123     out = sys.stdout
124
125     Cnf = utils.get_conf()
126
127     Arguments = [('h',"help","Generate-Releases::Options::Help"),
128                  ('a',"apt-conf","Generate-Releases::Options::Apt-Conf", "HasArg"),
129                  ('f',"force-touch","Generate-Releases::Options::Force-Touch"),
130                 ]
131     for i in [ "help", "apt-conf", "force-touch" ]:
132         if not Cnf.has_key("Generate-Releases::Options::%s" % (i)):
133             Cnf["Generate-Releases::Options::%s" % (i)] = ""
134
135     suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
136     Options = Cnf.SubTree("Generate-Releases::Options")
137
138     if Options["Help"]:
139         usage()
140
141     if not Options["Apt-Conf"]:
142         Options["Apt-Conf"] = utils.which_apt_conf_file()
143
144     AptCnf = apt_pkg.newConfiguration()
145     apt_pkg.ReadConfigFileISC(AptCnf, Options["Apt-Conf"])
146
147     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
148
149     if not suites:
150         suites = Cnf.SubTree("Suite").List()
151
152     for suite in suites:
153         print "Processing: " + suite
154         SuiteBlock = Cnf.SubTree("Suite::" + suite)
155
156         if SuiteBlock.has_key("Untouchable") and not Options["Force-Touch"]:
157             print "Skipping: " + suite + " (untouchable)"
158             continue
159
160         suite = suite.lower()
161
162         origin = SuiteBlock["Origin"]
163         label = SuiteBlock.get("Label", origin)
164         codename = SuiteBlock.get("CodeName", "")
165
166         version = ""
167         description = ""
168
169         q = projectB.query("SELECT version, description FROM suite WHERE suite_name = '%s'" % (suite))
170         qs = q.getresult()
171         if len(qs) == 1:
172             if qs[0][0] != "-": version = qs[0][0]
173             if qs[0][1]: description = qs[0][1]
174
175         if SuiteBlock.has_key("NotAutomatic"):
176             notautomatic = "yes"
177         else:
178             notautomatic = ""
179
180         if SuiteBlock.has_key("Components"):
181             components = SuiteBlock.ValueList("Components")
182         else:
183             components = []
184
185         suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
186         if components and suite_suffix:
187             longsuite = suite + "/" + suite_suffix
188         else:
189             longsuite = suite
190
191         tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
192
193         if AptCnf.has_key("tree::%s" % (tree)):
194             pass
195         elif AptCnf.has_key("bindirectory::%s" % (tree)):
196             pass
197         else:
198             aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
199             print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
200             continue
201
202         print Cnf["Dir::Root"] + tree + "/Release"
203         out = open(Cnf["Dir::Root"] + tree + "/Release", "w")
204
205         out.write("Origin: %s\n" % (origin))
206         out.write("Label: %s\n" % (label))
207         out.write("Suite: %s\n" % (suite))
208         if version != "":
209             out.write("Version: %s\n" % (version))
210         if codename != "":
211             out.write("Codename: %s\n" % (codename))
212         out.write("Date: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()))))
213
214         if SuiteBlock.has_key("ValidTime"):
215             validtime=float(SuiteBlock["ValidTime"])
216             out.write("Valid-Until: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()+validtime))))
217
218         if notautomatic != "":
219             out.write("NotAutomatic: %s\n" % (notautomatic))
220         out.write("Architectures: %s\n" % (" ".join(filter(utils.real_arch, SuiteBlock.ValueList("Architectures")))))
221         if components:
222             out.write("Components: %s\n" % (" ".join(components)))
223
224         if description:
225             out.write("Description: %s\n" % (description))
226
227         files = []
228
229         if AptCnf.has_key("tree::%s" % (tree)):
230             for sec in AptCnf["tree::%s::Sections" % (tree)].split():
231                 for arch in AptCnf["tree::%s::Architectures" % (tree)].split():
232                     if arch == "source":
233                         filepath = "%s/%s/Sources" % (sec, arch)
234                         for file in compressnames("tree::%s" % (tree), "Sources", filepath):
235                             files.append(file)
236                         add_tiffani(files, Cnf["Dir::Root"] + tree, filepath)
237                     else:
238                         disks = "%s/disks-%s" % (sec, arch)
239                         diskspath = Cnf["Dir::Root"]+tree+"/"+disks
240                         if os.path.exists(diskspath):
241                             for dir in os.listdir(diskspath):
242                                 if os.path.exists("%s/%s/md5sum.txt" % (diskspath, dir)):
243                                     files.append("%s/%s/md5sum.txt" % (disks, dir))
244
245                         filepath = "%s/binary-%s/Packages" % (sec, arch)
246                         for file in compressnames("tree::%s" % (tree), "Packages", filepath):
247                             files.append(file)
248                         add_tiffani(files, Cnf["Dir::Root"] + tree, filepath)
249
250                     if arch == "source":
251                         rel = "%s/%s/Release" % (sec, arch)
252                     else:
253                         rel = "%s/binary-%s/Release" % (sec, arch)
254                     relpath = Cnf["Dir::Root"]+tree+"/"+rel
255
256                     try:
257                         if os.access(relpath, os.F_OK):
258                             if os.stat(relpath).st_nlink > 1:
259                                 os.unlink(relpath)
260                         release = open(relpath, "w")
261                         #release = open(longsuite.replace("/","_") + "_" + arch + "_" + sec + "_Release", "w")
262                     except IOError:
263                         utils.fubar("Couldn't write to " + relpath)
264
265                     release.write("Archive: %s\n" % (suite))
266                     if version != "":
267                         release.write("Version: %s\n" % (version))
268                     if suite_suffix:
269                         release.write("Component: %s/%s\n" % (suite_suffix,sec))
270                     else:
271                         release.write("Component: %s\n" % (sec))
272                     release.write("Origin: %s\n" % (origin))
273                     release.write("Label: %s\n" % (label))
274                     if notautomatic != "":
275                         release.write("NotAutomatic: %s\n" % (notautomatic))
276                     release.write("Architecture: %s\n" % (arch))
277                     release.close()
278                     files.append(rel)
279
280             if AptCnf.has_key("tree::%s/main" % (tree)):
281                 for dis in ["main", "contrib", "non-free"]:
282                     if not AptCnf.has_key("tree::%s/%s" % (tree, dis)): continue
283                     sec = AptCnf["tree::%s/%s::Sections" % (tree,dis)].split()[0]
284                     if sec != "debian-installer":
285                         print "ALERT: weird non debian-installer section in %s" % (tree)
286
287                     for arch in AptCnf["tree::%s/%s::Architectures" % (tree,dis)].split():
288                         if arch != "source":  # always true
289                             for file in compressnames("tree::%s/%s" % (tree,dis),
290                                 "Packages",
291                                 "%s/%s/binary-%s/Packages" % (dis, sec, arch)):
292                                 files.append(file)
293             elif AptCnf.has_key("tree::%s::FakeDI" % (tree)):
294                 usetree = AptCnf["tree::%s::FakeDI" % (tree)]
295                 sec = AptCnf["tree::%s/main::Sections" % (usetree)].split()[0]
296                 if sec != "debian-installer":
297                     print "ALERT: weird non debian-installer section in %s" % (usetree)
298
299                 for arch in AptCnf["tree::%s/main::Architectures" % (usetree)].split():
300                     if arch != "source":  # always true
301                         for file in compressnames("tree::%s/main" % (usetree), "Packages", "main/%s/binary-%s/Packages" % (sec, arch)):
302                             files.append(file)
303
304         elif AptCnf.has_key("bindirectory::%s" % (tree)):
305             for file in compressnames("bindirectory::%s" % (tree), "Packages", AptCnf["bindirectory::%s::Packages" % (tree)]):
306                 files.append(file.replace(tree+"/","",1))
307             for file in compressnames("bindirectory::%s" % (tree), "Sources", AptCnf["bindirectory::%s::Sources" % (tree)]):
308                 files.append(file.replace(tree+"/","",1))
309         else:
310             print "ALERT: no tree/bindirectory for %s" % (tree)
311
312         out.write("MD5Sum:\n")
313         print_md5_files(tree, files)
314         out.write("SHA1:\n")
315         print_sha1_files(tree, files)
316         out.write("SHA256:\n")
317         print_sha256_files(tree, files)
318
319         out.close()
320         if Cnf.has_key("Dinstall::SigningKeyring"):
321             keyring = "--secret-keyring \"%s\"" % Cnf["Dinstall::SigningKeyring"]
322             if Cnf.has_key("Dinstall::SigningPubKeyring"):
323                 keyring += " --keyring \"%s\"" % Cnf["Dinstall::SigningPubKeyring"]
324
325             arguments = "--no-options --batch --no-tty --armour"
326             if Cnf.has_key("Dinstall::SigningKeyIds"):
327                 signkeyids = Cnf["Dinstall::SigningKeyIds"].split()
328             else:
329                 signkeyids = [""]
330
331             dest = Cnf["Dir::Root"] + tree + "/Release.gpg"
332             if os.path.exists(dest):
333                 os.unlink(dest)
334
335             for keyid in signkeyids:
336                 if keyid != "": defkeyid = "--default-key %s" % keyid
337                 else: defkeyid = ""
338                 os.system("gpg %s %s %s --detach-sign <%s >>%s" %
339                         (keyring, defkeyid, arguments,
340                         Cnf["Dir::Root"] + tree + "/Release", dest))
341
342 #######################################################################################
343
344 if __name__ == '__main__':
345     main()