Author: fw Date: 2010-05-06 13:58:56 +0000 (Thu, 06 May 2010) New Revision: 14615 Added: lib/python/parsers.py Log: lib/python/parsers.py: various text file parsers Added: lib/python/parsers.py ==================================================================--- lib/python/parsers.py (rev 0) +++ lib/python/parsers.py 2010-05-06 13:58:56 UTC (rev 14615) @@ -0,0 +1,73 @@ +# parsers -- various text file parsers +# Copyright (C) 2010 Florian Weimer <fw at deneb.enyo.de> +# +# 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 operator +import re + +import xpickle +import debian_support + +FORMAT = "1" + + at xpickle.loader("BINARY" + FORMAT) +def binarypackages(name, f): + """Returns a sequence of binary package names""" + obj = set(v for p in debian_support.PackageFile(name, f) + for k, v in p if k == "Package") + obj = list(obj) + obj.sort() + return tuple(obj) + + + at xpickle.loader("SOURCE" + FORMAT) +def sourcepackages(name, f): + """Returns a dictionary of source package objects. + + The keys are strings, containing the source package name, the + values are corresponding source package versions.""" + + data = {} + for p in debian_support.PackageFile(name, f): + pkg_name, pkg_version = (None, None) + for name, contents in p: + if name == "Package": + pkg_name = intern(contents) + elif name == "Version": + pkg_version = contents + if pkg_name is None: + raise SyntaxError("package record does not contain package name") + if pkg_version is None: + raise SyntaxError("package record for %s does not contain version" + % pkg_name) + if pkg_name in data: + oversion = debian_support.Version(data[pkg_name]) + if oversion >= debian_support.Version(pkg_version): + continue + data[pkg_name] = pkg_version + return data + +def _test(): + o = binarypackages("../../data/packages/sid__main_i386_Packages") + assert type(o) == type(()) + assert "bash" in o + + o = sourcepackages("../../data/packages/sid__main_Sources") + assert type(o) == type({}) + assert "bash" in o + +if __name__ == "__main__": + _test()