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