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