#!/usr/bin/python # Copyright 2007 by Alexander Schmehl, tolimar@debian.org # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 (only) of the GNU General Public License # as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import fileinput import sys ## This function creates an empty dictionary for us def newpackagedict(): return {'architecture': '', 'build-essential': 'no', 'conflicts': '', 'depends': '', 'description': '', 'enhances': '', 'essential': 'no', 'filename': '', 'installed-size': '', 'maintainer': '', 'md5sum': '', 'origin': '', 'package': '', 'pre-depends': '', 'priority': '', 'provides': '', 'recommends': '', 'replaces': '', 'section': '', 'sha1': '', 'sha256': '', 'size': '', 'source': '', 'suggests': '', 'tag': '', 'task': '', 'version': '', 'longdescription': '', 'url': ''} ## This funtion does the work... well... Perhaps I'll splitt it later def main(packagefile, withhead): # open a file for writing; just add .csv to the suplied filename file = open(packagefile+".csv", 'w') # write the csv-header if user wished to have one if withhead: file.write("package;source;priority;section;installed-size;maintainer;architecture;version;depends;conflicts;recommends;suggests;enhances;pre-depends;provides;replaces;build-essential;essential;filename;md5sum;origin;sha1;sha256;size;tag;task;description;longdescription;url\n") package = newpackagedict() # now loop over every line of the package file for line in fileinput.input([packagefile]): # if the line isn't empty, we have some stuff to add... if len(line) != 1: # everything but the long description starts with a real character if not line.startswith(" "): splittedline = line.split(":", 1) package[splittedline[0].lower()] = splittedline[1].strip().replace('"', "'") # the long description is handeled here else: longdescription = package['longdescription'] longdescription = longdescription + line package['longdescription'] = package['longdescription'] + line.rstrip().replace('"', "'") # ... if the current line is empty, we write our stuff to the csv else: # let's try to find a URL if mentioned in the long description; doesn't work for all if package['longdescription'].upper().rfind('HTTP') : package['url'] = package['longdescription'][package['longdescription'].upper().rfind('HTTP'):] # If the binary-packages name is the same as the source-package ones, # it isn't mentioned in the package file # we add it for convenience if package['source'] == '': package['source'] = package['package'] # create the line, which should be added to the csv packageline = '' for iterator in ['package', 'source', 'priority', 'section', 'installed-size', 'maintainer', 'architecture', 'version', 'depends', 'conflicts', 'recommends', 'suggests', 'enhances', 'pre-depends', 'provides', 'replaces', 'build-essential', 'essential', 'filename', 'md5sum', 'origin', 'sha1', 'sha256', 'size', 'tag', 'task', 'description', 'longdescription', 'url']: if package[iterator] != "" : packageline += "\""+package[iterator]+"\"" # url is the last data to be added to the line if iterator != 'url' : packageline += ";" else: packageline += "\n" # finally write the line to the file, and create a new empty dictionary file.write(packageline) package = newpackagedict() # close the file; we are done file.close() ## ## Main Program ## if len (sys.argv) > 3 or "-h" in sys.argv or "--help" in sys.argv or "-u" in sys.argv: print "Please specify the uncompressed Package-File you want to use as\nfirst parameter on the command line.\nMake sure you have enough free space left." print "The only know Parameter for now is: '-w', which will indicate\nthat the created csv-File should have a header" else : main (sys.argv[1], "-w" in sys.argv)