]> git.decadent.org.uk Git - dak.git/blob - dak/generate_index_diffs.py
untouchable
[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 subprocess
38 import time
39 import apt_pkg
40 import pg
41 from daklib import utils
42 from daklib import database
43
44 ################################################################################
45
46 projectB = None
47 Cnf = None
48 Logger = None
49 Options = None
50
51 ################################################################################
52
53 def usage (exit_code=0):
54     print """Usage: dak generate-index-diffs [OPTIONS] [suites]
55 Write out ed-style diffs to Packages/Source lists
56
57   -h, --help            show this help and exit
58   -c                    give the canonical path of the file
59   -p                    name for the patch (defaults to current time)
60   -n                    take no action
61     """
62     sys.exit(exit_code)
63
64
65 def tryunlink(file):
66     try:
67         os.unlink(file)
68     except OSError:
69         print "warning: removing of %s denied" % (file)
70
71 def smartstat(file):
72     for ext in ["", ".gz", ".bz2"]:
73         if os.path.isfile(file + ext):
74             return (ext, os.stat(file + ext))
75     return (None, None)
76
77 def smartlink(f, t):
78     if os.path.isfile(f):
79         os.link(f,t)
80     elif os.path.isfile("%s.gz" % (f)):
81         os.system("gzip -d < %s.gz > %s" % (f, t))
82     elif os.path.isfile("%s.bz2" % (f)):
83         os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
84     else:
85         print "missing: %s" % (f)
86         raise IOError, f
87
88 def smartopen(file):
89     if os.path.isfile(file):
90         f = open(file, "r")
91     elif os.path.isfile("%s.gz" % file):
92         f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
93     elif os.path.isfile("%s.bz2" % file):
94         f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
95     else:
96         f = None
97     return f
98
99 def pipe_file(f, t):
100     f.seek(0)
101     while 1:
102         l = f.read()
103         if not l: break
104         t.write(l)
105     t.close()
106
107 class Updates:
108     def __init__(self, readpath = None, max = 14):
109         self.can_path = None
110         self.history = {}
111         self.history_order = []
112         self.max = max
113         self.readpath = readpath
114         self.filesizesha1 = None
115
116         if readpath:
117             try:
118                 f = open(readpath + "/Index")
119                 x = f.readline()
120
121                 def read_hashs(ind, f, self, x=x):
122                     while 1:
123                         x = f.readline()
124                         if not x or x[0] != " ": break
125                         l = x.split()
126                         if not self.history.has_key(l[2]):
127                             self.history[l[2]] = [None,None]
128                             self.history_order.append(l[2])
129                         self.history[l[2]][ind] = (l[0], int(l[1]))
130                     return x
131
132                 while x:
133                     l = x.split()
134
135                     if len(l) == 0:
136                         x = f.readline()
137                         continue
138
139                     if l[0] == "SHA1-History:":
140                         x = read_hashs(0,f,self)
141                         continue
142
143                     if l[0] == "SHA1-Patches:":
144                         x = read_hashs(1,f,self)
145                         continue
146
147                     if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
148                         self.can_path = l[1]
149
150                     if l[0] == "SHA1-Current:" and len(l) == 3:
151                         self.filesizesha1 = (l[1], int(l[2]))
152
153                     x = f.readline()
154
155             except IOError:
156                 0
157
158     def dump(self, out=sys.stdout):
159         if self.can_path:
160             out.write("Canonical-Path: %s\n" % (self.can_path))
161
162         if self.filesizesha1:
163             out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
164
165         hs = self.history
166         l = self.history_order[:]
167
168         cnt = len(l)
169         if cnt > self.max:
170             for h in l[:cnt-self.max]:
171                 tryunlink("%s/%s.gz" % (self.readpath, h))
172                 del hs[h]
173             l = l[cnt-self.max:]
174             self.history_order = l[:]
175
176         out.write("SHA1-History:\n")
177         for h in l:
178             out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
179         out.write("SHA1-Patches:\n")
180         for h in l:
181             out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
182
183 def create_temp_file(r):
184     f = tempfile.TemporaryFile()
185     while 1:
186         x = r.readline()
187         if not x: break
188         f.write(x)
189     r.close()
190     del x,r
191     f.flush()
192     f.seek(0)
193     return f
194
195 def sizesha1(f):
196     size = os.fstat(f.fileno())[6]
197     f.seek(0)
198     sha1sum = apt_pkg.sha1sum(f)
199     return (sha1sum, size)
200
201 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
202     if Options.has_key("NoAct"):
203         return
204
205     patchname = Options["PatchName"]
206
207     # origfile = /path/to/Packages
208     # oldfile  = ./Packages
209     # newfile  = ./Packages.tmp
210     # difffile = outdir/patchname
211     # index   => outdir/Index
212
213     # (outdir, oldfile, origfile) = argv
214
215     newfile = oldfile + ".new"
216     difffile = "%s/%s" % (outdir, patchname)
217
218     upd = Updates(outdir, int(maxdiffs))
219     (oldext, oldstat) = smartstat(oldfile)
220     (origext, origstat) = smartstat(origfile)
221     if not origstat:
222         print "%s: doesn't exist" % (origfile)
223         return
224     if not oldstat:
225         print "%s: initial run" % (origfile)
226         os.link(origfile + origext, oldfile + origext)
227         return
228
229     if oldstat[1:3] == origstat[1:3]:
230         print "%s: hardlink unbroken, assuming unchanged" % (origfile)
231         return
232
233     oldf = smartopen(oldfile)
234     oldsizesha1 = sizesha1(oldf)
235
236     # should probably early exit if either of these checks fail
237     # alternatively (optionally?) could just trim the patch history
238
239     if upd.filesizesha1:
240         if upd.filesizesha1 != oldsizesha1:
241             print "info: old file " + oldfile + " changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
242
243     if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
244
245     if os.path.exists(newfile): os.unlink(newfile)
246     smartlink(origfile, newfile)
247     newf = open(newfile, "r")
248     newsizesha1 = sizesha1(newf)
249     newf.close()
250
251     if newsizesha1 == oldsizesha1:
252         os.unlink(newfile)
253         oldf.close()
254         print "%s: unchanged" % (origfile)
255     else:
256         if not os.path.isdir(outdir):
257             os.mkdir(outdir)
258
259         w = os.popen("diff --ed - %s | gzip -c -9 > %s.gz" %
260                      (newfile, difffile), "w")
261         pipe_file(oldf, w)
262         oldf.close()
263
264         difff = smartopen(difffile)
265         difsizesha1 = sizesha1(difff)
266         difff.close()
267
268         upd.history[patchname] = (oldsizesha1, difsizesha1)
269         upd.history_order.append(patchname)
270
271         upd.filesizesha1 = newsizesha1
272
273         os.unlink(oldfile + oldext)
274         os.link(origfile + origext, oldfile + origext)
275         os.unlink(newfile)
276
277         f = open(outdir + "/Index", "w")
278         upd.dump(f)
279         f.close()
280
281
282 def main():
283     global Cnf, Options, Logger, projectB
284
285     os.umask(0002)
286
287     Cnf = utils.get_conf()
288     Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
289                   ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
290                   ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
291                   ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
292                   ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
293                   ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
294                   ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
295                 ]
296     suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
297     Options = Cnf.SubTree("Generate-Index-Diffs::Options")
298     if Options.has_key("Help"): usage()
299
300     maxdiffs = Options.get("MaxDiffs::Default", "14")
301     maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
302     maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
303     maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
304
305     if not Options.has_key("PatchName"):
306         format = "%Y-%m-%d-%H%M.%S"
307         Options["PatchName"] = time.strftime( format )
308
309     AptCnf = apt_pkg.newConfiguration()
310     apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
311
312     if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
313
314     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
315     database.init(Cnf, projectB)
316
317     if not suites:
318         suites = Cnf.SubTree("Suite").List()
319
320     for suite in suites:
321         print "Processing: " + suite
322         SuiteBlock = Cnf.SubTree("Suite::" + suite)
323
324         if database.get_suite_untouchable(suite)
325             print "Skipping: " + suite + " (untouchable)"
326             continue
327
328         suite = suite.lower()
329
330         architectures = database.get_suite_architectures(suite)
331         if architectures == None:
332             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()