#!/usr/bin/python2.5 """ Copyright (C) 2008 Norman Messtorff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. """ import re from Global import Global class ParseStatusfile(Global): """Parsing DPKG Status files""" def __init__(self): self.debug(4, "Class ParseStatusfile initialized") def getall(self, filename="/var/lib/dpkg/status"): """Parse DPKG Status file""" try: self.debug(4, "Open file(r): " + filename) f=file(filename, 'r') except: self.debug(1, "Could not Open file: " + filename) return 1 # Post parsing requirements pat_package=re.compile('^Package:.*') pat_version=re.compile('^Version:.*') pat_installsize=re.compile('^Installed-Size:.*') pat_status=re.compile('^Status:.*') pat_status_purge=re.compile('^purge.*') b_packager="0" b_versionr="0" b_installsizer="0" b_statusr="0" result = [ ] # Parse dpkg status, don't change the order! for line in f.readlines(): b_package=pat_package.match(line) if b_package: b_packager=b_package.string[b_package.start()+9:b_package.end()] self.debug(5,"Package found: "+b_packager) b_status=pat_status.match(line) if b_status: b_statusr=b_status.string[b_status.start()+8:b_status.end()] self.debug(5,"Status found: "+b_statusr) # purged packages doesn't have "Version: " or "Installed-Size: " fields if pat_status_purge.match(b_statusr): self.debug(5, "Purged Packet, setting flags...") b_versionr="none" b_installsizer="none" b_installsize=pat_installsize.match(line) if b_installsize: b_installsizer=b_installsize.string[b_installsize.start()+16:b_installsize.end()] self.debug(5,"Install-Size found: "+b_installsizer) b_version=pat_version.match(line) if b_version: b_versionr=b_version.string[b_version.start()+9:b_version.end()] self.debug(5,"Version found: "+b_versionr) # order? if b_packager and b_statusr and b_installsizer and b_versionr != "0": self.debug(5,"Full Package information: "+b_packager+", "+b_versionr+", "+b_installsizer+", "+b_statusr) result.append([b_packager, b_statusr, b_installsizer, b_versionr]) b_packager="0" b_versionr="0" b_installsizer="0" b_statusr="0" f.close() return result