]> git.decadent.org.uk Git - dak.git/blob - ziyi
Use SuiteSuffix to simplify code. Always write lower level release files and die...
[dak.git] / ziyi
1 #!/usr/bin/env python
2
3 # Create all the Release files
4
5 # Copyright (C) 2001, 2002  Anthony Towns <ajt@debian.org>
6 # $Id: ziyi,v 1.22 2002-06-08 00:18:47 troup Exp $
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 #   ``Bored now''
23
24 ################################################################################
25
26 import sys, os, popen2, tempfile, stat, string, time
27 import utils
28 import apt_pkg
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: ziyi [OPTION]
41 Generate Release files.
42
43   -h, --help                 show this help and exit"""
44
45     sys.exit(exit_code)
46
47 ################################################################################
48
49 def compressnames (tree,type,file):
50     compress = AptCnf.get("%s::%s::Compress" % (tree,type), AptCnf.get("Default::%s::Compress" % (type), ". gzip"))
51     result = []
52     cl = string.split(compress)
53     uncompress = ("." not in cl)
54     for mode in string.split(compress):
55         if mode == ".":
56             result.append(file)
57         elif mode == "gzip":
58             if uncompress:
59                 result.append("<zcat/.gz>" + file)
60                 uncompress = 0
61             result.append(file + ".gz")
62         elif mode == "bzip2":
63             if uncompress:
64                 result.append("<bzcat/.bz2>" + file)
65                 uncompress = 0
66             result.append(file + ".bz2")
67     return result
68
69 def create_temp_file (cmd):
70     f = tempfile.TemporaryFile()
71     r = popen2.popen2(cmd)
72     r[1].close()
73     r = r[0]
74     size = 0
75     while 1:
76         x = r.readline()
77         if not x:
78             r.close()
79             del x,r
80             break
81         f.write(x)
82         size = size + len(x)
83     f.flush()
84     f.seek(0)
85     return (size, f)
86
87 def print_md5sha_files (tree, files, hashop):
88     path = Cnf["Dir::Root"] + tree + "/"
89     for name in files:
90         try:
91             if name[0] == "<":
92                 j = string.index(name, "/")
93                 k = string.index(name, ">")
94                 (cat, ext, name) = (name[1:j], name[j+1:k], name[k+1:])
95                 (size, file_handle) = create_temp_file("%s %s%s%s" %
96                     (cat, path, name, ext))
97             else:
98                 size = os.stat(path + name)[stat.ST_SIZE]
99                 file_handle = utils.open_file(path + name)
100         except utils.cant_open_exc:
101             print "ALERT: Couldn't open " + path + name
102         else:
103             hash = hashop(file_handle)
104             file_handle.close()
105             out.write(" %s         %8d %s\n" % (hash, size, name))
106
107 def print_md5_files (tree, files):
108     print_md5sha_files (tree, files, apt_pkg.md5sum)
109
110 def print_sha1_files (tree, files):
111     print_md5sha_files (tree, files, apt_pkg.sha1sum)
112
113 ################################################################################
114
115 def main ():
116     global Cnf, AptCnf, projectB, out
117     out = sys.stdout;
118
119     Cnf = utils.get_conf()
120
121     AptCnf = apt_pkg.newConfiguration()
122     apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
123
124     Arguments = [('h',"help","Ziyi::Options::Help")];
125     for i in [ "help" ]:
126         if not Cnf.has_key("Ziyi::Options::%s" % (i)):
127             Cnf["Ziyi::Options::%s" % (i)] = "";
128
129     suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
130     Options = Cnf.SubTree("Ziyi::Options")
131
132     if Options["Help"]:
133         usage();
134
135     if not suites:
136         suites = Cnf.SubTree("Suite").List()
137
138     for suite in suites:
139         print "Processing: " + suite
140         SuiteBlock = Cnf.SubTree("Suite::" + suite)
141
142         if SuiteBlock.has_key("Untouchable"):
143             print "Skipping: " + suite + " (untouchable)"
144             continue
145
146         suite = string.lower(suite)
147
148         origin = SuiteBlock["Origin"]
149         label = SuiteBlock.get("Label", origin)
150         version = SuiteBlock.get("Version", "")
151         codename = SuiteBlock.get("CodeName", "")
152
153         if SuiteBlock.has_key("NotAutomatic"):
154             notautomatic = "yes"
155         else:
156             notautomatic = ""
157
158         if SuiteBlock.has_key("Components"):
159             components = SuiteBlock.ValueList("Components")
160         else:
161             components = []
162
163         suite_suffix = Cnf.Find("Dinstall::SuiteSuffix");
164         if components and suite_suffix:
165             longsuite = suite + "/" + suite_suffix;
166         else:
167             longsuite = suite;
168
169         tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
170
171         if AptCnf.has_key("tree::%s" % (tree)):
172             pass
173         elif AptCnf.has_key("bindirectory::%s" % (tree)):
174             pass
175         else:
176             print "ALERT: suite %s not in apt.conf, nor untouchable!" % (suite)
177             continue
178
179         print Cnf["Dir::Root"] + tree + "/Release"
180         out = open(Cnf["Dir::Root"] + tree + "/Release", "w")
181
182         out.write("Origin: %s\n" % (origin))
183         out.write("Label: %s\n" % (label))
184         out.write("Suite: %s\n" % (suite))
185         if version != "":
186             out.write("Version: %s\n" % (version))
187         if codename != "":
188             out.write("Codename: %s\n" % (codename))
189         out.write("Date: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()))))
190         if notautomatic != "":
191             out.write("NotAutomatic: %s\n" % (notautomatic))
192         out.write("Architectures: %s\n" % (string.join(filter(utils.real_arch, SuiteBlock.ValueList("Architectures")))))
193         if components:
194             out.write("Components: %s\n" % (string.join(components)))
195
196         out.write("Description: %s\n" % (SuiteBlock["Description"]))
197
198         files = []
199
200         if AptCnf.has_key("tree::%s" % (tree)):
201             for sec in string.split(AptCnf["tree::%s::Sections" % (tree)]):
202                 for arch in string.split(AptCnf["tree::%s::Architectures" % (tree)]):
203                     if arch == "source":
204                         for file in compressnames("tree::%s" % (tree), "Sources", "%s/%s/Sources" % (sec, arch)):
205                             files.append(file)
206                     else:
207                         disks = "%s/disks-%s" % (sec, arch)
208                         diskspath = Cnf["Dir::Root"]+tree+"/"+disks
209                         if os.path.exists(diskspath):
210                             for dir in os.listdir(diskspath):
211                                 if os.path.exists("%s/%s/md5sum.txt" % (diskspath, dir)):
212                                     files.append("%s/%s/md5sum.txt" % (disks, dir))
213
214                         for file in compressnames("tree::%s" % (tree), "Packages", "%s/binary-%s/Packages" % (sec, arch)):
215                             files.append(file)
216
217                     if arch == "source":
218                         rel = "%s/%s/Release" % (sec, arch)
219                     else:
220                         rel = "%s/binary-%s/Release" % (sec, arch)
221                     relpath = Cnf["Dir::Root"]+tree+"/"+rel
222
223                     try:
224                         release = open(relpath, "w")
225                         #release = open(string.replace(longsuite,"/","_") + "_" + arch + "_" + sec + "_Release", "w")
226                     except IOError:
227                         utils.fubar("Couldn't write to " + relpath);
228
229                     release.write("Archive: %s\n" % (suite))
230                     if version != "":
231                         release.write("Version: %s\n" % (version))
232                     if suite_suffix:
233                         release.write("Component: %s/%s\n" % (suite_suffix,sec));
234                     else:
235                         release.write("Component: %s\n" % (sec));
236                     release.write("Origin: %s\n" % (origin))
237                     release.write("Label: %s\n" % (label))
238                     if notautomatic != "":
239                         release.write("NotAutomatic: %s\n" % (notautomatic))
240                     release.write("Architecture: %s\n" % (arch))
241                     release.close()
242                     files.append(rel)
243
244             if AptCnf.has_key("tree::%s/main" % (tree)):
245                 sec = string.split(AptCnf["tree::%s/main::Sections" % (tree)])[0]
246                 if sec != "debian-installer":
247                     print "ALERT: weird non debian-installer section in %s" % (tree)
248
249                 for arch in string.split(AptCnf["tree::%s/main::Architectures" % (tree)]):
250                     if arch != "source":  # always true
251                         for file in compressnames("tree::%s/main" % (tree), "Packages", "main/%s/binary-%s/Packages" % (sec, arch)):
252                             files.append(file)
253
254         elif AptCnf.has_key("bindirectory::%s" % (tree)):
255             for file in compressnames("bindirectory::%s" % (tree), "Packages", AptCnf["bindirectory::%s::Packages" % (tree)]):
256                 files.append(string.replace(file,tree+"/","",1))
257             for file in compressnames("bindirectory::%s" % (tree), "Sources", AptCnf["bindirectory::%s::Sources" % (tree)]):
258                 files.append(string.replace(file,tree+"/","",1))
259         else:
260             print "ALERT: no tree/bindirectory for %s" % (tree)
261
262         out.write("MD5Sum:\n")
263         print_md5_files(tree, files)
264         out.write("SHA1:\n")
265         print_sha1_files(tree, files)
266
267         out.close()
268         if Cnf.has_key("Dinstall::SigningKeyring"):
269             keyring = "--secret-keyring \"%s\"" % Cnf["Dinstall::SigningKeyring"]
270             if Cnf.has_key("Dinstall::SigningPubKeyring"):
271                 keyring = keyring + " --keyring \"%s\"" % Cnf["Dinstall::SigningPubKeyring"]
272
273             arguments = "--no-options --batch --no-tty --armour"
274             if Cnf.has_key("Dinstall::SigningKeyIds"):
275                 signkeyids = string.split(Cnf["Dinstall::SigningKeyIds"])
276             else:
277                 signkeyids = [""]
278
279             dest = Cnf["Dir::Root"] + tree + "/Release.gpg"
280             if os.path.exists(dest):
281                 os.unlink(dest)
282
283             for keyid in signkeyids:
284                 if keyid != "": defkeyid = "--default-key %s" % keyid
285                 else: defkeyid = ""
286                 os.system("gpg %s %s %s --detach-sign <%s >>%s" %
287                         (keyring, defkeyid, arguments,
288                         Cnf["Dir::Root"] + tree + "/Release", dest))
289
290 #######################################################################################
291
292 if __name__ == '__main__':
293     main()
294