]> git.decadent.org.uk Git - dak.git/blob - dak/generate_index_diffs.py
queued: remove buildd-alpha-keyring.gpg
[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 import glob
40
41 from daklib import utils
42 from daklib.dbconn import Archive, Component, DBConn, Suite, get_suite, get_suite_architectures
43 #from daklib.regexes import re_includeinpdiff
44 import re
45 re_includeinpdiff = re.compile(r"(Translation-[a-zA-Z_]+\.(?:bz2|xz))")
46
47 ################################################################################
48
49 Cnf = None
50 Logger = None
51 Options = None
52
53 ################################################################################
54
55 def usage (exit_code=0):
56     print """Usage: dak generate-index-diffs [OPTIONS] [suites]
57 Write out ed-style diffs to Packages/Source lists
58
59   -h, --help            show this help and exit
60   -a <archive>          generate diffs for suites in <archive>
61   -c                    give the canonical path of the file
62   -p                    name for the patch (defaults to current time)
63   -d                    name for the hardlink farm for status
64   -m                    how many diffs to generate
65   -n                    take no action
66     """
67     sys.exit(exit_code)
68
69 def tryunlink(file):
70     try:
71         os.unlink(file)
72     except OSError:
73         print "warning: removing of %s denied" % (file)
74
75 def smartstat(file):
76     for ext in ["", ".gz", ".bz2"]:
77         if os.path.isfile(file + ext):
78             return (ext, os.stat(file + ext))
79     return (None, None)
80
81 def smartlink(f, t):
82     if os.path.isfile(f):
83         os.link(f,t)
84     elif os.path.isfile("%s.gz" % (f)):
85         os.system("gzip -d < %s.gz > %s" % (f, t))
86     elif os.path.isfile("%s.bz2" % (f)):
87         os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
88     else:
89         print "missing: %s" % (f)
90         raise IOError(f)
91
92 def smartopen(file):
93     if os.path.isfile(file):
94         f = open(file, "r")
95     elif os.path.isfile("%s.gz" % file):
96         f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
97     elif os.path.isfile("%s.bz2" % file):
98         f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
99     else:
100         f = None
101     return f
102
103 def pipe_file(f, t):
104     f.seek(0)
105     while 1:
106         l = f.read()
107         if not l: break
108         t.write(l)
109     t.close()
110
111 class Updates:
112     def __init__(self, readpath = None, max = 56):
113         self.can_path = None
114         self.history = {}
115         self.history_order = []
116         self.max = max
117         self.readpath = readpath
118         self.filesizesha1 = None
119
120         if readpath:
121             try:
122                 f = open(readpath + "/Index")
123                 x = f.readline()
124
125                 def read_hashs(ind, f, self, x=x):
126                     while 1:
127                         x = f.readline()
128                         if not x or x[0] != " ": break
129                         l = x.split()
130                         if not self.history.has_key(l[2]):
131                             self.history[l[2]] = [None,None]
132                             self.history_order.append(l[2])
133                         self.history[l[2]][ind] = (l[0], int(l[1]))
134                     return x
135
136                 while x:
137                     l = x.split()
138
139                     if len(l) == 0:
140                         x = f.readline()
141                         continue
142
143                     if l[0] == "SHA1-History:":
144                         x = read_hashs(0,f,self)
145                         continue
146
147                     if l[0] == "SHA1-Patches:":
148                         x = read_hashs(1,f,self)
149                         continue
150
151                     if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
152                         self.can_path = l[1]
153
154                     if l[0] == "SHA1-Current:" and len(l) == 3:
155                         self.filesizesha1 = (l[1], int(l[2]))
156
157                     x = f.readline()
158
159             except IOError:
160                 0
161
162     def dump(self, out=sys.stdout):
163         if self.can_path:
164             out.write("Canonical-Path: %s\n" % (self.can_path))
165
166         if self.filesizesha1:
167             out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
168
169         hs = self.history
170         l = self.history_order[:]
171
172         cnt = len(l)
173         if cnt > self.max:
174             for h in l[:cnt-self.max]:
175                 tryunlink("%s/%s.gz" % (self.readpath, h))
176                 del hs[h]
177             l = l[cnt-self.max:]
178             self.history_order = l[:]
179
180         out.write("SHA1-History:\n")
181         for h in l:
182             out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
183         out.write("SHA1-Patches:\n")
184         for h in l:
185             out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
186
187 def create_temp_file(r):
188     f = tempfile.TemporaryFile()
189     while 1:
190         x = r.readline()
191         if not x: break
192         f.write(x)
193     r.close()
194     del x,r
195     f.flush()
196     f.seek(0)
197     return f
198
199 def sizesha1(f):
200     size = os.fstat(f.fileno())[6]
201     f.seek(0)
202     sha1sum = apt_pkg.sha1sum(f)
203     return (sha1sum, size)
204
205 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 56):
206     if Options.has_key("NoAct"):
207         print "Not acting on: od: %s, oldf: %s, origf: %s, md: %s" % (outdir, oldfile, origfile, maxdiffs)
208         return
209
210     patchname = Options["PatchName"]
211
212     # origfile = /path/to/Packages
213     # oldfile  = ./Packages
214     # newfile  = ./Packages.tmp
215     # difffile = outdir/patchname
216     # index   => outdir/Index
217
218     # (outdir, oldfile, origfile) = argv
219
220     newfile = oldfile + ".new"
221     difffile = "%s/%s" % (outdir, patchname)
222
223     upd = Updates(outdir, int(maxdiffs))
224     (oldext, oldstat) = smartstat(oldfile)
225     (origext, origstat) = smartstat(origfile)
226     if not origstat:
227         print "%s: doesn't exist" % (origfile)
228         return
229     if not oldstat:
230         print "%s: initial run" % (origfile)
231         os.link(origfile + origext, oldfile + origext)
232         return
233
234     if oldstat[1:3] == origstat[1:3]:
235         #print "%s: hardlink unbroken, assuming unchanged" % (origfile)
236         return
237
238     oldf = smartopen(oldfile)
239     oldsizesha1 = sizesha1(oldf)
240
241     # should probably early exit if either of these checks fail
242     # alternatively (optionally?) could just trim the patch history
243
244     #if upd.filesizesha1:
245     #    if upd.filesizesha1 != oldsizesha1:
246     #        print "info: old file " + oldfile + " changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
247
248     if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
249
250     if os.path.exists(newfile): os.unlink(newfile)
251     smartlink(origfile, newfile)
252     newf = open(newfile, "r")
253     newsizesha1 = sizesha1(newf)
254     newf.close()
255
256     if newsizesha1 == oldsizesha1:
257         os.unlink(newfile)
258         oldf.close()
259         #print "%s: unchanged" % (origfile)
260     else:
261         if not os.path.isdir(outdir):
262             os.mkdir(outdir)
263
264         w = os.popen("diff --ed - %s | gzip --rsyncable -c -9 > %s.gz" %
265                      (newfile, difffile), "w")
266         pipe_file(oldf, w)
267         oldf.close()
268
269         difff = smartopen(difffile)
270         difsizesha1 = sizesha1(difff)
271         difff.close()
272
273         upd.history[patchname] = (oldsizesha1, difsizesha1)
274         upd.history_order.append(patchname)
275
276         upd.filesizesha1 = newsizesha1
277
278         os.unlink(oldfile + oldext)
279         os.link(origfile + origext, oldfile + origext)
280         os.unlink(newfile)
281
282         f = open(outdir + "/Index", "w")
283         upd.dump(f)
284         f.close()
285
286
287 def main():
288     global Cnf, Options, Logger
289
290     os.umask(0o002)
291
292     Cnf = utils.get_conf()
293     Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
294                   ('a', 'archive', 'Generate-Index-Diffs::Options::Archive', 'hasArg'),
295                   ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
296                   ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
297                   ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
298                   ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
299                   ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
300                 ]
301     suites = apt_pkg.parse_commandline(Cnf,Arguments,sys.argv)
302     Options = Cnf.subtree("Generate-Index-Diffs::Options")
303     if Options.has_key("Help"): usage()
304
305     maxdiffs = Options.get("MaxDiffs::Default", "56")
306     maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
307     maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
308     maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
309
310     if not Options.has_key("PatchName"):
311         format = "%Y-%m-%d-%H%M.%S"
312         Options["PatchName"] = time.strftime( format )
313
314     session = DBConn().session()
315
316     if not suites:
317         query = session.query(Suite.suite_name)
318         if Options.get('Archive'):
319             query = query.join(Suite.archive).filter(Archive.archive_name == Options['Archive'])
320         suites = [ s.suite_name for s in query ]
321
322     for suitename in suites:
323         print "Processing: " + suitename
324
325         suiteobj = get_suite(suitename.lower(), session=session)
326
327         # Use the canonical version of the suite name
328         suite = suiteobj.suite_name
329
330         if suiteobj.untouchable:
331             print "Skipping: " + suite + " (untouchable)"
332             continue
333
334         architectures = get_suite_architectures(suite, skipall=True, session=session)
335         components = [ c.component_name for c in session.query(Component.component_name) ]
336
337         suite_suffix = Cnf.find("Dinstall::SuiteSuffix")
338         if components and suite_suffix:
339             longsuite = suite + "/" + suite_suffix
340         else:
341             longsuite = suite
342
343         tree = os.path.join(suiteobj.archive.path, 'dists', longsuite)
344
345         # See if there are Translations which might need a new pdiff
346         cwd = os.getcwd()
347         for component in components:
348             #print "DEBUG: Working on %s" % (component)
349             workpath=os.path.join(tree, component, "i18n")
350             if os.path.isdir(workpath):
351                 os.chdir(workpath)
352                 for dirpath, dirnames, filenames in os.walk(".", followlinks=True, topdown=True):
353                     for entry in filenames:
354                         if not re_includeinpdiff.match(entry):
355                             #print "EXCLUDING %s" % (entry)
356                             continue
357                         (fname, fext) = os.path.splitext(entry)
358                         processfile=os.path.join(workpath, fname)
359                         #print "Working: %s" % (processfile)
360                         storename="%s/%s_%s_%s" % (Options["TempDir"], suite, component, fname)
361                         #print "Storefile: %s" % (storename)
362                         genchanges(Options, processfile + ".diff", storename, processfile, maxdiffs)
363         os.chdir(cwd)
364
365         for archobj in architectures:
366             architecture = archobj.arch_string
367
368             for component in components:
369                 if architecture == "source":
370                     longarch = architecture
371                     packages = "Sources"
372                     maxsuite = maxsources
373                 else:
374                     longarch = "binary-%s"% (architecture)
375                     packages = "Packages"
376                     maxsuite = maxpackages
377                     # Process Contents
378                     file = "%s/%s/Contents-%s" % (tree, component, architecture)
379                     storename = "%s/%s_%s_contents_%s" % (Options["TempDir"], suite, component, architecture)
380                     genchanges(Options, file + ".diff", storename, file, maxcontents)
381
382                 file = "%s/%s/%s/%s" % (tree, component, longarch, packages)
383                 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
384                 genchanges(Options, file + ".diff", storename, file, maxsuite)
385
386 ################################################################################
387
388 if __name__ == '__main__':
389     main()