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 ################################################################################
40 from daklib import utils
41 from daklib.dbconn import get_suite, get_suite_architectures
43 ################################################################################
49 ################################################################################
51 def usage (exit_code=0):
52 print """Usage: dak generate-index-diffs [OPTIONS] [suites]
53 Write out ed-style diffs to Packages/Source lists
55 -h, --help show this help and exit
56 -c give the canonical path of the file
57 -p name for the patch (defaults to current time)
67 print "warning: removing of %s denied" % (file)
70 for ext in ["", ".gz", ".bz2"]:
71 if os.path.isfile(file + ext):
72 return (ext, os.stat(file + ext))
78 elif os.path.isfile("%s.gz" % (f)):
79 os.system("gzip -d < %s.gz > %s" % (f, t))
80 elif os.path.isfile("%s.bz2" % (f)):
81 os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
83 print "missing: %s" % (f)
87 if os.path.isfile(file):
89 elif os.path.isfile("%s.gz" % file):
90 f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
91 elif os.path.isfile("%s.bz2" % file):
92 f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
106 def __init__(self, readpath = None, max = 14):
109 self.history_order = []
111 self.readpath = readpath
112 self.filesizesha1 = None
116 f = open(readpath + "/Index")
119 def read_hashs(ind, f, self, x=x):
122 if not x or x[0] != " ": break
124 if not self.history.has_key(l[2]):
125 self.history[l[2]] = [None,None]
126 self.history_order.append(l[2])
127 self.history[l[2]][ind] = (l[0], int(l[1]))
137 if l[0] == "SHA1-History:":
138 x = read_hashs(0,f,self)
141 if l[0] == "SHA1-Patches:":
142 x = read_hashs(1,f,self)
145 if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
148 if l[0] == "SHA1-Current:" and len(l) == 3:
149 self.filesizesha1 = (l[1], int(l[2]))
156 def dump(self, out=sys.stdout):
158 out.write("Canonical-Path: %s\n" % (self.can_path))
160 if self.filesizesha1:
161 out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
164 l = self.history_order[:]
168 for h in l[:cnt-self.max]:
169 tryunlink("%s/%s.gz" % (self.readpath, h))
172 self.history_order = l[:]
174 out.write("SHA1-History:\n")
176 out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
177 out.write("SHA1-Patches:\n")
179 out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
181 def create_temp_file(r):
182 f = tempfile.TemporaryFile()
194 size = os.fstat(f.fileno())[6]
196 sha1sum = apt_pkg.sha1sum(f)
197 return (sha1sum, size)
199 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
200 if Options.has_key("NoAct"):
203 patchname = Options["PatchName"]
205 # origfile = /path/to/Packages
206 # oldfile = ./Packages
207 # newfile = ./Packages.tmp
208 # difffile = outdir/patchname
209 # index => outdir/Index
211 # (outdir, oldfile, origfile) = argv
213 newfile = oldfile + ".new"
214 difffile = "%s/%s" % (outdir, patchname)
216 upd = Updates(outdir, int(maxdiffs))
217 (oldext, oldstat) = smartstat(oldfile)
218 (origext, origstat) = smartstat(origfile)
220 print "%s: doesn't exist" % (origfile)
223 print "%s: initial run" % (origfile)
224 os.link(origfile + origext, oldfile + origext)
227 if oldstat[1:3] == origstat[1:3]:
228 print "%s: hardlink unbroken, assuming unchanged" % (origfile)
231 oldf = smartopen(oldfile)
232 oldsizesha1 = sizesha1(oldf)
234 # should probably early exit if either of these checks fail
235 # alternatively (optionally?) could just trim the patch history
238 if upd.filesizesha1 != oldsizesha1:
239 print "info: old file " + oldfile + " changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
241 if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
243 if os.path.exists(newfile): os.unlink(newfile)
244 smartlink(origfile, newfile)
245 newf = open(newfile, "r")
246 newsizesha1 = sizesha1(newf)
249 if newsizesha1 == oldsizesha1:
252 print "%s: unchanged" % (origfile)
254 if not os.path.isdir(outdir):
257 w = os.popen("diff --ed - %s | gzip --rsyncable -c -9 > %s.gz" %
258 (newfile, difffile), "w")
262 difff = smartopen(difffile)
263 difsizesha1 = sizesha1(difff)
266 upd.history[patchname] = (oldsizesha1, difsizesha1)
267 upd.history_order.append(patchname)
269 upd.filesizesha1 = newsizesha1
271 os.unlink(oldfile + oldext)
272 os.link(origfile + origext, oldfile + origext)
275 f = open(outdir + "/Index", "w")
281 global Cnf, Options, Logger
285 Cnf = utils.get_conf()
286 Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
287 ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
288 ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
289 ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
290 ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
291 ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
292 ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
294 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
295 Options = Cnf.SubTree("Generate-Index-Diffs::Options")
296 if Options.has_key("Help"): usage()
298 maxdiffs = Options.get("MaxDiffs::Default", "14")
299 maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
300 maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
301 maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
303 if not Options.has_key("PatchName"):
304 format = "%Y-%m-%d-%H%M.%S"
305 Options["PatchName"] = time.strftime( format )
307 AptCnf = apt_pkg.newConfiguration()
308 apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
310 if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
313 suites = Cnf.SubTree("Suite").List()
315 for suitename in suites:
316 print "Processing: " + suitename
317 SuiteBlock = Cnf.SubTree("Suite::" + suitename)
319 suiteobj = get_suite(suitename.lower())
321 # Use the canonical version of the suite name
322 suite = suiteobj.suite_name
324 if suiteobj.untouchable:
325 print "Skipping: " + suite + " (untouchable)"
328 architectures = get_suite_architectures(suite, skipall=True)
330 if SuiteBlock.has_key("Components"):
331 components = SuiteBlock.ValueList("Components")
335 suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
336 if components and suite_suffix:
337 longsuite = suite + "/" + suite_suffix
341 tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
343 if AptCnf.has_key("tree::%s" % (tree)):
344 sections = AptCnf["tree::%s::Sections" % (tree)].split()
345 elif AptCnf.has_key("bindirectory::%s" % (tree)):
346 sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
348 aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
349 print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
352 for archobj in architectures:
353 architecture = archobj.arch_string
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__':