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