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