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-6, 6 Anthony Towns <aj@azure.humbug.org.au>
10 # Copyright (C) 2004-5 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
37 ################################################################################
44 ################################################################################
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
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)
62 print "warning: removing of %s denied" % (file)
65 for ext in ["", ".gz", ".bz2"]:
66 if os.path.isfile(file + ext):
67 return (ext, os.stat(file + ext))
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))
78 print "missing: %s" % (f)
82 if os.path.isfile(file):
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"))
101 def __init__(self, readpath = None, max = 14):
104 self.history_order = []
106 self.readpath = readpath
107 self.filesizesha1 = None
111 f = open(readpath + "/Index")
114 def read_hashs(ind, f, self, x=x):
117 if not x or x[0] != " ": break
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]))
132 if l[0] == "SHA1-History:":
133 x = read_hashs(0,f,self)
136 if l[0] == "SHA1-Patches:":
137 x = read_hashs(1,f,self)
140 if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
143 if l[0] == "SHA1-Current:" and len(l) == 3:
144 self.filesizesha1 = (l[1], int(l[2]))
151 def dump(self, out=sys.stdout):
153 out.write("Canonical-Path: %s\n" % (self.can_path))
155 if self.filesizesha1:
156 out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
159 l = self.history_order[:]
163 for h in l[:cnt-self.max]:
164 tryunlink("%s/%s.gz" % (self.readpath, h))
167 self.history_order = l[:]
169 out.write("SHA1-History:\n")
171 out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
172 out.write("SHA1-Patches:\n")
174 out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
176 def create_temp_file(r):
177 f = tempfile.TemporaryFile()
189 size = os.fstat(f.fileno())[6]
191 sha1sum = apt_pkg.sha1sum(f)
192 return (sha1sum, size)
194 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
195 if Options.has_key("NoAct"):
196 print "not doing anything"
199 patchname = Options["PatchName"]
201 # origfile = /path/to/Packages
202 # oldfile = ./Packages
203 # newfile = ./Packages.tmp
204 # difffile = outdir/patchname
205 # index => outdir/Index
207 # (outdir, oldfile, origfile) = argv
209 newfile = oldfile + ".new"
210 difffile = "%s/%s" % (outdir, patchname)
212 upd = Updates(outdir, int(maxdiffs))
213 (oldext, oldstat) = smartstat(oldfile)
214 (origext, origstat) = smartstat(origfile)
216 print "%s doesn't exist" % (origfile)
220 os.link(origfile + origext, oldfile + origext)
223 if oldstat[1:3] == origstat[1:3]:
224 print "hardlink unbroken, assuming unchanged"
227 oldf = smartopen(oldfile)
228 oldsizesha1 = sizesha1(oldf)
230 # should probably early exit if either of these checks fail
231 # alternatively (optionally?) could just trim the patch history
234 if upd.filesizesha1 != oldsizesha1:
235 print "old file seems to have changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
237 # XXX this should be usable now
239 #for d in upd.history.keys():
240 # df = smartopen("%s/%s" % (outdir,d))
241 # act_sha1size = sizesha1(df)
243 # exp_sha1size = upd.history[d][1]
244 # if act_sha1size != exp_sha1size:
245 # print "patch file %s seems to have changed! %s %s => %s %s" % \
246 # (d,) + exp_sha1size + act_sha1size
248 if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
250 if os.path.exists(newfile): os.unlink(newfile)
251 smartlink(origfile, newfile)
252 newf = open(newfile, "r")
253 newsizesha1 = sizesha1(newf)
256 if newsizesha1 == oldsizesha1:
259 print "file unchanged, not generating diff"
261 if not os.path.isdir(outdir): os.mkdir(outdir)
262 print "generating diff"
263 w = os.popen("diff --ed - %s | gzip -c -9 > %s.gz" %
264 (newfile, difffile), "w")
268 difff = smartopen(difffile)
269 difsizesha1 = sizesha1(difff)
272 upd.history[patchname] = (oldsizesha1, difsizesha1)
273 upd.history_order.append(patchname)
275 upd.filesizesha1 = newsizesha1
277 os.unlink(oldfile + oldext)
278 os.link(origfile + origext, oldfile + origext)
281 f = open(outdir + "/Index", "w")
287 global Cnf, Options, Logger
291 Cnf = dak.lib.utils.get_conf()
292 Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
293 ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
294 ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
295 ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
296 ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
297 ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
298 ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
300 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
301 Options = Cnf.SubTree("Generate-Index-Diffs::Options")
302 if Options.has_key("Help"): usage()
304 maxdiffs = Options.get("MaxDiffs::Default", "14")
305 maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
306 maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
307 maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
309 if not Options.has_key("PatchName"):
310 format = "%Y-%m-%d-%H%M.%S"
311 i,o = os.popen2("date +%s" % (format))
313 Options["PatchName"] = o.readline()[:-1]
316 AptCnf = apt_pkg.newConfiguration()
317 apt_pkg.ReadConfigFileISC(AptCnf,dak.lib.utils.which_apt_conf_file())
319 if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
322 suites = Cnf.SubTree("Suite").List()
325 if suite == "Experimental": continue
327 print "Processing: " + suite
328 SuiteBlock = Cnf.SubTree("Suite::" + suite)
330 if SuiteBlock.has_key("Untouchable"):
331 print "Skipping: " + suite + " (untouchable)"
334 suite = suite.lower()
336 architectures = SuiteBlock.ValueList("Architectures")
338 if SuiteBlock.has_key("Components"):
339 components = SuiteBlock.ValueList("Components")
343 suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
344 if components and suite_suffix:
345 longsuite = suite + "/" + suite_suffix
349 tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
351 if AptCnf.has_key("tree::%s" % (tree)):
352 sections = AptCnf["tree::%s::Sections" % (tree)].split()
353 elif AptCnf.has_key("bindirectory::%s" % (tree)):
354 sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
356 aptcnf_filename = os.path.basename(dak.lib.utils.which_apt_conf_file())
357 print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
360 for architecture in architectures:
361 if architecture == "all":
364 if architecture != "source":
366 file = "%s/Contents-%s" % (Cnf["Dir::Root"] + tree,
368 storename = "%s/%s_contents_%s" % (Options["TempDir"], suite, architecture)
369 print "running contents for %s %s : " % (suite, architecture),
370 genchanges(Options, file + ".diff", storename, file, \
371 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::Contents" % (suite), maxcontents))
373 # use sections instead of components since dak.conf
374 # treats "foo/bar main" as suite "foo", suitesuffix "bar" and
375 # component "bar/main". suck.
377 for component in sections:
378 if architecture == "source":
379 longarch = architecture
381 maxsuite = maxsources
383 longarch = "binary-%s"% (architecture)
384 packages = "Packages"
385 maxsuite = maxpackages
387 file = "%s/%s/%s/%s" % (Cnf["Dir::Root"] + tree,
388 component, longarch, packages)
389 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
390 print "running for %s %s %s : " % (suite, component, architecture),
391 genchanges(Options, file + ".diff", storename, file, \
392 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::%s" % (suite, packages), maxsuite))
394 ################################################################################
396 if __name__ == '__main__':