1""" Helper functions for the various changes formats
3@contact: Debian FTPMaster <ftpmaster@debian.org>
4@copyright: 2009, 2010 Joerg Jaspert <joerg@debian.org>
5@copyright: 2009 Chris Lamb <lamby@debian.org>
6@license: GNU General Public License version 2 or later
7"""
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23################################################################################
25# <mhy> !!!!11111iiiiiioneoneoneone
26# <dak> mhy: Error: "!!!11111iiiiiioneoneoneone" is not a valid command.
27# <mhy> dak: oh shut up
28# <dak> mhy: Error: "oh" is not a valid command.
30################################################################################
32from .regexes import re_verwithext
33from .dak_exceptions import UnknownFormatError
36def parse_format(txt: str) -> tuple:
37 """
38 Parse a .changes Format string into a tuple representation for easy
39 comparison.
41 >>> parse_format('1.0')
42 (1, 0)
43 >>> parse_format('8.4 (hardy)')
44 (8, 4, 'hardy')
46 If the format doesn't match these forms, raises UnknownFormatError.
48 :param txt: Format string to parse
49 :return: Parsed format
51 :raises UnknownFormatError: Unknown Format: line
52 """
54 format = re_verwithext.search(txt)
56 if format is None:
57 raise UnknownFormatError(txt)
59 format = format.groups()
61 if format[1] is None: 61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never true
62 format = int(float(format[0])), 0, format[2]
63 else:
64 format = int(format[0]), int(format[1]), format[2]
66 if format[2] is None: 66 ↛ 69line 66 didn't jump to line 69, because the condition on line 66 was never false
67 format = format[:2]
69 return format
72def validate_changes_format(format: tuple[int], field: str) -> None:
73 """
74 Validate a tuple-representation of a .changes Format: field. Raises
75 UnknownFormatError if the field is invalid, otherwise return type is
76 undefined.
78 :raises UnknownFormatError: if the field is invalid
79 """
81 if (format < (1, 5) or format > (1, 8)):
82 raise UnknownFormatError(repr(format))
84 if field != 'files' and format < (1, 8):
85 raise UnknownFormatError(repr(format))