]> git.decadent.org.uk Git - dak.git/blob - dak/generate_index_diffs.py
Merge commit 'godog/master' into merge
[dak.git] / dak / generate_index_diffs.py
1 #!/usr/bin/env python
2
3 ###########################################################
4 # generates partial package updates list
5
6 # idea and basic implementation by Anthony, some changes by Andreas
7 # parts are stolen from 'dak generate-releases'
8 #
9 # Copyright (C) 2004, 2005, 2006  Anthony Towns <aj@azure.humbug.org.au>
10 # Copyright (C) 2004, 2005  Andreas Barth <aba@not.so.argh.org>
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26
27 # < elmo> bah, don't bother me with annoying facts
28 # < elmo> I was on a roll
29
30
31 ################################################################################
32
33 import sys, os, tempfile
34 import apt_pkg
35 from daklib import utils
36
37 ################################################################################
38
39 projectB = None
40 Cnf = None
41 Logger = None
42 Options = None
43
44 ################################################################################
45
46 def usage (exit_code=0):
47     print """Usage: dak generate-index-diffs [OPTIONS] [suites]
48 Write out ed-style diffs to Packages/Source lists
49
50   -h, --help            show this help and exit
51   -c                    give the canonical path of the file
52   -p                    name for the patch (defaults to current time)
53   -n                    take no action
54     """
55     sys.exit(exit_code)
56
57
58 def tryunlink(file):
59     try:
60         os.unlink(file)
61     except OSError:
62         print "warning: removing of %s denied" % (file)
63
64 def smartstat(file):
65     for ext in ["", ".gz", ".bz2"]:
66         if os.path.isfile(file + ext):
67             return (ext, os.stat(file + ext))
68     return (None, None)
69
70 def smartlink(f, t):
71     if os.path.isfile(f):
72         os.link(f,t)
73     elif os.path.isfile("%s.gz" % (f)):
74         os.system("gzip -d < %s.gz > %s" % (f, t))
75     elif os.path.isfile("%s.bz2" % (f)):
76         os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
77     else:
78         print "missing: %s" % (f)
79         raise IOError, f
80
81 def smartopen(file):
82     if os.path.isfile(file):
83         f = open(file, "r")
84     elif os.path.isfile("%s.gz" % file):
85         f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
86     elif os.path.isfile("%s.bz2" % file):
87         f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
88     else:
89         f = None
90     return f
91
92 def pipe_file(f, t):
93     f.seek(0)
94     while 1:
95         l = f.read()
96         if not l: break
97         t.write(l)
98     t.close()
99
100 class Updates:
101     def __init__(self, readpath = None, max = 14):
102         self.can_path = None
103         self.history = {}
104         self.history_order = []
105         self.max = max
106         self.readpath = readpath
107         self.filesizesha1 = None
108
109         if readpath:
110             try:
111                 f = open(readpath + "/Index")
112                 x = f.readline()
113
114                 def read_hashs(ind, f, self, x=x):
115                     while 1:
116                         x = f.readline()
117                         if not x or x[0] != " ": break
118                         l = x.split()
119                         if not self.history.has_key(l[2]):
120                             self.history[l[2]] = [None,None]
121                             self.history_order.append(l[2])
122                         self.history[l[2]][ind] = (l[0], int(l[1]))
123                     return x
124
125                 while x:
126                     l = x.split()
127
128                     if len(l) == 0:
129                         x = f.readline()
130                         continue
131
132                     if l[0] == "SHA1-History:":
133                         x = read_hashs(0,f,self)
134                         continue
135
136                     if l[0] == "SHA1-Patches:":
137                         x = read_hashs(1,f,self)
138                         continue
139
140                     if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
141                         self.can_path = l[1]
142
143                     if l[0] == "SHA1-Current:" and len(l) == 3:
144                         self.filesizesha1 = (l[1], int(l[2]))
145
146                     x = f.readline()
147
148             except IOError:
149                 0
150
151     def dump(self, out=sys.stdout):
152         if self.can_path:
153             out.write("Canonical-Path: %s\n" % (self.can_path))
154
155         if self.filesizesha1:
156             out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
157
158         hs = self.history
159         l = self.history_order[:]
160
161         cnt = len(l)
162         if cnt > self.max:
163             for h in l[:cnt-self.max]:
164                 tryunlink("%s/%s.gz" % (self.readpath, h))
165                 del hs[h]
166             l = l[cnt-self.max:]
167             self.history_order = l[:]
168
169         out.write("SHA1-History:\n")
170         for h in l:
171             out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
172         out.write("SHA1-Patches:\n")
173         for h in l:
174             out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
175
176 def create_temp_file(r):
177     f = tempfile.TemporaryFile()
178     while 1:
179         x = r.readline()
180         if not x: break
181         f.write(x)
182     r.close()
183     del x,r
184     f.flush()
185     f.seek(0)
186     return f
187
188 def sizesha1(f):
189     size = os.fstat(f.fileno())[6]
190     f.seek(0)
191     sha1sum = apt_pkg.sha1sum(f)
192     return (sha1sum, size)
193
194 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
195     if Options.has_key("NoAct"):
196         return
197
198     patchname = Options["PatchName"]
199
200     # origfile = /path/to/Packages
201     # oldfile  = ./Packages
202     # newfile  = ./Packages.tmp
203     # difffile = outdir/patchname
204     # index   => outdir/Index
205
206     # (outdir, oldfile, origfile) = argv
207
208     newfile = oldfile + ".new"
209     difffile = "%s/%s" % (outdir, patchname)
210
211     upd = Updates(outdir, int(maxdiffs))
212     (oldext, oldstat) = smartstat(oldfile)
213     (origext, origstat) = smartstat(origfile)
214     if not origstat:
215         print "%s: doesn't exist" % (origfile)
216         return
217     if not oldstat:
218         print "%s: initial run" % (origfile)
219         os.link(origfile + origext, oldfile + origext)
220         return
221
222     if oldstat[1:3] == origstat[1:3]:
223         print "%s: hardlink unbroken, assuming unchanged" % (origfile)
224         return
225
226     oldf = smartopen(oldfile)
227     oldsizesha1 = sizesha1(oldf)
228
229     # should probably early exit if either of these checks fail
230     # alternatively (optionally?) could just trim the patch history
231
232     if upd.filesizesha1:
233         if upd.filesizesha1 != oldsizesha1:
234             print "warning: old file seems to have changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
235
236     # XXX this should be usable now
237     #
238     #for d in upd.history.keys():
239     #    df = smartopen("%s/%s" % (outdir,d))
240     #    act_sha1size = sizesha1(df)
241     #    df.close()
242     #    exp_sha1size = upd.history[d][1]
243     #    if act_sha1size != exp_sha1size:
244     #        print "patch file %s seems to have changed! %s %s => %s %s" % \
245     #            (d,) + exp_sha1size + act_sha1size
246
247     if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
248
249     if os.path.exists(newfile): os.unlink(newfile)
250     smartlink(origfile, newfile)
251     newf = open(newfile, "r")
252     newsizesha1 = sizesha1(newf)
253     newf.close()
254
255     if newsizesha1 == oldsizesha1:
256         os.unlink(newfile)
257         oldf.close()
258         print "%s: unchanged" % (origfile)
259     else:
260         if not os.path.isdir(outdir): os.mkdir(outdir)
261         w = os.popen("diff --ed - %s | gzip -c -9 > %s.gz" %
262                          (newfile, difffile), "w")
263         pipe_file(oldf, w)
264         oldf.close()
265
266         difff = smartopen(difffile)
267         difsizesha1 = sizesha1(difff)
268         difff.close()
269
270         upd.history[patchname] = (oldsizesha1, difsizesha1)
271         upd.history_order.append(patchname)
272
273         upd.filesizesha1 = newsizesha1
274
275         os.unlink(oldfile + oldext)
276         os.link(origfile + origext, oldfile + origext)
277         os.unlink(newfile)
278
279         f = open(outdir + "/Index", "w")
280         upd.dump(f)
281         f.close()
282
283
284 def main():
285     global Cnf, Options, Logger
286
287     os.umask(0002)
288
289     Cnf = utils.get_conf()
290     Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
291                   ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
292                   ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
293                   ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
294                   ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
295                   ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
296                   ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
297                 ]
298     suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
299     Options = Cnf.SubTree("Generate-Index-Diffs::Options")
300     if Options.has_key("Help"): usage()
301
302     maxdiffs = Options.get("MaxDiffs::Default", "14")
303     maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
304     maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
305     maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
306
307     if not Options.has_key("PatchName"):
308         format = "%Y-%m-%d-%H%M.%S"
309         i,o = os.popen2("date +%s" % (format))
310         i.close()
311         Options["PatchName"] = o.readline()[:-1]
312         o.close()
313
314     AptCnf = apt_pkg.newConfiguration()
315     apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
316
317     if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
318
319     if not suites:
320         suites = Cnf.SubTree("Suite").List()
321
322     for suite in suites:
323         print "Processing: " + suite
324         SuiteBlock = Cnf.SubTree("Suite::" + suite)
325
326         if SuiteBlock.has_key("Untouchable"):
327             print "Skipping: " + suite + " (untouchable)"
328             continue
329
330         suite = suite.lower()
331
332         architectures = SuiteBlock.ValueList("Architectures")
333
334         if SuiteBlock.has_key("Components"):
335             components = SuiteBlock.ValueList("Components")
336         else:
337             components = []
338
339         suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
340         if components and suite_suffix:
341             longsuite = suite + "/" + suite_suffix
342         else:
343             longsuite = suite
344
345         tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
346
347         if AptCnf.has_key("tree::%s" % (tree)):
348             sections = AptCnf["tree::%s::Sections" % (tree)].split()
349         elif AptCnf.has_key("bindirectory::%s" % (tree)):
350             sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
351         else:
352             aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
353             print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
354             continue
355
356         for architecture in architectures:
357             if architecture == "all":
358                 continue
359
360             if architecture != "source":
361                 # Process Contents
362                 file = "%s/Contents-%s" % (Cnf["Dir::Root"] + tree,
363                         architecture)
364                 storename = "%s/%s_contents_%s" % (Options["TempDir"], suite, architecture)
365                 genchanges(Options, file + ".diff", storename, file, \
366                   Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::Contents" % (suite), maxcontents))
367
368             # use sections instead of components since dak.conf
369             # treats "foo/bar main" as suite "foo", suitesuffix "bar" and
370             # component "bar/main". suck.
371
372             for component in sections:
373                 if architecture == "source":
374                     longarch = architecture
375                     packages = "Sources"
376                     maxsuite = maxsources
377                 else:
378                     longarch = "binary-%s"% (architecture)
379                     packages = "Packages"
380                     maxsuite = maxpackages
381
382                 file = "%s/%s/%s/%s" % (Cnf["Dir::Root"] + tree,
383                            component, longarch, packages)
384                 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
385                 genchanges(Options, file + ".diff", storename, file, \
386                   Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::%s" % (suite, packages), maxsuite))
387
388 ################################################################################
389
390 if __name__ == '__main__':
391     main()