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 import database
44 ################################################################################
51 ################################################################################
53 def usage (exit_code=0):
54 print """Usage: dak generate-index-diffs [OPTIONS] [suites]
55 Write out ed-style diffs to Packages/Source lists
57 -h, --help show this help and exit
58 -c give the canonical path of the file
59 -p name for the patch (defaults to current time)
69 print "warning: removing of %s denied" % (file)
72 for ext in ["", ".gz", ".bz2"]:
73 if os.path.isfile(file + ext):
74 return (ext, os.stat(file + ext))
80 elif os.path.isfile("%s.gz" % (f)):
81 os.system("gzip -d < %s.gz > %s" % (f, t))
82 elif os.path.isfile("%s.bz2" % (f)):
83 os.system("bzip2 -d < %s.bz2 > %s" % (f, t))
85 print "missing: %s" % (f)
89 if os.path.isfile(file):
91 elif os.path.isfile("%s.gz" % file):
92 f = create_temp_file(os.popen("zcat %s.gz" % file, "r"))
93 elif os.path.isfile("%s.bz2" % file):
94 f = create_temp_file(os.popen("bzcat %s.bz2" % file, "r"))
108 def __init__(self, readpath = None, max = 14):
111 self.history_order = []
113 self.readpath = readpath
114 self.filesizesha1 = None
118 f = open(readpath + "/Index")
121 def read_hashs(ind, f, self, x=x):
124 if not x or x[0] != " ": break
126 if not self.history.has_key(l[2]):
127 self.history[l[2]] = [None,None]
128 self.history_order.append(l[2])
129 self.history[l[2]][ind] = (l[0], int(l[1]))
139 if l[0] == "SHA1-History:":
140 x = read_hashs(0,f,self)
143 if l[0] == "SHA1-Patches:":
144 x = read_hashs(1,f,self)
147 if l[0] == "Canonical-Name:" or l[0]=="Canonical-Path:":
150 if l[0] == "SHA1-Current:" and len(l) == 3:
151 self.filesizesha1 = (l[1], int(l[2]))
158 def dump(self, out=sys.stdout):
160 out.write("Canonical-Path: %s\n" % (self.can_path))
162 if self.filesizesha1:
163 out.write("SHA1-Current: %s %7d\n" % (self.filesizesha1))
166 l = self.history_order[:]
170 for h in l[:cnt-self.max]:
171 tryunlink("%s/%s.gz" % (self.readpath, h))
174 self.history_order = l[:]
176 out.write("SHA1-History:\n")
178 out.write(" %s %7d %s\n" % (hs[h][0][0], hs[h][0][1], h))
179 out.write("SHA1-Patches:\n")
181 out.write(" %s %7d %s\n" % (hs[h][1][0], hs[h][1][1], h))
183 def create_temp_file(r):
184 f = tempfile.TemporaryFile()
196 size = os.fstat(f.fileno())[6]
198 sha1sum = apt_pkg.sha1sum(f)
199 return (sha1sum, size)
201 def genchanges(Options, outdir, oldfile, origfile, maxdiffs = 14):
202 if Options.has_key("NoAct"):
205 patchname = Options["PatchName"]
207 # origfile = /path/to/Packages
208 # oldfile = ./Packages
209 # newfile = ./Packages.tmp
210 # difffile = outdir/patchname
211 # index => outdir/Index
213 # (outdir, oldfile, origfile) = argv
215 newfile = oldfile + ".new"
216 difffile = "%s/%s" % (outdir, patchname)
218 upd = Updates(outdir, int(maxdiffs))
219 (oldext, oldstat) = smartstat(oldfile)
220 (origext, origstat) = smartstat(origfile)
222 print "%s: doesn't exist" % (origfile)
225 print "%s: initial run" % (origfile)
226 os.link(origfile + origext, oldfile + origext)
229 if oldstat[1:3] == origstat[1:3]:
230 print "%s: hardlink unbroken, assuming unchanged" % (origfile)
233 oldf = smartopen(oldfile)
234 oldsizesha1 = sizesha1(oldf)
236 # should probably early exit if either of these checks fail
237 # alternatively (optionally?) could just trim the patch history
240 if upd.filesizesha1 != oldsizesha1:
241 print "info: old file " + oldfile + " changed! %s %s => %s %s" % (upd.filesizesha1 + oldsizesha1)
243 if Options.has_key("CanonicalPath"): upd.can_path=Options["CanonicalPath"]
245 if os.path.exists(newfile): os.unlink(newfile)
246 smartlink(origfile, newfile)
247 newf = open(newfile, "r")
248 newsizesha1 = sizesha1(newf)
251 if newsizesha1 == oldsizesha1:
254 print "%s: unchanged" % (origfile)
256 if not os.path.isdir(outdir):
259 w = os.popen("diff --ed - %s | gzip -c -9 > %s.gz" %
260 (newfile, difffile), "w")
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, projectB
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"]
314 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
315 database.init(Cnf, projectB)
318 suites = Cnf.SubTree("Suite").List()
321 print "Processing: " + suite
322 SuiteBlock = Cnf.SubTree("Suite::" + suite)
324 if database.get_suite_untouchable(suite):
325 print "Skipping: " + suite + " (untouchable)"
328 suite = suite.lower()
330 architectures = database.get_suite_architectures(suite)
331 if architectures == None:
334 if SuiteBlock.has_key("Components"):
335 components = SuiteBlock.ValueList("Components")
339 suite_suffix = Cnf.Find("Dinstall::SuiteSuffix")
340 if components and suite_suffix:
341 longsuite = suite + "/" + suite_suffix
345 tree = SuiteBlock.get("Tree", "dists/%s" % (longsuite))
347 if AptCnf.has_key("tree::%s" % (tree)):
348 sections = AptCnf["tree::%s::Sections" % (tree)].split()
349 elif AptCnf.has_key("bindirectory::%s" % (tree)):
350 sections = AptCnf["bindirectory::%s::Sections" % (tree)].split()
352 aptcnf_filename = os.path.basename(utils.which_apt_conf_file())
353 print "ALERT: suite %s not in %s, nor untouchable!" % (suite, aptcnf_filename)
356 for architecture in architectures:
357 if architecture == "all":
360 if architecture != "source":
362 file = "%s/Contents-%s" % (Cnf["Dir::Root"] + tree,
364 storename = "%s/%s_contents_%s" % (Options["TempDir"], suite, architecture)
365 genchanges(Options, file + ".diff", storename, file, \
366 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::Contents" % (suite), maxcontents))
368 # use sections instead of components since dak.conf
369 # treats "foo/bar main" as suite "foo", suitesuffix "bar" and
370 # component "bar/main". suck.
372 for component in sections:
373 if architecture == "source":
374 longarch = architecture
376 maxsuite = maxsources
378 longarch = "binary-%s"% (architecture)
379 packages = "Packages"
380 maxsuite = maxpackages
382 file = "%s/%s/%s/%s" % (Cnf["Dir::Root"] + tree,
383 component, longarch, packages)
384 storename = "%s/%s_%s_%s" % (Options["TempDir"], suite, component, architecture)
385 genchanges(Options, file + ".diff", storename, file, \
386 Cnf.get("Suite::%s::Generate-Index-Diffs::MaxDiffs::%s" % (suite, packages), maxsuite))
388 ################################################################################
390 if __name__ == '__main__':