format.py 3.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# vim: set fileencoding=utf-8 :
#
# (C) 2012 Guido Günther <agx@sigxcpu.org>
#    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
15 16
#    along with this program; if not, please see
#    <http://www.gnu.org/licenses/>
17 18
"""Parse debian/source/format"""

Guido Günther's avatar
Guido Günther committed
19

20 21 22
class DebianSourceFormatError(Exception):
    pass

Guido Günther's avatar
Guido Günther committed
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
class DebianSourceFormat(object):
    """
    Contents of debian/source/format

    >>> d = DebianSourceFormat("3.0 (quilt)")
    >>> d.type
    'quilt'
    >>> d.version
    '3.0'
    >>> d = DebianSourceFormat("3.0 (native)")
    >>> d.type
    'native'
    >>> d = DebianSourceFormat("1.0")
    >>> d.type
    >>> d.version
    '1.0'
    >>> d = DebianSourceFormat("1.0 broken")
    Traceback (most recent call last):
    ...
43
    gbp.deb.format.DebianSourceFormatError: Cannot get source format from '1.0 broken'
44
    """
Guido Günther's avatar
Guido Günther committed
45
    format_file = 'debian/source/format'
46 47 48 49 50 51

    def _parse(self, content):
        parts = content.split()

        self._version = parts[0]
        if len(parts) == 2:
Guido Günther's avatar
Guido Günther committed
52
            if (parts[1][0] == '(' and parts[1][-1] == ')'):
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
                self._type = parts[1][1:-1]
            else:
                raise DebianSourceFormatError("Cannot get source format from "
                                              "'%s'" % content)

    def __init__(self, content):
        self._version = None
        self._type = None
        self._parse(content)

    @property
    def version(self):
        """The source format version number"""
        return self._version

    @property
    def type(self):
        """The 'type' (e.g. git, native)"""
        return self._type

Guido Günther's avatar
Guido Günther committed
73 74 75
    def __str__(self):
        return "%s (%s)" % (self._version, self._type)

76
    @classmethod
Guido Günther's avatar
Guido Günther committed
77
    def parse_file(cls, filename):
78 79 80 81 82 83 84 85 86 87
        """
        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:
Guido Günther's avatar
Guido Günther committed
88
        ...    ret = t.write(b"3.0 (quilt)")
89 90 91 92 93 94 95
        >>> d = DebianSourceFormat.parse_file(t.name)
        >>> d.version
        '3.0'
        >>> d.type
        'quilt'
        >>> os.unlink(t.name)
        """
96
        with open(filename) as f:
Guido Günther's avatar
Guido Günther committed
97
            return cls(f.read())
98

Guido Günther's avatar
Guido Günther committed
99
    @classmethod
Guido Günther's avatar
Guido Günther committed
100
    def from_content(cls, version, type, format_file=None):
Guido Günther's avatar
Guido Günther committed
101 102 103 104 105 106 107 108 109
        """
        Write a format file from I{type} and I{format} at
        I{format_file}

        @param version: the source package format version
        @param type: the format type
        @param format_file: the format file to create with
            the above parameters
        """
Guido Günther's avatar
Guido Günther committed
110 111
        format_file = format_file or cls.format_file
        with open(cls.format_file, 'w') as f:
Guido Günther's avatar
Guido Günther committed
112
            f.write("%s (%s)" % (version, type))
Guido Günther's avatar
Guido Günther committed
113
        return cls.parse_file(cls.format_file)
114

Guido Günther's avatar
Guido Günther committed
115

116 117 118
if __name__ == "__main__":
    import doctest
    doctest.testmod()