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 ################################################################################
39 from daklib import utils
41 ################################################################################
48 ################################################################################
50 def usage (exit_code=0):
51 print """Usage: dak generate-index-diffs [OPTIONS] [suites]
52 Write out ed-style diffs to Packages/Source lists
54 -h, --help show this help and exit
55 -c give the canonical path of the file
56 -p name for the patch (defaults to current time)
66 print "warning: removing of %s denied" % (file)
69 for ext in ["", ".gz", ".bz2"]:
70 if os.path.isfile(file + ext):
71 return (ext, os.stat(file + ext))
77 elif os.path.isfile("%s.gz" % (f)):
78 os.system("gzip -d < %s.gz > %s" % (f, t))
79 elif os.path.isfile("%s.bz2" % (f)):
80 os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
82 print "missing: %s" % (f)
86 if os.path.isfile(file):
88 elif os.path.isfile("%s.gz" % file):
89 f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
90 elif os.path.isfile("%s.bz2" % file):
91 f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
105 def __init__(self, readpath = None, max = 14):
108 self.history_order = []
110 self.readpath = readpath
111 self.filesizesha1 = None
115 f = open(readpath + "/Index")
118 def read_hashs(ind, f, self, x=x):
121 if not x or x[0] != " ": break
123 if not self.history.has_key(l[2]):
124 self.history[l[2]] = [None,None]
125 self.history_order.append(l[2])
126 self.history[l[2]][ind] = (l[0], int(l[1]))
136 if l[0] == "SHA1-History:":
137 x = read_hashs(0,f,self)
140 if l[0] == "SHA1-Patches:":
141 x = read_hashs(1,f,self)
144 if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
147 if l[0] == "SHA1-Current:" and len(l) == 3:
148 self.filesizesha1 = (l[1], int(l[2]))
155 def dump(self, out=sys.stdout):
157 out.write("Canonical-Path: %s\n" % (self.can_path))
159 if self.filesizesha1:
160 out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
163 l = self.history_order[:]
167 for h in l[:cnt-self.max]:
168 tryunlink("%s/%s.gz" % (self.readpath, h))
171 self.history_order = l[:]
173 out.write("SHA1-History:\n")
175 out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
176 out.write("SHA1-Patches:\n")
178 out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
180 def create_temp_file(r):
181 f = tempfile.TemporaryFile()
193 size = os.fstat(f.fileno())[6]
195 sha1sum = apt_pkg.sha1sum(f)
196 return (sha1sum, size)
198 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
199 if Options.has_key("NoAct"):
202 patchname = Options["PatchName"]
204 # origfile = /path/to/Packages
205 # oldfile = ./Packages
206 # newfile = ./Packages.tmp
207 # difffile = outdir/patchname
208 # index => outdir/Index
210 # (outdir, oldfile, origfile) = argv
212 newfile = oldfile + ".new"
213 difffile = "%s/%s" % (outdir, patchname)
215 upd = Updates(outdir, int(maxdiffs))
216 (oldext, oldstat) = smartstat(oldfile)
217 (origext, origstat) = smartstat(origfile)
219 print "%s: doesn't exist" % (origfile)
222 print "%s: initial run" % (origfile)
223 os.link(origfile + origext, oldfile + origext)
226 if oldstat[1:3] == origstat[1:3]:
227 print "%s: hardlink unbroken, assuming unchanged" % (origfile)
230 oldf = smartopen(oldfile)
231 oldsizesha1 = sizesha1(oldf)
233 # should probably early exit if either of these checks fail
234 # alternatively (optionally?) could just trim the patch history
237 if upd.filesizesha1 != oldsizesha1:
238 print "info: old file " + oldfile + " changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
240 if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
242 if os.path.exists(newfile): os.unlink(newfile)
243 smartlink(origfile, newfile)
244 newf = open(newfile, "r")
245 newsizesha1 = sizesha1(newf)
248 if newsizesha1 == oldsizesha1:
251 print "%s: unchanged" % (origfile)
253 if not os.path.isdir(outdir):
256 cmd = "diff --ed - %s | gzip -c -9 > %s.gz" % (newfile, difffile)
257 # Do we need shell=True?
258 w = subprocess.Popen(cmd, shell=True, stdin=PIPE).stdin
260 # I bet subprocess can do that better than this, but lets do little steps
264 difff = smartopen(difffile)
265 difsizesha1 = sizesha1(difff)
268 upd.history[patchname] = (oldsizesha1, difsizesha1)
269 upd.history_order.append(patchname)
271 upd.filesizesha1 = newsizesha1
273 os.unlink(oldfile + oldext)
274 os.link(origfile + origext, oldfile + origext)
277 f = open(outdir + "/Index", "w")
283 global Cnf, Options, Logger
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"),
296 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
297 Options = Cnf.SubTree("Generate-Index-Diffs::Options")
298 if Options.has_key("Help"): usage()
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)
305 if not Options.has_key("PatchName"):
306 format = "%Y-%m-%d-%H%M.%S"
307 Options["PatchName"] = time.strftime( format )
309 AptCnf = apt_pkg.newConfiguration()
310 apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
312 if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
315 suites = Cnf.SubTree("Suite").List()
318 print "Processing: " + suite
319 SuiteBlock = Cnf.SubTree("Suite::" + suite)
321 if SuiteBlock.has_key("Untouchable"):
322 print "Skipping: " + suite + " (untouchable)"
325 suite = suite.lower()
327 architectures = SuiteBlock.ValueList("Architectures")
329 if SuiteBlock.has_key("Components"):
330 components = SuiteBlock.ValueList("Components")
334 suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
335 if components and suite_suffix:
336 longsuite = suite + "/" + suite_suffix
340 tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
342 if AptCnf.has_key("tree::%s" % (tree)):
343 sections = AptCnf["tree::%s::Sections" % (tree)].split()
344 elif AptCnf.has_key("bindirectory::%s" % (tree)):
345 sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
347 aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
348 print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
351 for architecture in architectures:
352 if architecture == "all":
355 if architecture != "source":
357 file = "%s/Contents-%s" % (Cnf["Dir::Root"] + tree,
359 storename = "%s/%s_contents_%s" % (Options["TempDir"], suite, architecture)
360 genchanges(Options, file + ".diff", storename, file, \
361 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::Contents" % (suite), maxcontents))
363 # use sections instead of components since dak.conf
364 # treats "foo/bar main" as suite "foo", suitesuffix "bar" and
365 # component "bar/main". suck.
367 for component in sections:
368 if architecture == "source":
369 longarch = architecture
371 maxsuite = maxsources
373 longarch = "binary-%s"% (architecture)
374 packages = "Packages"
375 maxsuite = maxpackages
377 file = "%s/%s/%s/%s" % (Cnf["Dir::Root"] + tree,
378 component, longarch, packages)
379 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
380 genchanges(Options, file + ".diff", storename, file, \
381 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::%s" % (suite, packages), maxsuite))
383 ################################################################################
385 if __name__ == '__main__':