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