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