]> git.decadent.org.uk Git - dak.git/blob - ziyi
pre-define globals
[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.21 2002-05-18 23:54:59 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     def real_arch(x):
139         return x != "source" and x != "all"
140
141     for suite in suites:
142         print "Processing: " + suite
143         SuiteBlock = Cnf.SubTree("Suite::" + suite)
144
145         if SuiteBlock.has_key("Untouchable"):
146             print "Skipping: " + suite + " (untouchable)"
147             continue
148
149         suite = string.lower(suite)
150
151         origin = SuiteBlock["Origin"]
152         label = SuiteBlock.get("Label", origin)
153         version = SuiteBlock.get("Version", "")
154         codename = SuiteBlock.get("CodeName", "")
155
156         if SuiteBlock.has_key("NotAutomatic"):
157             notautomatic = "yes"
158         else:
159             notautomatic = ""
160
161         if SuiteBlock.has_key("Components"):
162             components = SuiteBlock.ValueList("Components")
163         else:
164             components = []
165
166         nonus = 1
167         if components:
168             for c in components:
169                 if string.find(c, "non-US/") != 0:
170                     nonus = 0
171         else:
172             nonus = 0
173
174         if not nonus and  string.find(codename, "/updates") > 0:
175             security = 1
176         else:
177             security = 0
178
179         if nonus:
180             longsuite = suite + "/non-US"
181         elif security:
182             suite = suite + "/updates"
183             longsuite = suite
184         else:
185             longsuite = suite
186
187         tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
188
189         if AptCnf.has_key("tree::%s" % (tree)):
190             pass
191         elif AptCnf.has_key("bindirectory::%s" % (tree)):
192             pass
193         else:
194             print "ALERT: suite %s not in apt.conf, nor untouchable!" % (suite)
195             continue
196
197         print Cnf["Dir::Root"] + tree + "/Release"
198         out = open(Cnf["Dir::Root"] + tree + "/Release", "w")
199
200         out.write("Origin: %s\n" % (origin))
201         out.write("Label: %s\n" % (label))
202         out.write("Suite: %s\n" % (suite))
203         if version != "":
204             out.write("Version: %s\n" % (version))
205         if codename != "":
206             out.write("Codename: %s\n" % (codename))
207         out.write("Date: %s\n" % (time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time()))))
208         if notautomatic != "":
209             out.write("NotAutomatic: %s\n" % (notautomatic))
210         out.write("Architectures: %s\n" % (string.join(filter(real_arch, SuiteBlock.ValueList("Architectures")))))
211         if components:
212             out.write("Components: %s\n" % (string.join(components)))
213
214         out.write("Description: %s\n" % (SuiteBlock["Description"]))
215
216         files = []
217
218         if AptCnf.has_key("tree::%s" % (tree)):
219             for sec in string.split(AptCnf["tree::%s::Sections" % (tree)]):
220                 for arch in string.split(AptCnf["tree::%s::Architectures" % (tree)]):
221                     if arch == "source":
222                         for file in compressnames("tree::%s" % (tree), "Sources", "%s/%s/Sources" % (sec, arch)):
223                             files.append(file)
224                     else:
225                         disks = "%s/disks-%s" % (sec, arch)
226                         diskspath = Cnf["Dir::Root"]+tree+"/"+disks
227                         if os.path.exists(diskspath):
228                             for dir in os.listdir(diskspath):
229                                 if os.path.exists("%s/%s/md5sum.txt" % (diskspath, dir)):
230                                     files.append("%s/%s/md5sum.txt" % (disks, dir))
231
232                         for file in compressnames("tree::%s" % (tree), "Packages", "%s/binary-%s/Packages" % (sec, arch)):
233                             files.append(file)
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                     if os.path.exists(relpath):
242                         try:
243                             os.unlink(relpath)
244                             release = open(relpath, "w")
245                             #release = open(string.replace(longsuite,"/","_") + "_" + arch + "_" + sec + "_Release", "w")
246                         except IOError:
247                             print "Couldn't write to " + relpath
248                         else:
249                             release.write("Archive: %s\n" % (suite))
250                             if version != "":
251                                 release.write("Version: %s\n" % (version))
252                             if nonus:
253                                 release.write("Component: non-US/%s\n" % (sec))
254                             else:
255                                 release.write("Component: %s\n" % (sec))
256                             release.write("Origin: %s\n" % (origin))
257                             release.write("Label: %s\n" % (label))
258                             if notautomatic != "":
259                                 release.write("NotAutomatic: %s\n" % (notautomatic))
260                             release.write("Architecture: %s\n" % (arch))
261                             release.close()
262                             files.append(rel)
263
264             if AptCnf.has_key("tree::%s/main" % (tree)):
265                 sec = string.split(AptCnf["tree::%s/main::Sections" % (tree)])[0]
266                 if sec != "debian-installer":
267                     print "ALERT: weird non debian-installer section in %s" % (tree)
268
269                 for arch in string.split(AptCnf["tree::%s/main::Architectures" % (tree)]):
270                     if arch != "source":  # always true
271                         for file in compressnames("tree::%s/main" % (tree), "Packages", "main/%s/binary-%s/Packages" % (sec, arch)):
272                             files.append(file)
273
274         elif AptCnf.has_key("bindirectory::%s" % (tree)):
275             for file in compressnames("bindirectory::%s" % (tree), "Packages", AptCnf["bindirectory::%s::Packages" % (tree)]):
276                 files.append(string.replace(file,tree+"/","",1))
277             for file in compressnames("bindirectory::%s" % (tree), "Sources", AptCnf["bindirectory::%s::Sources" % (tree)]):
278                 files.append(string.replace(file,tree+"/","",1))
279         else:
280             print "ALERT: no tree/bindirectory for %s" % (tree)
281
282         out.write("MD5Sum:\n")
283         print_md5_files(tree, files)
284         out.write("SHA1:\n")
285         print_sha1_files(tree, files)
286
287         out.close()
288         if Cnf.has_key("Dinstall::SigningKeyring"):
289             keyring = "--secret-keyring \"%s\"" % Cnf["Dinstall::SigningKeyring"]
290             if Cnf.has_key("Dinstall::SigningPubKeyring"):
291                 keyring = keyring + " --keyring \"%s\"" % Cnf["Dinstall::SigningPubKeyring"]
292
293             arguments = "--no-options --batch --no-tty --armour"
294             if Cnf.has_key("Dinstall::SigningKeyIds"):
295                 signkeyids = string.split(Cnf["Dinstall::SigningKeyIds"])
296             else:
297                 signkeyids = [""]
298
299             dest = Cnf["Dir::Root"] + tree + "/Release.gpg"
300             if os.path.exists(dest):
301                 os.unlink(dest)
302
303             for keyid in signkeyids:
304                 if keyid != "": defkeyid = "--default-key %s" % keyid
305                 else: defkeyid = ""
306                 os.system("gpg %s %s %s --detach-sign <%s >>%s" %
307                         (keyring, defkeyid, arguments,
308                         Cnf["Dir::Root"] + tree + "/Release", dest))
309
310 #######################################################################################
311
312 if __name__ == '__main__':
313     main()
314