3 ###########################################################
4 # generates partial package updates list
6 # idea and basic implementation by Anthony, some changes by Andreas
7 # parts are stolen from 'dak generate-releases'
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>
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.
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.
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
27 # < elmo> bah, don't bother me with annoying facts
28 # < elmo> I was on a roll
31 ################################################################################
33 import sys, os, tempfile
35 import bz2, gzip, time
36 from daklib import utils
38 ################################################################################
45 ################################################################################
47 def usage (exit_code=0):
48 print """Usage: dak generate-index-diffs [OPTIONS] [suites]
49 Write out ed-style diffs to Packages/Source lists
51 -h, --help show this help and exit
52 -c give the canonical path of the file
53 -p name for the patch (defaults to current time)
63 print "warning: removing of %s denied" % (file)
66 for ext in ["", ".gz", ".bz2"]:
67 if os.path.isfile(file + ext):
68 return (ext, os.stat(file + ext))
74 elif os.path.isfile("%s.gz" % (f)):
75 os.system("gzip -d < %s.gz > %s" % (f, t))
76 elif os.path.isfile("%s.bz2" % (f)):
77 os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
79 print "missing: %s" % (f)
82 def smartread(filename):
84 If filename exists, slurp the contents into a string.
85 if filename.gz or filename.bz2 exists instead, decompress and slurp
86 It returns a tuple of (filename, filecontents)
88 actual_filename = None
90 if os.path.isfile(filename):
91 f = open(filename, "r")
92 elif os.path.isfile("%s.gz" % filename):
93 actual_filename = "%s.gz" % filename
94 f = decompressors['zcat'](actual_filename)
95 elif os.path.isfile("%s.bz2" % filename):
96 actual_filename = "%s.bz2" % filename
97 f = decompressors['bzcat'](actual_filename)
104 return (actual_filename, contents)
115 def __init__(self, readpath = None, max = 14):
118 self.history_order = []
120 self.readpath = readpath
121 self.filesizesha1 = None
125 f = open(readpath + "/Index")
128 def read_hashs(ind, f, self, x=x):
131 if not x or x[0] != " ": break
133 if not self.history.has_key(l[2]):
134 self.history[l[2]] = [None,None]
135 self.history_order.append(l[2])
136 self.history[l[2]][ind] = (l[0], int(l[1]))
146 if l[0] == "SHA1-History:":
147 x = read_hashs(0,f,self)
150 if l[0] == "SHA1-Patches:":
151 x = read_hashs(1,f,self)
154 if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
157 if l[0] == "SHA1-Current:" and len(l) == 3:
158 self.filesizesha1 = (l[1], int(l[2]))
165 def dump(self, out=sys.stdout):
167 out.write("Canonical-Path: %s\n" % (self.can_path))
169 if self.filesizesha1:
170 out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
173 l = self.history_order[:]
177 for h in l[:cnt-self.max]:
178 tryunlink("%s/%s.gz" % (self.readpath, h))
181 self.history_order = l[:]
183 out.write("SHA1-History:\n")
185 out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
186 out.write("SHA1-Patches:\n")
188 out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
190 decompressors = { 'zcat' : gzip.GzipFile,
191 'bzip2' : bz2.BZ2File }
195 given a string, return a tuple containing its (sha1sum, length)
197 return (apt_pkg.sha1sum(s), len( s ) )
200 size = os.fstat(f.fileno())[6]
202 sha1sum = apt_pkg.sha1sum(f)
203 return (sha1sum, size)
205 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
206 if Options.has_key("NoAct"):
209 patchname = Options["PatchName"]
211 # origfile = /path/to/Packages
212 # oldfile = ./Packages
213 # newfile = ./Packages.tmp
214 # difffile = outdir/patchname
215 # index => outdir/Index
217 # (outdir, oldfile, origfile) = argv
219 newfile = oldfile + ".new"
220 difffile = "%s/%s" % (outdir, patchname)
222 upd = Updates(outdir, int(maxdiffs))
223 (oldext, oldstat) = smartstat(oldfile)
224 (origext, origstat) = smartstat(origfile)
226 print "%s: doesn't exist" % (origfile)
229 print "%s: initial run" % (origfile)
230 os.link(origfile + origext, oldfile + origext)
233 if oldstat[1:3] == origstat[1:3]:
234 print "%s: hardlink unbroken, assuming unchanged" % (origfile)
237 (oldf,contents) = smartread(oldfile)
238 oldsizesha1 = sizesha1_str(contents)
240 # should probably early exit if either of these checks fail
241 # alternatively (optionally?) could just trim the patch history
244 if upd.filesizesha1 != oldsizesha1:
245 print "warning: old file seems to have changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
247 # XXX this should be usable now
248 # stew: whatever this is, it won't be usable now that i removed smartopen
250 #for d in upd.history.keys():
251 # df = smartopen("%s/%s" % (outdir,d))
252 # act_sha1size = sizesha1(df)
254 # exp_sha1size = upd.history[d][1]
255 # if act_sha1size != exp_sha1size:
256 # print "patch file %s seems to have changed! %s %s => %s %s" % \
257 # (d,) + exp_sha1size + act_sha1size
259 if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
261 if os.path.exists(newfile): os.unlink(newfile)
262 smartlink(origfile, newfile)
263 newf = open(newfile, "r")
264 newsizesha1 = sizesha1(newf)
267 if newsizesha1 == oldsizesha1:
270 print "%s: unchanged" % (origfile)
272 if not os.path.isdir(outdir): os.mkdir(outdir)
273 os.popen("diff --ed %s %s | gzip -c -9 > %s.gz" %
274 (oldf, newfile, difffile))
278 (oldf,contents) = smartread(difffile)
279 difsizesha1 = sizesha1_str(contents)
281 upd.history[patchname] = (oldsizesha1, difsizesha1)
282 upd.history_order.append(patchname)
284 upd.filesizesha1 = newsizesha1
286 os.unlink(oldfile + oldext)
287 os.link(origfile + origext, oldfile + origext)
290 f = open(outdir + "/Index", "w")
296 global Cnf, Options, Logger
300 Cnf = utils.get_conf()
301 Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
302 ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
303 ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
304 ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
305 ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
306 ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
307 ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
309 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
310 Options = Cnf.SubTree("Generate-Index-Diffs::Options")
311 if Options.has_key("Help"): usage()
313 maxdiffs = Options.get("MaxDiffs::Default", "14")
314 maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
315 maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
316 maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
318 if not Options.has_key("PatchName"):
319 format = "%Y-%m-%d-%H%M.%S"
320 Options["PatchName"] = time.strftime( format )
322 AptCnf = apt_pkg.newConfiguration()
323 apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
325 if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
328 suites = Cnf.SubTree("Suite").List()
331 print "Processing: " + suite
332 SuiteBlock = Cnf.SubTree("Suite::" + suite)
334 if SuiteBlock.has_key("Untouchable"):
335 print "Skipping: " + suite + " (untouchable)"
338 suite = suite.lower()
340 architectures = SuiteBlock.ValueList("Architectures")
342 if SuiteBlock.has_key("Components"):
343 components = SuiteBlock.ValueList("Components")
347 suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
348 if components and suite_suffix:
349 longsuite = suite + "/" + suite_suffix
353 tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
355 if AptCnf.has_key("tree::%s" % (tree)):
356 sections = AptCnf["tree::%s::Sections" % (tree)].split()
357 elif AptCnf.has_key("bindirectory::%s" % (tree)):
358 sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
360 aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
361 print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
364 for architecture in architectures:
365 if architecture == "all":
368 if architecture != "source":
370 file = "%s/Contents-%s" % (Cnf["Dir::Root"] + tree,
372 storename = "%s/%s_contents_%s" % (Options["TempDir"], suite, architecture)
373 genchanges(Options, file + ".diff", storename, file, \
374 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::Contents" % (suite), maxcontents))
376 # use sections instead of components since dak.conf
377 # treats "foo/bar main" as suite "foo", suitesuffix "bar" and
378 # component "bar/main". suck.
380 for component in sections:
381 if architecture == "source":
382 longarch = architecture
384 maxsuite = maxsources
386 longarch = "binary-%s"% (architecture)
387 packages = "Packages"
388 maxsuite = maxpackages
390 file = "%s/%s/%s/%s" % (Cnf["Dir::Root"] + tree,
391 component, longarch, packages)
392 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
393 genchanges(Options, file + ".diff", storename, file, \
394 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::%s" % (suite, packages), maxsuite))
396 ################################################################################
398 if __name__ == '__main__':