From c7df84174a706874e835d688df577e20f1c25abd Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Wed, 21 May 2008 19:03:08 +0100 Subject: [PATCH] Change of plan: take a .deb package file as input --- deb2zero | 77 +++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/deb2zero b/deb2zero index 383136d..4b05cf4 100755 --- a/deb2zero +++ b/deb2zero @@ -4,6 +4,7 @@ import sys from optparse import OptionParser +import tempfile, shutil, os from xml.dom import minidom import subprocess @@ -29,7 +30,7 @@ valid_categories = [ 'Utility', ] -parser = OptionParser('usage: %prog [options] package\n' +parser = OptionParser('usage: %prog [options] package.deb\n' 'Create a Zero Install feed for a Debian package.') (options, args) = parser.parse_args() @@ -37,18 +38,25 @@ if len(args) != 1: parser.print_help() sys.exit(1) -pkg_name = args[0] +deb_file = os.path.abspath(args[0]) -child = subprocess.Popen(['apt-cache', 'show', pkg_name], stdout = subprocess.PIPE) -details, unused = child.communicate() -if child.returncode: - print >>sys.stderr, "apt-cache failed: code = %d" % child.returncode - sys.exit(1) +def read_child(cmd): + child = subprocess.Popen(cmd, stdout = subprocess.PIPE) + output, unused = child.communicate() + if child.returncode: + print >>sys.stderr, output + print >>sys.stderr, "%s: code = %d" % (' '.join(cmd), child.returncode) + sys.exit(1) + return output + +details = read_child(['dpkg-deb', '--info', deb_file]) -description_and_summary = details.split('\nDescription: ')[1].split('\n') +description_and_summary = details.split('\n Description: ')[1].split('\n') summary = description_and_summary[0] description = '' for x in description_and_summary[1:]: + assert x[0] == ' ' + x = x[1:] if x[0] != ' ': break if x == ' .': @@ -57,36 +65,45 @@ for x in description_and_summary[1:]: description += x[1:].replace('. ', '. ') + '\n' description = description.strip() +pkg_name = '(unknown)' category = None for line in details.split('\n'): - if line.startswith('Section:'): - deb_cat = line.split(':', 1)[1].strip() - category = deb_category_to_freedesktop.get(deb_cat) - if category: - break - else: - print >>sys.stderr, "Warning: no mapping for Debian category '%s'" % deb_cat + if not line: continue + assert line.startswith(' ') + line = line[1:] + if ':' in line: + key, value = line.split(':', 1) + value = value.strip() + if key == 'Section': + category = deb_category_to_freedesktop.get(value) + if category: + break + else: + print >>sys.stderr, "Warning: no mapping for Debian category '%s'" % value + elif key == 'Package': + pkg_name = value template = ''' ''' doc = minidom.parseString(template) root = doc.documentElement -child = subprocess.Popen(['dpkg', '-L', pkg_name], stdout = subprocess.PIPE) -files, unused = child.communicate() -if child.returncode: - print >>sys.stderr, "dpkg -L failed: code = %d" % child.returncode - sys.exit(1) - -for f in files.split('\n'): - if f.endswith('.desktop'): - for line in file(f): - if line.startswith('Categories'): - for cat in line.split('=', 1)[1].split(';'): - cat = cat.strip() - if cat in valid_categories: - category = cat - break +tmp = tempfile.mkdtemp(prefix = 'deb2zero-') +try: + files = read_child(['dpkg-deb', '-X', deb_file, tmp]) + + for f in files.split('\n'): + if f.endswith('.desktop'): + f = os.path.join(tmp, f) + for line in file(f): + if line.startswith('Categories'): + for cat in line.split('=', 1)[1].split(';'): + cat = cat.strip() + if cat in valid_categories: + category = cat + break +finally: + shutil.rmtree(tmp) def add_text(parent, element, text): doc = parent.ownerDocument -- 2.11.4.GIT