3 """ generates partial package updates list"""
5 ###########################################################
7 # idea and basic implementation by Anthony, some changes by Andreas
8 # parts are stolen from 'dak generate-releases'
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>
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.
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.
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
28 # < elmo> bah, don't bother me with annoying facts
29 # < elmo> I was on a roll
32 ################################################################################
41 from daklib import utils
42 from daklib.dbconn import get_suite, get_suite_architectures
44 ################################################################################
50 ################################################################################
52 def usage (exit_code=0):
53 print """Usage: dak generate-index-diffs [OPTIONS] [suites]
54 Write out ed-style diffs to Packages/Source lists
56 -h, --help show this help and exit
57 -c give the canonical path of the file
58 -p name for the patch (defaults to current time)
68 print "warning: removing of %s denied" % (file)
71 for ext in ["", ".gz", ".bz2"]:
72 if os.path.isfile(file + ext):
73 return (ext, os.stat(file + ext))
79 elif os.path.isfile("%s.gz" % (f)):
80 os.system("gzip -d < %s.gz > %s" % (f, t))
81 elif os.path.isfile("%s.bz2" % (f)):
82 os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
84 print "missing: %s" % (f)
88 if os.path.isfile(file):
90 elif os.path.isfile("%s.gz" % file):
91 f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
92 elif os.path.isfile("%s.bz2" % file):
93 f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
107 def __init__(self, readpath = None, max = 14):
110 self.history_order = []
112 self.readpath = readpath
113 self.filesizesha1 = None
117 f = open(readpath + "/Index")
120 def read_hashs(ind, f, self, x=x):
123 if not x or x[0] != " ": break
125 if not self.history.has_key(l[2]):
126 self.history[l[2]] = [None,None]
127 self.history_order.append(l[2])
128 self.history[l[2]][ind] = (l[0], int(l[1]))
138 if l[0] == "SHA1-History:":
139 x = read_hashs(0,f,self)
142 if l[0] == "SHA1-Patches:":
143 x = read_hashs(1,f,self)
146 if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
149 if l[0] == "SHA1-Current:" and len(l) == 3:
150 self.filesizesha1 = (l[1], int(l[2]))
157 def dump(self, out=sys.stdout):
159 out.write("Canonical-Path: %s\n" % (self.can_path))
161 if self.filesizesha1:
162 out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
165 l = self.history_order[:]
169 for h in l[:cnt-self.max]:
170 tryunlink("%s/%s.gz" % (self.readpath, h))
173 self.history_order = l[:]
175 out.write("SHA1-History:\n")
177 out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
178 out.write("SHA1-Patches:\n")
180 out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
182 def create_temp_file(r):
183 f = tempfile.TemporaryFile()
195 size = os.fstat(f.fileno())[6]
197 sha1sum = apt_pkg.sha1sum(f)
198 return (sha1sum, size)
200 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
201 if Options.has_key("NoAct"):
204 patchname = Options["PatchName"]
206 # origfile = /path/to/Packages
207 # oldfile = ./Packages
208 # newfile = ./Packages.tmp
209 # difffile = outdir/patchname
210 # index => outdir/Index
212 # (outdir, oldfile, origfile) = argv
214 newfile = oldfile + ".new"
215 difffile = "%s/%s" % (outdir, patchname)
217 upd = Updates(outdir, int(maxdiffs))
218 (oldext, oldstat) = smartstat(oldfile)
219 (origext, origstat) = smartstat(origfile)
221 print "%s: doesn't exist" % (origfile)
224 print "%s: initial run" % (origfile)
225 os.link(origfile + origext, oldfile + origext)
228 if oldstat[1:3] == origstat[1:3]:
229 print "%s: hardlink unbroken, assuming unchanged" % (origfile)
232 oldf = smartopen(oldfile)
233 oldsizesha1 = sizesha1(oldf)
235 # should probably early exit if either of these checks fail
236 # alternatively (optionally?) could just trim the patch history
239 if upd.filesizesha1 != oldsizesha1:
240 print "info: old file " + oldfile + " changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
242 if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
244 if os.path.exists(newfile): os.unlink(newfile)
245 smartlink(origfile, newfile)
246 newf = open(newfile, "r")
247 newsizesha1 = sizesha1(newf)
250 if newsizesha1 == oldsizesha1:
253 print "%s: unchanged" % (origfile)
255 if not os.path.isdir(outdir):
258 w = os.popen("diff --ed - %s | gzip -c -9 > %s.gz" %
259 (newfile, difffile), "w")
263 difff = smartopen(difffile)
264 difsizesha1 = sizesha1(difff)
267 upd.history[patchname] = (oldsizesha1, difsizesha1)
268 upd.history_order.append(patchname)
270 upd.filesizesha1 = newsizesha1
272 os.unlink(oldfile + oldext)
273 os.link(origfile + origext, oldfile + origext)
276 f = open(outdir + "/Index", "w")
282 global Cnf, Options, Logger
286 Cnf = utils.get_conf()
287 Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
288 ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
289 ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
290 ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
291 ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
292 ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
293 ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
295 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
296 Options = Cnf.SubTree("Generate-Index-Diffs::Options")
297 if Options.has_key("Help"): usage()
299 maxdiffs = Options.get("MaxDiffs::Default", "14")
300 maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
301 maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
302 maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
304 if not Options.has_key("PatchName"):
305 format = "%Y-%m-%d-%H%M.%S"
306 Options["PatchName"] = time.strftime( format )
308 AptCnf = apt_pkg.newConfiguration()
309 apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
311 if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
314 suites = Cnf.SubTree("Suite").List()
316 for suitename in suites:
317 print "Processing: " + suitename
318 SuiteBlock = Cnf.SubTree("Suite::" + suitename)
320 suiteobj = get_suite(suitename.lower())
322 # Use the canonical version of the suite name
323 suite = suiteobj.suite_name
325 if suiteobj.untouchable:
326 print "Skipping: " + suite + " (untouchable)"
329 architectures = get_suite_architectures(suite, skipall=True)
331 if SuiteBlock.has_key("Components"):
332 components = SuiteBlock.ValueList("Components")
336 suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
337 if components and suite_suffix:
338 longsuite = suite + "/" + suite_suffix
342 tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
344 if AptCnf.has_key("tree::%s" % (tree)):
345 sections = AptCnf["tree::%s::Sections" % (tree)].split()
346 elif AptCnf.has_key("bindirectory::%s" % (tree)):
347 sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
349 aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
350 print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
353 for archobj in architectures:
354 architecture = archobj.arch_string
356 if architecture != "source":
358 file = "%s/Contents-%s" % (Cnf["Dir::Root"] + tree,
360 storename = "%s/%s_contents_%s" % (Options["TempDir"], suite, architecture)
361 genchanges(Options, file + ".diff", storename, file, \
362 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::Contents" % (suite), maxcontents))
364 # use sections instead of components since dak.conf
365 # treats "foo/bar main" as suite "foo", suitesuffix "bar" and
366 # component "bar/main". suck.
368 for component in sections:
369 if architecture == "source":
370 longarch = architecture
372 maxsuite = maxsources
374 longarch = "binary-%s"% (architecture)
375 packages = "Packages"
376 maxsuite = maxpackages
378 file = "%s/%s/%s/%s" % (Cnf["Dir::Root"] + tree,
379 component, longarch, packages)
380 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
381 genchanges(Options, file + ".diff", storename, file, \
382 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::%s" % (suite, packages), maxsuite))
384 ################################################################################
386 if __name__ == '__main__':