diff --git a/debian/copyright b/debian/copyright index f1c5b02..2bfa848 100644 --- a/debian/copyright +++ b/debian/copyright @@ -194,10 +194,10 @@ Files: doc/pull-debian-debdiff.1, ubuntutools/update_maintainer.py, update-maintainer, wrap-and-sort -Copyright: 2010, Benjamin Drung - 2010, Evan Broder - 2008, Siegfried-Angel Gevatter Pujals - 2010, Stefano Rivera +Copyright: 2010, Benjamin Drung + 2010, Evan Broder + 2008, Siegfried-Angel Gevatter Pujals + 2010-2011, Stefano Rivera License: ISC Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/pull-debian-debdiff b/pull-debian-debdiff index 053f5c5..5e68237 100755 --- a/pull-debian-debdiff +++ b/pull-debian-debdiff @@ -2,7 +2,7 @@ # pull-debian-debdiff - find and download a specific version of a Debian # package and its immediate parent to generate a debdiff. # -# Copyright (C) 2010, Stefano Rivera +# Copyright (C) 2010-2011, Stefano Rivera # Inspired by a tool of the same name by Kees Cook. # # Permission to use, copy, modify, and/or distribute this software for any @@ -105,19 +105,7 @@ def main(): Logger.error(str(e)) sys.exit(1) oldpkg.unpack() - - cmd = ['debdiff', oldpkg.dsc_name, newpkg.dsc_name] - difffn = newpkg.dsc_name[:-3] + 'debdiff' - Logger.command(cmd + ['> %s' % difffn]) - debdiff_file = open(difffn, 'w') - if subprocess.call(cmd, stdout=debdiff_file) > 2: - Logger.error('Debdiff failed.') - sys.exit(1) - debdiff_file.close() - cmd = ('diffstat', '-p0', difffn) - Logger.command(cmd) - subprocess.check_call(cmd) - print 'file://' + os.path.abspath(difffn) + print 'file://' + oldpkg.debdiff(newpkg, diffstat=True) if __name__ == '__main__': main() diff --git a/ubuntutools/archive.py b/ubuntutools/archive.py index e144557..f337240 100644 --- a/ubuntutools/archive.py +++ b/ubuntutools/archive.py @@ -1,7 +1,7 @@ # archive.py - Functions for dealing with Debian source packages, archives, # and mirrors. # -# Copyright (C) 2010, Stefano Rivera +# Copyright (C) 2010-2011, Stefano Rivera # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -27,6 +27,8 @@ Approach: 3. Verify checksums. """ +from __future__ import with_statement + import hashlib import os.path import subprocess @@ -74,13 +76,12 @@ class Dsc(debian.deb822.Dsc): if os.path.getsize(pathname) != size: return False hash_func = getattr(hashlib, alg)() - f = open(pathname, 'rb') - while True: - buf = f.read(hash_func.block_size) - if buf == '': - break - hash_func.update(buf) - f.close() + with open(pathname, 'rb') as f: + while True: + buf = f.read(hash_func.block_size) + if buf == '': + break + hash_func.update(buf) return hash_func.hexdigest() == digest return False @@ -270,16 +271,15 @@ class SourcePackage(object): except urllib2.HTTPError: return False - out = open(pathname, 'wb') - while True: - block = in_.read(10240) - if block == '': - break - out.write(block) - Logger.stdout.write('.') - Logger.stdout.flush() + with open(pathname, 'wb') as out: + while True: + block = in_.read(10240) + if block == '': + break + out.write(block) + Logger.stdout.write('.') + Logger.stdout.flush() in_.close() - out.close() Logger.stdout.write(' done\n') Logger.stdout.flush() if self.dsc and not url.endswith('.dsc'): @@ -313,6 +313,24 @@ class SourcePackage(object): Logger.command(cmd) subprocess.check_call(cmd, cwd=self.workdir) + def debdiff(self, newpkg, diffstat=False): + """Write a debdiff comparing this src pkg to a newer one. + Optionally print diffstat. + Return the debdiff filename. + """ + cmd = ['debdiff', self.dsc_name, newpkg.dsc_name] + difffn = newpkg.dsc_name[:-3] + 'debdiff' + Logger.command(cmd + ['> %s' % difffn]) + with open(difffn, 'w') as f: + if subprocess.call(cmd, stdout=f, cwd=self.workdir) > 2: + Logger.error('Debdiff failed.') + sys.exit(1) + if diffstat: + cmd = ('diffstat', '-p1', difffn) + Logger.command(cmd) + subprocess.check_call(cmd) + return os.path.abspath(difffn) + class DebianSourcePackage(SourcePackage): "Download / unpack a Debian source package"