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
42 ################################################################################
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 cmd = "diff --ed - %s | gzip -c -9 > %s.gz" % (newfile, difffile)
258 # Do we need shell=True?
259 w = subprocess.Popen(cmd, shell=True, stdin=PIPE).stdin
261 # I bet subprocess can do that better than this, but lets do little steps
265 difff = smartopen(difffile)
266 difsizesha1 = sizesha1(difff)
269 upd.history[patchname] = (oldsizesha1, difsizesha1)
270 upd.history_order.append(patchname)
272 upd.filesizesha1 = newsizesha1
274 os.unlink(oldfile + oldext)
275 os.link(origfile + origext, oldfile + origext)
278 f = open(outdir + "/Index", "w")
284 global Cnf, Options, Logger
288 Cnf = utils.get_conf()
289 Arguments = [ ('h', "help", "Generate-Index-Diffs::Options::Help"),
290 ('c', None, "Generate-Index-Diffs::Options::CanonicalPath", "hasArg"),
291 ('p', "patchname", "Generate-Index-Diffs::Options::PatchName", "hasArg"),
292 ('r', "rootdir", "Generate-Index-Diffs::Options::RootDir", "hasArg"),
293 ('d', "tmpdir", "Generate-Index-Diffs::Options::TempDir", "hasArg"),
294 ('m', "maxdiffs", "Generate-Index-Diffs::Options::MaxDiffs", "hasArg"),
295 ('n', "n-act", "Generate-Index-Diffs::Options::NoAct"),
297 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
298 Options = Cnf.SubTree("Generate-Index-Diffs::Options")
299 if Options.has_key("Help"): usage()
301 maxdiffs = Options.get("MaxDiffs::Default", "14")
302 maxpackages = Options.get("MaxDiffs::Packages", maxdiffs)
303 maxcontents = Options.get("MaxDiffs::Contents", maxdiffs)
304 maxsources = Options.get("MaxDiffs::Sources", maxdiffs)
306 if not Options.has_key("PatchName"):
307 format = "%Y-%m-%d-%H%M.%S"
308 Options["PatchName"] = time.strftime( format )
310 AptCnf = apt_pkg.newConfiguration()
311 apt_pkg.ReadConfigFileISC(AptCnf,utils.which_apt_conf_file())
313 if Options.has_key("RootDir"): Cnf["Dir::Root"] = Options["RootDir"]
316 suites = Cnf.SubTree("Suite").List()
319 print "Processing: " + suite
320 SuiteBlock = Cnf.SubTree("Suite::" + suite)
322 if SuiteBlock.has_key("Untouchable"):
323 print "Skipping: " + suite + " (untouchable)"
326 suite = suite.lower()
328 architectures = SuiteBlock.ValueList("Architectures")
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 architecture in architectures:
353 if architecture == "all":
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__':