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