Commit c3c2778e authored by Guido Günther's avatar Guido Günther

Add debian/source/format parser

parent 24881615
# vim: set fileencoding=utf-8 :
# (C) 2012 Guido Günther <>
# 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
# 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
"""Parse debian/source/format"""
class DebianSourceFormatError(Exception):
class DebianSourceFormat(object):
Contents of debian/source/format
>>> d = DebianSourceFormat("3.0 (quilt)")
>>> d.type
>>> d.version
>>> d = DebianSourceFormat("3.0 (native)")
>>> d.type
>>> d = DebianSourceFormat("1.0")
>>> d.type
>>> d.version
>>> d = DebianSourceFormat("1.0 broken")
Traceback (most recent call last):
DebianSourceFormatError: Cannot get source format from '1.0 broken'
def _parse(self, content):
parts = content.split()
self._version = parts[0]
if len(parts) == 2:
if (parts[1][0] == '(' and
parts[1][-1] == ')'):
self._type = parts[1][1:-1]
raise DebianSourceFormatError("Cannot get source format from "
"'%s'" % content)
def __init__(self, content):
self._version = None
self._type = None
def version(self):
"""The source format version number"""
return self._version
def type(self):
"""The 'type' (e.g. git, native)"""
return self._type
def parse_file(klass, filename):
Parse debian/source/format file
@param filename: the file to parse
@type filename: C{str}
@returns: a debisn/source/format object
@rtype: L{DebianSourceFormat}
>>> import tempfile, os
>>> with tempfile.NamedTemporaryFile(delete=False) as t:
... t.write("3.0 (quilt)")
>>> d = DebianSourceFormat.parse_file(
>>> d.version
>>> d.type
>>> os.unlink(
with file(filename) as f:
return klass(
if __name__ == "__main__":
import doctest
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment